W
W
wrongangel2012-05-02 21:04:24
Facebook
wrongangel, 2012-05-02 21:04:24

How to calculate the amount of likes?

In general, there is one site that has a lot of "I like" "Like" buttons and so on (facebook, vkontakte, google +, my world, classmates, twitter).
It is necessary to automatically calculate the amount of likes and tweets.
How to do this if possible?
And what to do, except how to hang yourself, if not possible? :D

Answer the question

In order to leave comments, you need to log in

8 answer(s)
W
wrongangel, 2012-05-02
@wrongangel

I wonder if I figure it out, is it worth writing an article about this?

K
Kirill Firsov, 2013-11-13
@Isis


var services = {
  facebook: {
    counterUrl: 'http://graph.facebook.com/fql?q=SELECT+total_count+FROM+link_stat+WHERE+url%3D%22{url}%22&callback=?',
    convertNumber: function(data) {
      return data.data[0].total_count;
    }
  },
  twitter: {
    counterUrl: 'http://urls.api.twitter.com/1/urls/count.json?url={url}&callback=?',
    convertNumber: function(data) {
      return data.count;
    }
  },
  vkontakte: {
    counterUrl: 'http://vkontakte.ru/share.php?act=count&url={url}&index={index}',
    counter: function(jsonUrl, deferred) {
      var options = services.vkontakte;
      if (!options._) {
        options._ = [];
        if (!window.VK) window.VK = {};
        window.VK.Share = {
          count: function(idx, number) {
            options._[idx].resolve(number);
          }
        };
      }

      var index = options._.length;
      options._.push(deferred);
      $.ajax({
        url: makeUrl(jsonUrl, {index: index}),
        dataType: 'jsonp'
      });
    }
  }
};

/**
 * Counters manager
 */
var counters = {
  promises: {},
  fetch: function(service, url) {
    if (!counters.promises[service]) counters.promises[service] = {};
    var servicePromises = counters.promises[service];

    if (servicePromises[url]) {
      return servicePromises[url];
    }
    else {
      var options = services[service],
        deferred = $.Deferred(),
        jsonUrl = options.counterUrl && makeUrl(options.counterUrl, {url: url});

      if ($.isFunction(options.counter)) {
        options.counter(jsonUrl, deferred);
      }
      else if (options.counterUrl) {
        $.getJSON(jsonUrl)
          .done(function(data) {
            try {
              var number = data;
              if ($.isFunction(options.convertNumber)) {
                number = options.convertNumber(data);
              }
              deferred.resolve(number);
            }
            catch (e) {
              deferred.reject(e);
            }
          });
      }

      servicePromises[url] = deferred.promise();
      return servicePromises[url];
    }
  }
};

function makeUrl(url, context) {
  return template(url, context, encodeURIComponent);
}

function template(tmpl, context, filter) {
  return tmpl.replace(/\{([^\}]+)\}/g, function(m, key) {
    // If key don't exists in the context we should keep template tag as is
    return key in context ? (filter ? filter(context[key]) : context[key]) : m;
  });
}
Launch:

var url = 'http://habrahabr.ru';
$.each(services, function(service) {
  counters.fetch(service, url).done(function(number) {
    number = parseInt(number, 10);
    if (!number) number = 0;
    console.log(number);
  });
});

M
Melanitsky, 2012-05-02
@Melanitsky

I implemented this in 2 ways:
1) This is a node.js daemon that constantly pulled the facebook twitter and google+ contact API and pulled the number of likes. There are several disadvantages:
- counts approximately 50-70 items per minute. Which is rather slow
- VKontakte has a limit on the number of requests to API from one IP. And every few minutes the demon falls into a stupor. The limit is on the number of requests per 5 minutes.
2) The second option is to count them on the client. If the page is regularly viewed, then you can collect the amount by the client (yes, it is in the browser to collect the number of varnishes, everyone has a callback), and send the result to the server.
Cons:
- only works if pages are regularly viewed
- you can easily post any rubbish on a server =(

G
getnaked, 2012-05-03
@getnaked

There was Share Count JS. The official site has been shut down, but a copy can still be found. He could count Digg, Twitter, Facebook and Delicious.
image

P
Platon Fedorovich, 2015-09-30
@platon_fedorovich

Pay attention to this project (posted on github, open source): https://github.com/enjoyiacm/goodshare.js . It takes all the indicators of the counters from the social networks themselves. networks (via API), so the data is always up-to-date. If you know jQuery well, then you can easily add a function to it that will sum up the indicators from the counters of the social networks you need. networks and display a common counter.
Personally, I use them all the time due to the fact that many services (like AddThis or pluso) do not allow you to make a unique design for each button and take a long time to load (sometimes with errors). Well, of course, goodshare.js does not have any tracking and advertising links.

V
Vitaliy Petrychuk, 2012-05-02
@vermilion1

Each service separately must give the number of likes. Otherwise, nothing because of the crossdomain.
Facebook, for example - stackoverflow.com/questions/4877919/get-facebook-like-count-for-every-page-on-my-domain

S
Sergey Cherepanov, 2012-05-02
@fear86

Well, or send the bot on all pages, let him count :)

B
buxley, 2012-05-03
@buxley

Alternatively, you can use www.addthis.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question