(home)

Learning About Twitter API (with Python)

Summary

Documents my notes on the process of accessing the Twitter API with Python.

Library selection

The Twitter wiki has a Twitter API Python library list.

I think I started with this Twittering with Python presentation/comparison (which includes a comparison table) but ended up choosing tweepy based pretty much on features and that it didn't annoy me while I learned to use it (so far :) ).

One nice thing about the Twitter API is that you don't require an API key to get started. It's only if the functions you use need to be logged in, for example.

Installation of tweepy can be as simple as:

easy_install tweepy
# Also required if you have Python < 2.6 (dependency not specified?):
easy_install simplejson

Example usage to retrieve most recent tweets for an individual:

import tweepy
result = tweepy.api.user_timeline("eV")
print result

which should result in something like:

[<tweepy.models.Status object at 0x50a6d0>, <tweepy.models.Status object at 0x50a6f0>,
<tweepy.models.Status object at 0x52db50>, ..., <tweepy.models.Status object at 0x535170>]

or, more usefully, try something like:

for status in result:
    print status.text

For the second page of results use:

results2 = tweepy.api.user_timeline("myelin", page=2)

You can also specify how many results per page (this seems to have much higher latency if you go for the maximum value of 200):

response = tweepy.api.user_timeline(username, count=200)

Links

Project log

( 15 September 2009 )

code@rancidbacon.com