See Which Twitterers Don’t Follow You Back In Less Than 14 Lines of Python
UPDATED (1/8/10): See Which Twitterers Don’t Follow You Back (updated)
All right, so I totally stole this from See Which Twitterers Don’t Follow You Back In Less Than 15 Lines of Ruby. Basically, if you have some bizarre desire to see who doesn’t reciprocate your follows on Twitter, or you just like to feel unpopular, I have the script for you.
I have no idea what practical application this might have, but I’m going to be using Twitter for a development project and I thought this would be a nice way to get my feet wet with the API. Also, everyone loves a good Python vs Ruby shootout :)
There’s a couple of Twitter API clients written in python, but python-twitter seems to be the most popular. To install just do
$ easy_install python-twitter
or check out the subversion repository if you want the latest source release. To be honest, I was a little disappointed that it lacked some basic features, like having the friend and follower count be attributes of a Twitter user object. If the Rubyists can have them with twitter gem, why can’t we? Anyway, here’s the code:
import twitter, sys, getpass, os
def call_api(username,password):
api = twitter.Api(username,password)
friends = api.GetFriends()
followers = api.GetFollowers()
heathens = filter(lambda x: x not in followers,friends)
print "There are %i people you follow who do not follow you:" % len(heathens)
for heathen in heathens:
print heathen.screen_name
if __name__ == "__main__":
password = getpass.getpass()
call_api(sys.argv[1], password)
You can see that I added some convenience functions so you can run this script from the command line. If you took these out you could get it down to 8 lines, but whatever. I’m not a big fan of leaving my passwords lying around in plain text files, so I use the getpass module to hide my password as I type it into the command line. To run the script, just do
$ python twit_heathens.py <twitter-username>
Running the program looks like this:
$ python twit_heathens.py david_ziegler Password: There are 9 people you follow who do not follow you: leahculver boxee boxee_bd jeresig sunlightlabs venturehacks djangolinks lushwhip thefo0
And bam, you can see all the people who are too cool to follow you on Twitter. If you want, you can download the script here: http://filer.case.edu/~dez4/twit_heathens.py