Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using tweepy to access Twitter's Streaming API

I'm currently having trouble getting example code for using tweepy to access Twitter's Streaming API to run correctly (err...or at least how I expect it to run). I'm using a recent clone of tweepy from GitHub (labeled version 1.9) and Python 2.7.1.

I've tried example code from three sources, in each case using "twitter" as a test term for tracking:

  1. O'Rilley Answers code: How to Capture Tweets in Real-time with Twitter's Streaming API

  2. Andrew Robinson's blog: Using Tweepy to access the Twitter Stream

  3. Tweepy examples repository on GitHub (which, as Andrew Robinson has done, can be easily modified to support OAuth authentication): streamwatcher.py

In all three cases I get the same result: Authentication is successful, no errors are produced, and the main program loop seems to be executing w/o any problems. I see network usage jump to about 200KB/s, and the python process jumps to near 100% CPU usage, so I think data is being received. Nothing is output to the console, however.

I suspect that tweepy's Stream class is not calling the custom callback method for some reason. I've tried rewriting the callback methods in each example to produce output whenever they're called, which seems to confirm this. This is one very simple bit of test code based on Andrew Robinson's blog entry (with my app's keys removed, of course):

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

import tweepy

consumer_key = ''
consumer_secret = ''

access_token_key = ''
access_token_secret = ''

auth1 = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth1.set_access_token(access_token_key, access_token_secret)

class StreamListener(tweepy.StreamListener):
    def on_status(self, tweet):
        print 'Ran on_status'

    def on_error(self, status_code):
        print 'Error: ' + repr(status_code)
        return False

    def on_data(self, data):
        print 'Ok, this is actually running'


l = StreamListener()
streamer = tweepy.Stream(auth=auth1, listener=l)
#setTerms = ['hello', 'goodbye', 'goodnight', 'good morning']
setTerms = ['twitter']
streamer.filter(track = setTerms)

What am I doing wrong?

like image 614
Morris Cornell-Morgan Avatar asked Jun 10 '12 17:06

Morris Cornell-Morgan


1 Answers

I ran into this as well and fixed it on my local checkout by changing line 160 in streaming.py to

if delimited_string.strip().isdigit():

This seems to be a known issue/bug in Tweepy - should have checked the issues list before doing all that debugging :) -

https://github.com/tweepy/tweepy/pull/173 https://github.com/tweepy/tweepy/pull/182

like image 115
georgemp Avatar answered Nov 09 '22 04:11

georgemp