Skip to content

Instantly share code, notes, and snippets.

@Twipped
Created June 16, 2012 03:31
Show Gist options
  • Select an option

  • Save Twipped/2939809 to your computer and use it in GitHub Desktop.

Select an option

Save Twipped/2939809 to your computer and use it in GitHub Desktop.
TwitterMostFollowed
#!/usr/bin/env node
var request = require('request');
var async = require('async');
var db = require('dirty')('followers.db');
var oauth = {
consumer_key: '',
consumer_secret: '',
token: '',
token_secret: ''
};
db.on('load', function () {
var getFollows = function (userid, callback) {
var stored = db.get(userid);
if (!!stored) {
callback(stored);
} else {
request({
url:'https://api.twitter.com/1/friends/ids.json?user_id='+userid+'&cursor=-1',
method:'GET',
json:true,
//oauth:oauth
}, function (error, response, body) {
if (!!body.ids) db.set(userid, body);
else console.log(body);
callback(body);
});
}
};
var followersCollection = {};
var stack = 0, idcount = 0;
getFollows(23116122 /* ChiperSoft */, function (body) {
async.forEach(
body.ids,
function loop (id, callback) {
getFollows(id, function (body) {
if (!!body.ids) {
body.ids.forEach(function (id) {
stack++;
if (!!followersCollection[id]) followersCollection[id]++;
else {
idcount++;
followersCollection[id] = 1;
}
});
}
callback();
});
},
function done (err) {
var results = [];
for (var key in followersCollection) {
if (followersCollection.hasOwnProperty(key)) {
if (followersCollection[key] > 2) {
results.push({
id:key,
count:followersCollection[key]
});
}
}
}
results.sort(function (a,b) {
return b.count - a.count;
});
results = results.slice(0,99);
var ids = results.map(function (o) {
return o.id;
});
results = [];
request({
url:'http://api.twitter.com/1/users/lookup.json?user_id='+ids.join(','),
method:'GET',
json:true,
//oauth:oauth
}, function (error, response, body) {
body.forEach(function (profile) {
results.push({
screenname:profile.screen_name,
name:profile.name,
count:followersCollection[profile.id],
followers: profile.followers_count
});
});
results.sort(function (a,b) {
return b.count - a.count;
});
console.log(results);
});
}
);
});
});
@Twipped

Twipped commented Jun 16, 2012

Copy link
Copy Markdown
Author

Needs to have the request, async and dirty libraries installed via npm.

Change the userid on line 42 to your own. It helps a lot if you fill in the oauth object details and uncomment lines 28 and 85, as that increases the twitter request cap from 150 to 350 requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment