Created
February 8, 2014 04:12
-
-
Save Syfaro/8876598 to your computer and use it in GitHub Desktop.
A simple node.js application to choose a random mutual follower
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Twit = require 'twit' | |
async = require 'async' | |
fs = require 'fs' | |
shuffle = require('knuth-shuffle').knuthShuffle | |
T = new Twit { | |
consumer_key: '' | |
consumer_secret: '' | |
access_token: '' | |
access_token_secret: '' | |
} | |
Array::chunk = (n) -> | |
if !this.length | |
return [] | |
return [ this.slice 0, n ].concat this.slice(n).chunk(n) | |
getIDs = (err, reply, callback) -> | |
callback err, reply.ids | |
getFollowing = (callback) -> | |
T.get 'friends/ids', count: 5000, (err, reply) -> | |
getIDs err, reply, callback | |
getFollowers = (callback) -> | |
T.get 'followers/ids', count: 5000, (err, reply) -> | |
getIDs err, reply, callback | |
handleResults = (err, results) -> | |
mutual = results[0].filter (n) -> | |
return results[1].indexOf(n) != -1 | |
mutualFollowers = [] | |
handleChunk = (err, reply, callback) -> | |
for user in reply | |
mutualFollowers.push user | |
callback err | |
getChunk = (item, callback) -> | |
T.post 'users/lookup', user_id: item.join(','), (err, reply) -> | |
handleChunk err, reply, callback | |
getRandomUser = -> | |
output = [] | |
for user in mutualFollowers | |
output.push user.screen_name | |
console.log shuffle(output.slice(0))[Math.floor(Math.random())*output.length] | |
async.eachLimit mutual.chunk(100), 5, getChunk, getRandomUser | |
async.series [ | |
getFollowers | |
getFollowing | |
], handleResults |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment