Thursday, May 14, 2009

Awesome Python Twitter Library: See Which Twitter users don’t Follow You Back In Less than 10 lines of Python

Mike Verdon’s Python Twitter Tools is an awesome twitter library. It is less than 100 lines of code, and is very well commented. It uses the dynamic language features and creates the methods on the fly and returns the python dictionary.

The best part: It is dynamic and creates methods on the fly. So there is no need for any documentation beyond the standard twitter API documentation pbwiki. Use the url patterns after `/` as the object parameters by separating them by a `.`.

There is a global constants file that has a list of all methods that must be a POST and it makes post requests for those and get requests for the rest. Infact the author has even written a documentation scrapper that parses the twitter documentation to find out which methods are POST.*

Using it is very simple and pretty straightforward.

#Instantiation: from twitter import Twitter
t = Twitter()
#Public Timeline?

Jerod Santo showed how using a ruby twitter gem library, he was able to find the people not following you back in twitter in less than 15 lines of code. A perl guy demonstrated to do the same in the Zen Perl library.

So how many lines of code would it take using the library that I think is the best across all languages. Lets try out.
Finding Friends ids? From the twitter API wiki:


Thus, our method for finding the friends of a given user will be


and thus the entire code to print all those the user follows but who is inturn not followed back is as follows:

import wrapper
ts = wrapper.Twitter()
user = 'scorpion032'
diff= set(ts.friends.ids(screen_name=user)) - set(ts.followers.ids(screen_name=user))
for i in diff:
 try: non_fol1 =
 except: continue
 print "%s with %d followers and %d following" %(non_fol1['name'],non_fol1['followers_count'],non_fol1['friends_count'])

*However this auto updating of POST functions is yet to be updated as the documentation format got changed, recently after @dougw joined.
blog comments powered by Disqus