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 BowlingSo 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 http://code.google.com/p/python-twitter/issues/detail?id=20 friends.extend(self.GetFriends(page=((len(friends)/100)+1))) 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: try: self.CreateFriendship(name) except HTTPError, e: print “got error with code”,e.code # see http://code.google.com/p/python-twitter/issues/detail?id=33 if e.code == 401 or e.code == 403: data=simplejson.loads(e.read()) print “got data”, data if (hasattr(data,’error’)): print “can’t follow “, name, ” because “, err.error continue raise
if name == “__main__”: import sys import getpass u=sys.argv1 file=open(sys.argv2,’r’) p=getpass.getpass(“Twitter password: “) try: 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.” else: print ‘The server couldn't fulfill the request: code’,e.code /sourcecode
Kudos to Niklas Saers whose own py-twitter utility, lasttweet.py, 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.