Skip to content

Instantly share code, notes, and snippets.

@dunn
Last active August 29, 2015 14:02
Show Gist options
  • Save dunn/cc016928ce7f7e78ae29 to your computer and use it in GitHub Desktop.
Save dunn/cc016928ce7f7e78ae29 to your computer and use it in GitHub Desktop.
use node and twit to look at relationships between twitterers
#!/usr/bin/env node
var Twit = require('twit');
var async = require('async');
var T = new Twit({
consumer_key: '😈',
consumer_secret: '😈',
access_token: '😈',
access_token_secret: '😈'
});
var args = process.argv;
if (args.length < 4) {
console.error('Error [mutual]: Bad # of arguments');
console.log("Usage: `mutual darth '<>' duckthecat [--simple]`");
}
else {
var simple = !!args[5];
// "darth '<<' duckthecat" β€” darth's followers that duck follows
// "darth '<>' duckthecat" β€” darth's followers that follow duck
// "darth '>>' duckthecat" β€” duck's followers that darth follows
// "darth '><' duckthecat" β€” people that darth follows that duck follows
var user1 = (args[3].charAt(0) === '>' ? 'friends' : 'followers');
var user2 = (args[3].charAt(1) === '<' ? 'friends' : 'followers');
async.parallel([
function(callback){
var user1opts = { screen_name: args[2], count: 5000 };
T.get(user1 + '/ids', user1opts, function(err,data,response) {
if (err) return callback(err);
if (!simple){
console.log('remaining ' +
user1 + ' requests: ' +
response.headers['x-rate-limit-remaining']);
}
var user1Ids = data.ids;
var cursor = data.next_cursor_str;
async.whilst(
function(){ return cursor !== '0'; },
function(callback){
var getUser1Opts = {
cursor: cursor,
screen_name: args[2],
count: 5000
};
T.get(user1 + '/ids', getUser1Opts, function(err, data, response) {
if (err) return callback(err);
user1Ids = user1Ids.concat(data.ids);
cursor = data.next_cursor_str;
return callback(null);
});
},
function(err) {
if (err) { return callback(err); }
return callback(null, user1Ids);
});
});
},
function(callback){
var user2opts = { screen_name: args[4], count: 5000 };
T.get(user2 + '/ids', user2opts, function(err,data,response) {
if (err) return callback(err);
if (!simple){
console.log('remaining ' +
user2 + ' requests: ' +
response.headers['x-rate-limit-remaining']);
}
var user2Ids = data.ids;
var cursor = data.next_cursor_str;
async.whilst(
function(){ return cursor !== '0'; },
function(callback){
var getUser2Opts = {
cursor: cursor,
screen_name: args[4],
count: 5000
};
T.get(user2 + '/ids', getUser2Opts, function(err,data,response) {
if (err) return callback(err);
user2Ids = user2Ids.concat(data.ids);
cursor = data.next_cursor_str;
return callback(null);
});
},
function(err) {
if (err) return callback(err);
return callback(null, user2Ids);
});
});
}
],function(err, results){
if (err) {
return console.error(err);
}
var all = results[0].concat(results[1]);
// http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array
var sorted = all.sort();
var i = all.length;
var mutuals = [];
while (i--) {
if (sorted[i + 1] === sorted[i]) {
mutuals.push(sorted[i]);
}
}
if (!simple){
var n = mutuals.length;
var output1 = '@' + args[2];
output1 += (user1 === 'friends' ?
' is following' : ' is followed by');
var output2 = '@' + args[4];
output2 += (user2 === 'friends' ?
' is following' : ' is followed by');
console.log(output1 + ' ' + n + ' people that ' + output2);
}
async.whilst(
function(){ return mutuals.length; },
function(callback){
var lookups = '';
var lookupLimit = (mutuals.length < 100 ?
mutuals.length : 100 );
while (lookupLimit--) {
lookups += ',' + mutuals.pop();
}
lookups = lookups.slice(1);
T.post('users/lookup',{ user_id: lookups }, function(err,data,res) {
if(err) {
console.error('users/lookup error:');
return callback(err);
}
var k = data.length;
while (k--) {
if (simple){
console.log(data[k].screen_name);
}
else {
console.log(data[k].name +
' (@' + data[k].screen_name + ')');
}
}
return callback(null);
});
},
function(err){
if (err) return console.error(err);
return true;
}
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment