Skip to content

Instantly share code, notes, and snippets.

@seratch
Forked from yusuke/common_followers.rb
Created May 2, 2012 15:58
Show Gist options
  • Save seratch/2577754 to your computer and use it in GitHub Desktop.
Save seratch/2577754 to your computer and use it in GitHub Desktop.
print common follower screen names of specified twitter accounts
#!/usr/bin/env ruby
require "net/https"
require "uri"
API_BASE_URL = "https://api.twitter.com/1"
def get(url)
uri = URI.parse(url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = uri.scheme == "https"
https.start { |conn| conn.get(url).body }
end
def follower_ids(screen_name)
# fetches up to 5000 followers' ids
response = get(API_BASE_URL + "/followers/ids.json?screen_name=" + screen_name)
/\[(.*)\]/.match(response)[1].split(",")
end
def intersect_followers(screen_names, common_followers)
screen_name = screen_names.shift
if screen_name.nil?
common_followers
else
intersect_followers(
screen_names,
common_followers & follower_ids(screen_name)
)
end
end
# main
screen_names = ARGV
common_followers = intersect_followers(
screen_names,
follower_ids(screen_names.shift)
)
# assuming the specified two have only up to 100 common followers
users = get(API_BASE_URL + "/users/lookup.json?user_id=" + common_followers.join(","))
users.scan(/(?:"screen_name":")([a-zA-Z0-9_]*)(?:")/).each do |screen_name|
puts "@#{screen_name.first}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment