Skip to content

Instantly share code, notes, and snippets.

@kylehotchkiss
Created November 19, 2013 04:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylehotchkiss/7540207 to your computer and use it in GitHub Desktop.
Save kylehotchkiss/7540207 to your computer and use it in GitHub Desktop.
A crude Instagram hashtag like calculator. Theoretically can grab up to 100,000 pics of likes (an hour), given Instagram's API throttling. It's not 100% reflective of all images in a hashtag, Instagram doesn't return full data sets. But it can report averages and project a total, if that's useful to ya. Get your own access key and try it out.
var request = require("request");
var moment = require("moment");
var access_token = '';
var getTotalLikes = function( hashtag, callback ) {
var realCount;
var totalLikes = 0;
var totalImages = 0;
var totalRequests = 0;
var lookup = {};
var getPage = function getPage( hashtag, page, callback ) {
var endpoint;
if ( page ) {
endpoint = page;
} else {
endpoint = 'https://api.instagram.com/v1/tags/' + hashtag + '/media/recent?access_token=' + access_token;
}
request.get({
url: endpoint,
json: true
}, function( error, response, body ) {
if ( error ) {
callback();
} else {
totalRequests++;
var createdTime;
for ( var i in body.data ) {
var image = body.data[i];
totalImages++;
totalLikes += image.likes.count;
createdTime = image.created_time;
}
var ago = moment.unix( parseInt(createdTime) ).fromNow();
if ( !body.pagination.next_url ) {
callback();
} else {
getPage( hashtag, body.pagination.next_url, callback );
}
}
});
}
request({
url: 'https://api.instagram.com/v1/tags/' + hashtag + '?access_token=' + access_token,
json: true
}, function( error, response, body ) {
realCount = body.data.media_count;
getPage( hashtag, false, function() {
callback( totalLikes, totalImages, totalRequests, realCount );
});
});
};
getTotalLikes("funeralselfie", function( likes, images, requests, realCount ) {
var percentage = ( images / realCount ) * 100;
var average = likes / images;
var projected = realCount * average;
console.log("Done - " + images + " images; >" + likes + " likes; " + requests + " requests.")
console.log( "Sample size: " + percentage + "%; Average likes: " + average + "; Projected Total: " + projected );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment