如何通过提及过滤流?
我正在创建一个Twitter机器人,它会对发给它的推文进行回复。我看到过一些关于如何通过话题标签来过滤推文的文档,但我不知道怎么通过提及来过滤。比如说,如果这个机器人关联的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 个回答
2
有一个REST API接口可以用来获取提到你的信息,你可以用tweepy这个库来实现。虽然文档有点过时,但这个方法现在叫做mentions_timeline。在tweepy中,你可以找到这个方法: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