Twitter Streaming API with Python

Sunday, January 3rd, 2010

Tags: ,

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! :)

3 Responses to “Twitter Streaming API with Python”

  1. [...] This post was mentioned on Twitter by python ism, Abu Ashraf Masnun. Abu Ashraf Masnun said: Twitter Streaming API with Python: http://bit.ly/4nPlv4 #Python #Twitter [...]

  2. [...] the rest here: Twitter Streaming API with Python « maSnun.com By admin | category: python | tags: api, installed-before, make-sure, module-which, [...]

  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

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