-
-
Save mccraigmccraig/140081 to your computer and use it in GitHub Desktop.
ruby : find twitter common followers
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
#!/usr/bin/env ruby | |
# Trivial little twitter script | |
# Pass it a list of names as command line arguments | |
# and it will return a list of all people who follow everyone in the | |
# list. | |
# A person is implicitly assumed to follow themselves, so if foo follows | |
# bar and bar follows foo then common_followers foo bar would include | |
# both foo and bar in the output, but if bar did not follow foo it | |
# would include neither. | |
require "rubygems" | |
require "httparty" | |
class Twitter | |
include HTTParty | |
base_uri "http://twitter.com" | |
format :json | |
def self.follower_ids(user) | |
get "/followers/ids/#{user}.json" | |
end | |
def self.username_for_id(user) | |
details(user)["screen_name"] | |
end | |
def self.id_for_username(user) | |
details(user)["id"] | |
end | |
def self.details(user) | |
(get "/users/show.json", :query => { :id => user }) | |
end | |
end | |
require "set" | |
intersection = nil; | |
if __FILE__ == $0 | |
ARGV.each do |user| | |
followers = Set[*Twitter.follower_ids(user)] | |
id = Twitter.id_for_username(user) | |
followers.add id | |
if !intersection | |
intersection = followers | |
else | |
intersection &= followers | |
end | |
end | |
intersection.each do |user| | |
puts Twitter.username_for_id(user) | |
end | |
end |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment