Created
November 24, 2011 05:52
-
-
Save mattdsteele/1390711 to your computer and use it in GitHub Desktop.
Of the people I follow, who do they follow that I don't follow? (sorted by count of people I follow)
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
require 'rubygems' | |
require 'json' | |
user = ARGV[0] | |
if user == nil | |
puts "usage: who-should-i-follow.rb screen_name" | |
exit 1 | |
end | |
followers_to_check = 100 | |
def get_followers(screen_name) | |
JSON.parse `curl -s http://api.twitter.com/1/friends/ids.json?screen_name=#{screen_name}` | |
end | |
def followers_of(id) | |
JSON.parse `curl -s http://api.twitter.com/1/friends/ids.json?user_id=#{id}` | |
end | |
def get_user_names(ids) | |
JSON.parse `curl -s http://api.twitter.com/1/users/lookup.json?user_id=#{ids.join(",")}` | |
end | |
f = get_followers(user) | |
followers_of_followers = f['ids'].first(followers_to_check).map do |id| | |
followers_of(id)['ids'] | |
end | |
followers_of_followers.flatten! | |
followers_i_dont_follow = followers_of_followers - f['ids'] | |
sorted = followers_i_dont_follow.group_by {|i| i}.sort{|a,b| b[1].size <=> a[1].size} | |
hash_ids = {} | |
sorted[1..10].each {|i| hash_ids[i[0]] = i[1]} | |
user_names = get_user_names(hash_ids.keys).map {|user| [user['screen_name'],user['id']]} | |
puts "You should follow (since x of your users follow):" | |
user_names.each do |name| | |
puts "#{name[0]} (#{hash_ids[name[1]].size})" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gave a shout out to this gist here: http://blog.jerodsanto.net/2009/05/expand-your-twitter-network-in-less-than-15-lines-of-ruby/