Question

I'm using twython (twitter API library for python) to connect to the streaming API, but I seem to only get the public twitter stream possibly filtered by words. Isn't there a way to get a real-time stream of the authenticated user timeline or @mentions?

I've been looping through delayed calls to the REST API to get those mentions but twitter doesn't like me to make so many requests.

Twython documentation isn't helping me much about it, neither is the official twitter doc.

If there's another python library that can work better than twython for streaming (for Twitter API v1.1). I'd appreciate the suggestion... Thanks.

Was it helpful?

Solution

In the beginning of my research I thought python-twitter is the twitter library for Python. But finally, it seems as if the Python Twitter Tools are more popular and support also twitter streaming.

It's a bit tricky, the streaming API and the REST api are not equal for direct messages. This small example script demonstrates how you can use the user stream to get direct messages:

import twitter # if this module does not 
               # contain OAuth or stream,
               # check if sixohsix' twitter
               # module is used! 
auth = twitter.OAuth(
    consumer_key='...',
    consumer_secret='...',
    token='...',
    token_secret='...'
)

stream = twitter.stream.TwitterStream(auth=auth, domain='userstream.twitter.com')

for msg in stream.user():
    if 'direct_message' in msg:
        print msg['direct_message']['text']

This script will print all new messages - not the ones already received before starting the script.

OTHER TIPS

There is no way to stream direct messages.

However, there is a way to stream a users timeline. Check out the documentation on Twitter here: https://dev.twitter.com/docs/streaming-apis/streams/user

from twython import TwythonStreamer


class MyStreamer(TwythonStreamer):
    def on_success(self, data):
        if 'text' in data:
            print data['text'].encode('utf-8')
        # Want to disconnect after the first result?
        # self.disconnect()

    def on_error(self, status_code, data):
        print status_code, data

# Requires Authentication as of Twitter API v1.1
stream = MyStreamer(APP_KEY, APP_SECRET,
                    OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

stream.user()

Until a new version of requests (https://github.com/kennethreitz/requests) is released, however, tweets from your followers will be one post behind. This should be fixed relatively soon though! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top