25
Dec 08
Experiments with Twitter, who are your mutual followers

A week ago, I suddently got curious about a recent drop in my followers count on Twitter which, I didn't know about, had been because of a bot removal action. Anyway, I came to thought about how many of my contacts were following me back. Althought there are plenty of sites that tell you that, I could name none by the time (check Lessfriends.com or FriendOrFollow), so I thought it would be just faster to write my own thing to do it.

Here's the result, just feed username and password with yours and it should work.

Note: if you've over 100 followers the script might not work due to Twitter's API restriction of 100 requests per hour. If that is the case, just use the web alternatives, this is mostly educational.

 

require 'net/https'
require "rexml/document"

username = ""
password = ""
friendsxml = ""

friends = Array.new
friendlist = Array.new
nonlist = Array.new

http = Net::HTTP.new('twitter.com', 443)
http.use_ssl = true
http.start do |http|
    request = Net::HTTP::Get.new('/statuses/friends/' + username + '.xml')
    request.basic_auth username, password
    response = http.request(request)
    response.value
    friendsxml = response.body
end

doc = REXML::Document.new(friendsxml)
doc.elements.each("*/user/screen_name") {
  |element| friends << element.text
}

friends.each do |f|
  isfriend = ""
  http.start do |http|
      request = Net::HTTP::Get.new("/friendships/exists.xml?user_a=" + f + "&user_b="+ username)
      request.basic_auth username, password
      response = http.request(request)
      response.value
      isfriend = response.body
  end
 
  if isfriend == "<friends>true</friends>"
    friendlist << f
  else
    nonlist << f
  end
end


puts ":::::::Your true friends:::::::"
friendlist.each do |f|
  puts f
end
puts "Total: "
puts friendlist.size

puts "::::::::::Non friends::::::::::"
nonlist.each do |f|
  puts f
end
puts "Total: "
puts nonlist.size


Go to Blog

Comments

Add your thoughts about it!