Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting mentions and DMs through twitter stream API 1.1? (Using twython)

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.

like image 926
Carlos Bruguera Avatar asked Oct 03 '22 18:10

Carlos Bruguera


1 Answers

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.

like image 161
lumbric Avatar answered Oct 07 '22 18:10

lumbric