Monday, May 4, 2009

Twitter: Ruby Script to auto follow

A ruby program to auto follow users who follow you. In this you can define a back list as well ( users that you would never want to follow even if they follow you)
 

Pre-requisites for auto follow script:
  • Install Ruby 1.8.x
  • Install rubygems and twitter gem ( Tested with 0.6.8 version )

#!/usr/bin/ruby

require 'rubygems'
require 'twitter'

username = 'username'
password = 'urpassword'

#Add the twitter users that you don't want follow
never_follow = %w( spammy hawaii_hotels click4click fdgddf roOffers )

httpauth = Twitter::HTTPAuth.new(username, password)

twitter = Twitter::Base.new( httpauth)

# Query for the information, keep only the
# information we need.
filter = proc {|f| f.screen_name }

followers = []
friends = []

pageNo =1
begin
fol = twitter.followers(:page => pageNo).map(&filter)
puts "Got #{fol.length} followers in Page #{pageNo}"
followers += fol
pageNo+=1
end while fol.length > 0

puts "Total Followers: #{followers.length}"

pageNo =1
begin
fri = twitter.friends(:page => pageNo).map(&filter)
puts "Got #{fri.length} friends in Page #{pageNo}"
friends += fri
pageNo+=1
end while fri.length > 0

puts "Total Friends: #{friends.length}"

# Follow user, who follows you
# follow_list gives the list of twitter who follows you,
# but you are not following back

follow_list = followers - friends - never_follow
puts "No. of twitter profiles to autofollow #{follow_list.length}"
follow_list.each do |f|
puts " http://twitter.com/#{f}"
#print f," "
begin
twitter.friendship_create f
rescue
puts "Not able to follow: #{f}. Error: #{$!}"
end
# Sleep a few seconds, be nice to Twitter
sleep 5
end

No comments:

Post a Comment