jmhobbs

Streaming Tweets With Tweepy

I've been meaning to check out the Tweepy for a while and got around to it today. It's a Python library for interacting with Twitter. The feature I'm most interested in is the streaming API support, which isn't advertised much by Tweepy but seems pretty solid.

Tweepy has pretty good documentation, and the code is terse and readable, but what I found most useful was the examples repository, which had the only example of streaming with Tweepy that I could find in the official documentation.

It's really straightforward. Implement a tweepy.streaming.StreamListener to consume data, set up a tweepy.streaming.Stream with that listener, then pull the trigger on the streaming function you want to use.

Here's a quick example I set up to track the filter keyword "omaha".

# -*- coding: utf-8 -*-

from tweepy.streaming import StreamListener, Stream

class Listener ( StreamListener ):
  def on_status( self, status ):
    print '-' * 20
    print status.text
    return

if __name__ == "__main__":

  USERNAME = "YourUsernameHere"
  PASSWORD = "YourPasswordHere"

  listener = Listener()
  stream = Stream(
    USERNAME,
    PASSWORD,
    listener
  );

  stream.filter( track=( "omaha", ) )