Mass/Bulk Follow with py-twitter

Mar 30, 2009

While trying to maintain the Greek Liberals twitter account, specifically trying to follow a bunch of people that I also follow, I didn't manage to find a satisfactory solution to automatically follow a specific list of screen names.

Zac Bowling's bulk following tool wasn't working for me (I get an authentication error). The only other promising solution is Flashtweet but they insist on loading the list of new people to follow from another twitter account's follow list. At first that might not seem like a problem, since I could simply load my personal twitter account friends in Flashtweet and check the people I wanted to follow. But I already had a hand-picked list of people that I wanted the GreekLiberals twitter account to follow. I created this list by going through the CSV file that Tweeple exports where I can browse all the details of a user instead of just the twitter name like in Flashtweet's interface. I don't want to start mindlessly following people who for starters might not even speak greek or be otherwise interested in dialogue with the Greek Liberals.

So I decided to write a py-twitter script to do the importing. Here it is:
[sourcecode language='python']
import twitter
import simplejson
from urllib2 import HTTPError, URLError

class MassFollower(twitter.Api):
''' Follow a list of people '''

def AllFriends(self):
friends = []
while (not len(friends) % 100) :
# see
return friends

def MassFollow(self,names):
for name in set(names)-set([f.screen_name for f in self.AllFriends()]):
if not self.GetUser(name).protected:
except HTTPError, e:
print "got error with code",e.code
# see
if e.code == 401 or e.code == 403:
print "got data", data
if (hasattr(data,'error')):
print "can't follow ", name, " because ", err.error

if __name__ == "__main__":
import sys
import getpass
p=getpass.getpass("Twitter password: ")
api = MassFollower(username=u,password=p)
api.MassFollow([ line.strip() for line in file ])
except IOError, e:
if hasattr(e, 'reason'):
print "Can't contact twitter:", e.reason
elif hasattr(e, 'code'):
if e.code==400:
print "Probably struck rate limit, try again later."
print 'The server couldn\'t fulfill the request: code',e.code

Kudos to Niklas Saers whose own py-twitter utility,, tipped me off to the page parameter argument to GetFriends method. At the time of this writing, an observant fellow will notice that, according to the aforelinked docs, there is no parameter named page. This is why this script is currently dependent on a version of py-twitter greater than 0.5, which is currently the stable version. So basically you need to get a svn checkout and install it manually on your system.

Apart from being my first use of the twitter API it is also the first semi-usefull thing I've written in python, so I would be gratefull for criticism or comments regarding the code above.