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-twitterPost install, I got a warm fuzzy feeling executing:
import twitterooooh, 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.
api = twitter.Api()
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()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:
Traceback (most recent call last):
", line 1, in
File "build\bdist.win32\egg\twitter.py", line 1597, in GetFollowers
twitter.TwitterError: twitter.Api instance must be authenticated
api.SetCredentials('myusername','mypassword')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:
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)
I'll probably follow up this article at a later time with a more detailed look into it but hopefully you enjoyed the brief.