我有一个twitter机器人,可以响应包含标签的tweet。我一直在使用twt = api.search(q='param')
来拉推特,它一直工作得很好,但后来我改用了myStreamListener = MyStreamListener()
twt = tweepy.Stream(auth = api.auth, listener=myStreamListener())
来实时拉推。但这不管用,我不知道为什么。我的代码是:
myStreamListener = MyStreamListener()
twt = tweepy.Stream(auth = api.auth, listener=myStreamListener())
twt.filter(track=['#www'], async=True)
#list of specific strings we want to omit from responses
badWords = ['xxx','yyy' ]
#list of specific strings I want to check for in tweets and reply to
genericparam = ['@zzz']
def does_contain_words(tweet, wordsToCheck):
for word in wordsToCheck:
if word in tweet:
return True
return False
for currentTweet in twt:
#if the tweet contains a good word and doesn't contain a bad word
if does_contain_words(currentTweet.text, genericparam) and not does_contain_words(currentTweet.text, badWords):
#reply to tweet
screen = currentTweet.user.screen_name
message = "@%s this is a reply" % (screen)
currentTweet = api.update_status(message, currentTweet.id)
只是偶然发现的。在
当您创建
myStreamListener = MyStreamListener(self.api)
自我.api要使用的实例需要传递给StreamListner构造函数,否则它将为null。在我相信您的问题是您试图通过流发送回复,而不是在单独的线程中使用http请求。这是不可能做到的,如下所述:
https://dev.twitter.com/streaming/overview
相关问题 更多 >
编程相关推荐