A simple Ruby script to follow all your Twitter followers

October 30, 2009

Personally I don’t think there’s much sense to following 30,000 people and I’ve been enjoying Twitter a lot more as I’ve pared down on the number of people I follow. But this little script is just a gesture of thanks to Jay Electronica for making great music over the past three years and for spitting that burnt mouth Isaiah type prophesy at Governor’s Island this summer.

I’ve seen Jay Elec on Twitter talking about how he can’t find an automated solution for following all 30,000 of his current followers. There are tools for auto-following people back when they follow you but those are only for new followers. Twitter Karma has worked well for my Bulk UNfollowing needs but apparently it chokes when loading up tens of thousands of followers.

Jay being the worldly fellow that he is, already tried his hand at this Python script and it didn’t work for him. I haven’t ever messed with Python so I thought I’d try doing this in Ruby.

This script on About.com didn’t work but I was able to use it as a starting point.

Step 1: (if you don’t have Ruby set up)
From here, install Ruby and Rubygems. You won’t need Rails since this isn’t a web app. But it’s pretty cool to play with for afterwards.

Step 2:
At the command prompt gem install twitter for access to the Twitter API

Step 3:
Save the code below into a file called follow.rb

Step 4:
At the command prompt ruby follow.rb

Code:

require 'rubygems'
gem 'twitter'
require 'twitter'

# replace username and password with your own.
auth   = Twitter::HTTPAuth.new('username', 'password')
twitter   = Twitter::Base.new(auth)

# throw your unfollowed followers into a bucket.
to_follow = twitter.follower_ids - twitter.friend_ids

# show count of how many people you want to auto-follow
puts "There are #{to_follow.size} people to follow"

# follow them
to_follow.each do|f|
  user = twitter.user(f)
  puts "#{user.name} - http://twitter.com/#{user.screen_name}"
  twitter.friendship_create user.screen_name
  # Sleep 60 seconds to respect twitter api rules
  sleep 60
end

Caveat 1: Because it pauses for a minute between each follow, this will take a crazy long amount of time for Jay Electronica since he has 30,000 followers. Like it will need to run for weeks. But it can run in the background and I don’t see a better alternative for this given Twitter’s API rules with a max of 150 requests per hour. Each follow is actually making 2 requests to Twitter – 1 to get the user info and 1 to make the follow.

Caveat 2: I tested this with an account that had 5 followers. It should work for any amount but I didn’t test it for one with hundreds, let alone tens of thousands. So let me know if it works for you.

Thanks to Ann, Asante, Karuna and Rob for helping me test the script.

{ 1 comment… read it below or add one }

Hav November 2, 2009 at 12:23 pm

Thanks Rafi,
Great work! I’m looking forward to trying this out. Do you know of any tools that will unfollow people based on keywords they say? Perhaps someone speaks about “lil wayne”, or mentions “soulja boy” too frequently for my tastes. I use socialtoo for auto following, though I end up spending way too much time unfollowing and marking people as spam.

Peace!

Hav @ FH

Leave a Comment

Previous post:

Next post: