Saturday, November 20, 2010

A Python script using Tornado web server with the Twitter Streaming API

So today I decided to play with Tornado the open source version of a scalable, non-blocking web server which powers FriendFeed. While doing so I discovered it will probably be cool to play with the Twitter streaming API as well and write a Python script that uses both.

Here's the result of my experiment:

from tornado.httpclient import HTTPClient, HTTPRequest
import simplejson

def handle_tweet_stream(response):
    try:
        # Print the parsed twitter stream.
        print simplejson.loads(response)
    except ValueError:
        return
    print

if __name__ == '__main__':
    req = HTTPRequest(
        "http://stream.twitter.com/1/statuses/filter.json", 
        body='track=google',
        method = "POST",
        auth_username = "username",
        auth_password = "xxxxxxx",
        streaming_callback=handle_tweet_stream)

    client = HTTPClient()
    client.fetch(req)