Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tweepy streaming API: filtering on user

I'm trying to simply connect to the Twitter streaming API using tweepy (and python 3), and stream all tweets from a given single user.

I was under the impression this is possible, so I have the following simple code to do this:

from tweepy import StreamListener
from tweepy import Stream
import tweepy

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

class StdOutListener(StreamListener):

    def on_data(self, data):
        # process stream data here
        print(data)

    def on_error(self, status):
        print(status)

if __name__ == '__main__':
    listener = StdOutListener()
    twitterStream = Stream(auth, listener)
    twitterStream.filter(follow=['575930104'])

When I run this from the command line, I just get back a bunch of 406 codes from Twitter. Is there something very obviously wrong with how I am trying to use tweepy, or is the "follow" parameter not designed to do what I think it does?

EDIT: I've also posted this on the tweepy discussion boards, FYI.

like image 513
gogurt Avatar asked Feb 25 '15 01:02

gogurt


1 Answers

I was able to reproduce your problem on Python 3.4 using Tweepy 3.2.0. Upgrading to 3.3.0 solved the problem:

$ bin/pip install -U tweepy==3.2.0
[...]
Successfully installed tweepy requests-oauthlib six requests
Cleaning up...
$ bin/python test.py 
406
^CTraceback (most recent call last):
[...]
KeyboardInterrupt
$ bin/pip install -U tweepy==3.3.0
[...]
Successfully installed tweepy requests requests-oauthlib six
Cleaning up...
$ bin/python test.py
{"created_at":"Fri Feb 27 14:02:02 +0000 2015","id":571309283217768448,"id_str":"571309283217768448",
[...]

The Tweepy 3.3.0 Changelog mentions several streaming improvements, although I did not see the 'freezing' problem they mention.

like image 188
Martijn Pieters Avatar answered Sep 19 '22 03:09

Martijn Pieters