Twitterで共通のfriends/followersを調べる

be careful on 150 req/h rate limit!

require 'rubygems'
require 'twitter_oauth'
require 'bdb'

def get_id(screen_name)
  user = @client.show(screen_name)
  if(user.key?('error'))
    p user
    exit
  end
  user['id']
end

def get_ids(method, id)
  case method
  when 'friends'
    ids = @client.friends_ids({:user_id => id})
  when 'followers'
    ids = @client.followers_ids({:user_id => id})
  end
  unless(ids.is_a?(Array))
    p ids
    exit
  end
  ids
end

def get_user(id)
  user = @client.show(id)
  if(user.key?('error'))
    p user
    exit
  end
  user
end

screen_name_a = ARGV[0]
screen_name_b = ARGV[1]

@client = TwitterOAuth::Client.new

id_a = get_id(screen_name_a)
id_b = get_id(screen_name_b)

friends_ids_a = get_ids('friends',id_a)
friends_ids_b = get_ids('friends',id_b)

followers_ids_a = get_ids('followers',id_a)
followers_ids_b = get_ids('followers',id_b)

common_friends_ids = friends_ids_a & friends_ids_b
common_followers_ids = followers_ids_a & followers_ids_b
common_mutal_ids = common_friends_ids & common_followers_ids

print "common friends   : #{common_friends_ids.length}\n"
p common_friends_ids
print "common followers : #{common_followers_ids.length}\n"
p common_followers_ids
print "common mutal     : #{common_mutal_ids.length}\n"
p common_mutal_ids

@twitter_user = BDB::Hash.create('twitter_user.db',nil,BDB::CREATE)

(common_friends_ids | common_followers_ids).each do |id|
  if @twitter_user.key?(id)
    user = JSON.parse(@twitter_user[id])
  else
    user = get_user(id)
    @twitter_user[id] = user.to_json
  end
  print "#{user['id']} #{user['screen_name']} #{user['name']}\n"
end

@twitter_user.close()

twitter_oatuh install howto:

gem sources -a http://gems.github.com
sudo gem install moomerman-twitter_oauth