Tweety回复推文流

时间:2016-07-02 16:29:44

标签: python twitter tweepy

我有一个Twitter机器人响应包含主题标签的推文。我一直在使用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)

2 个答案:

答案 0 :(得分:1)

我相信您的问题是您尝试通过流发送回复,而不是在单独的线程中使用http请求。如下所述,这是不可能的:

https://dev.twitter.com/streaming/overview

答案 1 :(得分:1)

偶然发现了这一点。

当您创建myStreamListener = MyStreamListener(self.api) self.api实例时,您希望将需要传递给StreamListner构造函数,否则它将为null。