Friday, July 31, 2009

python-twitter, why not?

Why Not?

I have recently started to experience Facebook and twitter, living the life of a truly 'connected' geek. But as you all could have guessed, being connected isn't all that interesting of an idea to me. In the end, what matters the most is the Code. How can I interact with twitter? There is a plethora of libraries available for interacting with Twitter. I heart Python and decided on hacking at python-twitter. The Scripting with Curl article linked off of Twitter was rather informative and provided me a reasonably good amount of background. If interested, most everything done in the article can be replicated with urllib2 in Python, and that's good to know. :-) I needed to install python-twitter and had the choice for installing from source or using easy_install. I'm on windows at the moment and have become use to using easy_install whenever possible, so I simply executed:
easy_install python-twitter
Post install, I got a warm fuzzy feeling executing:
import twitter
api = twitter.Api()
ooooh, ahhhh, I already feel more connected..... I used the api reference here as my introduction to the api. I usually like to toy a bit at the interactive interpreter to see what types of methods / attributes are on certain objects and see if I can figure things out on my own. First thing I noticed was there is a plethora of help available: help(twitter.Api), help(api.GetDirectMessage), etc... That was a +. I also noticed that upon executing api.GetDirectMessages() out of line, I received a good error message with a human-readible description. Yet another huge +. I like to look for things like this from the start, to see how reasonably the API is.

Getting my list of Followers

I decided to attack a simple problem: get my list of followers. I signed up to twitter around Aug 2008 but never used it or told anyone I had an account. I only started using it seriously the last week. So, if I try to do GetFollowers just to see what happens and...

>>> api.GetFollowers()
Traceback (most recent call last):
File "", line 1, in
File "build\bdist.win32\egg\", line 1597, in GetFollowers
twitter.TwitterError: twitter.Api instance must be authenticated
Woot! Another good error message. So, let's get authenticated. Upon browsing dir(api), I found a SetCredentials method, which sounds like what I was searching for. The help provided a simple and straight forward explanation of the call:
All there is to it, no hackin at headers required. Can I get my followers now?
>>> len(api.GetFollowers())
I only have a few followers at this point in time, perhaps you can be another? It was far too easy to do this, yes/ no? Notice above I was getting the length of the list returned by GetFollowers. This call returns a List of twitter.User objects that allows you to obtain further details on the user of interest. To get the screen names of all my followers, all that needs to be done is the following:

followers = api.Followers()
for follower in followers:
print follower.screen_name

And the listing is provided. The are several functions available in the api that simplify interacting with twitter. You can trivially:
  • get different user status
  • get your direct messages
  • get replies
  • get a friends timeline
  • post a message
  • post an update (or updates)
It's pretty slick. Yet another case of Python, along with a nice API making my life simpler.

I'll probably follow up this article at a later time with a more detailed look into it but hopefully you enjoyed the brief.

1 comment:

Ranganath said...

Hi, Some one from facebook refereed your link i have book marked it nice blogs you write see Free  how to grow twitter followers here