Insights on Ruby, Git, jQuery, Cappuccino, WordPress, Debian and OS X. Please subscribe if you find something useful!

See Which Twitterers Don’t Follow You Back In Less Than 15 Lines of Ruby

Posted: May 12th, 2009 | Author: Jerod | Filed under: Ruby | Tags: , | Comments

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 did’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:

sudo gem install twitter

Now. The script:

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:

no_follows

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?


  • Great Read
  • nice script
    Love that
  • Jasperiel
    puts guilty.map { |user_id| user = base.user(user_id)
    "#{user.name} follows #{user.friends_count} and has #{user.followers_count} followers."}.join("\n")}

    Gain two lines by using the much underused "map" iterator.
    Why are most Rubyists neglecting "map" so much ? :(

    Gain one more if you don't declare "guilty" and inline (base.friend_ids - base.follower_ids) before the "map".
    But this would require to put the counting line after and using "_", which is quite ugly and less readable.
  • Thanks for the input! I do use map a lot but I wasn't going for absolute minimum LOC. It was more of a "look how easy it is" script, so brevity & readability were key.

    Still, I agree that map if very cool.
  • dziegler
    Very cool. My Python version was 14 ;)
    http://blog.davidziegler.net/post/107429458/see...
  • Nifty!
  • cool!
  • danielhepper
    Nice little script!

    Keep in mind that if you have lots of followers and friends (think aplusk), you will run out of API requests before you even fetched all ids.
  • I wanted to write a little script using this library that would sniff out people I ought to follow, based on who my friends follow. But the 100 API requests per hour is a bummer.
  • lowell
    you can ask to get white listed: http://twitter.com/help/request_whitelisting
  • That could be a fun project.

    What you could do is limit the API requests by sleeping the script for awhile so that you don't overuse the hourly limit. It wouldn't take too many requests to have a list of user_ids that all your friends follow. Then you could cross-reference them and identify accounts that overlap.
  • Nice! I'm relatively new to Ruby so this is a nice little treat. Thanks for posting this!
blog comments powered by Disqus