Twitter Streaming API with Python

Hasin Vai has a good post about collecting tweets in real time with php. Here’s the post: http://hasin.wordpress.com/2009/06/20/collecting-data-from-streaming-api-in-twitter/ :) If you are a php developer look into there.

Well, I am going to cover Python for the Twitter streaming API. You need the “tweetstream” python module which depends on the “anyjson” module. Please make sure you have these modules installed before you proceed.

Download Links: anyjson | tweetstream

To install these packages, use the built in setup.py of the individual packages :

sudo python setup.py install

In my case, I didn’t have the “setuptools” python module installed with my default python installation. So I had to install it to get the setup.py work:

sudo apt-get install python-setuptools

I love the magic of debian packages :)

Now, you can install the modules and dive into our example:

 
#! /usr/bin/env python
 
import tweetstream
 
stream = tweetstream.TweetStream("twitter_username", "twitter_password")
 
for tweet in stream:
    if tweet.has_key("text"):
        print tweet['user']['screen_name'] + ": " + tweet['text'] , "\n"
        print "-----" * 4

That’s it! In real, you get the stream dictionary which you can use to manipulate the tweets.

The “tweetstream” module also lets you access the Track and Follow APIs in a superb fashion!

I love Twitter, I love Python! :)

This entry was posted in Blog Post and tagged , . Bookmark the permalink.

3 Responses to Twitter Streaming API with Python

  1. Pingback: Tweets that mention Twitter Streaming API with Python « maSnun.com -- Topsy.com

  2. Pingback: Twitter Streaming API with Python « maSnun.com MoinMoin Wiki

  3. Hi that is a genuinely interesting view, It does give one food for thought, I am very delighted I stumbled on your blog, i was using Stumbleupon at the time, in any case i don?t want to ramble on too much, but i would like to say that I will be back when I have a little time to read your blog more thoroughly, Once again thanks a lot for the blog post and please do keep up the right work,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">