简体   繁体   English

通过Twitter流API 1.1获取提及和DM? (使用twython)

[英]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. 我正在使用twython(用于python的twitter API库)连接到流API,但我似乎只获得可能通过单词过滤的公共twitter流。 Isn't there a way to get a real-time stream of the authenticated user timeline or @mentions? 有没有办法获得经过身份验证的用户时间线或@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. 我一直在循环调用REST API以获取这些提及但是twitter不喜欢我提出这么多请求。

Twython documentation isn't helping me much about it, neither is the official twitter doc. Twython文档对我没什么帮助,官方Twitter文档也没有。

If there's another python library that can work better than twython for streaming (for Twitter API v1.1). 如果有另一个python库可以比twython更好地工作流(对于Twitter API v1.1)。 I'd appreciate the suggestion... Thanks. 我很感激这个建议......谢谢。

In the beginning of my research I thought python-twitter is the twitter library for Python. 在我的研究开始时,我认为python-twitter是Python 推特库。 But finally, it seems as if the Python Twitter Tools are more popular and support also twitter streaming. 但最后,似乎Python Twitter工具更受欢迎,并支持Twitter流媒体。

It's a bit tricky, the streaming API and the REST api are not equal for direct messages. 这有点棘手,流API和REST api对于直接消息不相等。 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. 此脚本将打印所有新消息 - 而不是在启动脚本之前已收到的消息。

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 在这里查看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()

Until a new version of requests ( https://github.com/kennethreitz/requests ) is released, however, tweets from your followers will be one post behind. 但是,在发布新版本的requestshttps://github.com/kennethreitz/requests )之前,您的关注者的推文将成为一个帖子。 This should be fixed relatively soon though! 这应该很快就会修好! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM