Finding Mutual Follows
gga
#2009-01-19
When you’re a Twitter’er you will often be in a situation where someone follows you, and you’re wondering, ‘Who is this person? Do I know them?’ Well, I can’t answer that question for you. But, I have found that one thing that tells you about your new follower is who they follow that you also follow. Follow?
I want to be able to ask the question ‘Who do we know in common?’, in
short. A useful question, but one that can take quite a while to
answer using the web site. I
asked the lazy
twitterverse if there was already an app for this, but my twitterverse is too small to get an answer. So, I
wrote my own script. I don’t have any handy web space to run this
from, so you’ll have to grab it and run it yourself. You will need to
install the twitter4r
gem first:
sudo gem install twitter4r
Then paste the following code into a Ruby file, and run. It takes two parameters, the names of the two users for who you want to find common ground.
require 'rubygems'
require 'open-uri'
require 'rexml/document'
require 'twitter'
class Twitter::User
def all_friends
users = friends.map { |f| f.screen_name }
# If there's more than one page of users, we've already got the
# first one
page = 2
found_users = friends.length
while found_users >= 100
found_users = 0
open("http://twitter.com/statuses/friends/#{screen_name}.xml?page=#{page}") do |f|
users_doc = REXML::Document.new(f.readlines.join(''))
users_doc.elements.each('/users/user/screen_name') do |friend_name|
users << friend_name.text
found_users += 1
end
end
page += 1
end
users
end
end
def in_common(my_friends, other_friends)
my_friends.select { |m_n| m_n if other_friends.member? m_n }
end
def main(me, other)
c = Twitter::Client.new
me_friends = c.user(me).all_friends
other_friends = c.user(other).all_friends
in_common(me_friends, other_friends).each do |f|
puts " #{f}"
end
end
main(ARGV.shift, ARGV.shift)
Enjoy, and please let me know how it works out for you, or if you make any changes. And by the way, this is why RESTful APIs rock.