The asynchronous nature of Twitter is one of its keys to success. No friend requests. Awesome.

A lot of the people I follow on Twitter have no business following me. I didn’t get upset when DHH, _why, and alexalbrecht did not reciprocate interest. Why would they? They don’t know me from Adam (even though I’m waayyy cooler than that dude…).

However, sometimes it is interesting to see all the jerks people who you follow that do not follow you back. There’s probably a web application out there that does this, but who needs a web app when this is a perfectly good excuse to play with Ruby?

The twitter gem by John Nunemaker makes this task so easy it’s retarded. First, get the gem if you don’t already have it:

jerod@mbp:~$ gem install twitter

Now. The script:

require 'rubygems'
require 'twitter'

auth   ='username', 'password')
base   =
guilty = base.friend_ids - base.follower_ids

puts "#{guilty.size} people you follow don't follow you back"

guilty.each do |user_id|
  user = base.user(user_id)
  puts "#{} follows #{user.friends_count}" +
       " and has #{user.followers_count} followers."

Isn’t that simple? Are you with me on the retarded comment now? How it works:

The friend_ids and follower_ids methods each return an array of ids and we subtract the followers array from the friends array. All that is left over after subtraction are the ids of users in your friends list (people you follow) that are not in your followers list. Then we loop over the new array of “guilty” parties and fetch their user account information based on their user_id, printing the details each time.

Just replace username and password with your own information and you’re off to the races. Run the script from your command line and you should see output similar to this:


NOTE: Twitter only allows (currently) 100 API requests per hour. Each user account lookup requires an API request, so if your “guilty” array is quite large, you’ll probably get an error before the script terminates (Maybe its time to un-follow a few peeps!).

What other cool tricks can we do using the Twitter gem?