2013-06-21 76 views
5

我正在使用twython(用于python的twitter API库)连接到流API,但我似乎只获得可能通过单词过滤的公共Twitter流。没有办法获得经过身份验证的用户时间表或@mentions的实时流吗?通过twitter stream API 1.1获取提及和DM吗? (使用twython)

我一直在循环浏览REST API的延迟调用以获得这些提及,但Twitter并不喜欢我提出如此多的请求。

Twython文档对我的帮助不大,官方的twitter文档也不是。

如果还有另一个python库比twython更适合流式传输(对于Twitter API v1.1)。我会很感激这个建议......谢谢。

回答

3

在我的研究开始时,我认为python-twitter用于Python的twitter库。但最后,看起来好像更受欢迎,并支持Twitter流。

这有点棘手,流媒体API和REST API对于直接消息是不相等的。这个小示例脚本演示如何使用用户流获得直接的信息:

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'] 

这个脚本会打印所有新邮件 - 不启动脚本之前已经收到的人。

0

无法传输直接消息。

但是,有一种方法可以流式传输用户时间表。检查出的文档在Twitter上的位置: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() 

待到requestshttps://github.com/kennethreitz/requests)发布了新的版本,但是,从你的追随者鸣叫将在后面一个职位。尽管这应该尽快修复! :)

+2

这似乎是错误的。直接消息流在这里提到:https://dev.twitter.com/docs/streaming-apis/streams/user#Direct_messages – lumbric

+0

哦,我很抱歉;我从来没有看到这个文档。 –

+0

更新或删除您的答案以反映DM流。 – Sheharyar

相关问题