Skip to content

Instantly share code, notes, and snippets.

@recyclerobot
Last active August 29, 2015 13:56
Show Gist options
  • Save recyclerobot/9277513 to your computer and use it in GitHub Desktop.
Save recyclerobot/9277513 to your computer and use it in GitHub Desktop.
Twitter followers command line using PhantomJS
var usernames = [],
results = {},
page = require('webpage').create(),
system = require('system');
function init(){
if (system.args.length === 1) {
console.log('Pass one or more twitter usernames as arguments');
} else {
system.args.forEach(function (arg, i) {
if(i!==0) usernames.push(arg);
});
next();
}
}
function open_twitter_with_user(user){
page.open('https://twitter.com/' + user, function (status) {
if (status === 'success') {
setTimeout(function(){
var followers = page.evaluate(function () {
return document.querySelectorAll("[data-element-term='follower_stats'] strong")[0].innerText;
});
results[user] = followers;
next();
},200);
}else{
console.log("something went wrong");
next();
}
});
}
function next(){
if(usernames.length > 0){
open_twitter_with_user(usernames[0]);
usernames.shift();
}else{
final_result();
page.close();
phantom.exit();
}
}
function final_result(){
console.log(JSON.stringify(results));
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment