如何通过提及过滤流

时间:2014-06-05 21:43:46

标签: python tweepy twitter-streaming-api

我正在创建一个Twitter机器人,它将响应针对其相关Twitter手柄的推文​​。我已经看过有关如何通过主题标签过滤流的文档,但我不知道如何通过提及进行过滤。例如,如果与bot相关联的Twitter句柄是twitter_bot,我想做这样的事情:

listener = CustomListener()
stream = tweepy.Stream(OAuth, listener)

# Is there a parameter here that accomplishes this??
stream.filter(mentions=["twitter_bot"])

我只想处理有人在twitter_bot处理的推文。

即。 “@twitter_bot怎么回事?”

谢谢!

1 个答案:

答案 0 :(得分:1)

有一个REST API endpoint用于提及,您可以使用它,而documentation已过时,该方法已重命名为mentions_timeline。这里有你的方法:https://github.com/tweepy/tweepy/blob/master/tweepy/api.py#L79

使用以下代码并更改密钥/秘密,您将获得经过身份验证的用户的提及:

import tweepy

auth = tweepy.OAuthHandler(consumer_key='AAA', consumer_secret='BBB')
auth.set_access_token('CCC', 'DDD')
api = tweepy.API(auth_handler=auth, secure=True, retry_count=5)

mentions = api.mentions_timeline()
for mention in mentions:
    print mention.id, mention.author.screen_name, mention.text