Sync your Twitter followers and friends

Posted on Wed Jun 10, 2009

I have a couple of accounts in Twitter (namely @debian and @planetalinux) that are starting to bring a lot of followers (well, at least some of them). And given that I consider these accounts to be Twitter-polite enough, I like to follow the followers back too; however, this task sometimes gets really hard and it’s tiring to go through the followers pages and follow those that I don’t follow yet over and over.

So, I spent a few minutes and came up with this simple Ruby script that uses John Nunemaker’s awesome Twitter gem.

require “rubygems” require “twitter”

httpauth = Twitter::HTTPAuth.new( ARGV[0] || ‘yehyeh’, ARGV[1] || ‘kissm3’ )

base = Twitter::Base.new(httpauth)

i = 0 (base.follower_ids - base.friend_ids).each do |id| i += 1 begin base.friendship_create id rescue Twitter::General => e puts “#{e.class}: #{e.message}” end end puts “#{i} new friendships.”

i = 0 (base.friend_ids - base.follower_ids).each do |id| i += 1 base.friendship_destroy id end puts “#{i} destroyed friendships.”

puts “#{base.friend_ids.size} friends now.” puts “#{base.follower_ids.size} followers now.”

What this little code does is exactly that, it will start following the followers you don’t follow yet, and it will stop following the people that don’t follow you back, right? Got it? It’s basically synchronizing your friends with your followers. As said, this is particularly helpful when you are maintaining a community account and want to keep up befriending your kind followers :-) Handling the exception Twitter::General on line 18 is only done because the twitter gem raises it even when you are trying to befriend an account to which you have already requested friendship (like pending requests to protected updates accounts) or those of suspended accounts (spammers).

comments powered by Disqus