即使此人不发推文,也会调用Tweepy MyStreamListener吗?

时间:2019-01-30 04:35:32

标签: python twitter tweepy

我正在使用StreamListener制作一个Tweepy程序,该程序等待一个帐户发送鸣叫,将鸣叫另存为txt文件,替换字符,然后鸣叫更新txt文件。

当我将帐户设置为自己的@Bobwont时,它可以正常工作。等待@Bobwont鸣叫,将鸣叫另存为txt文件,替换字符并鸣叫文本。

当我将帐户设置为@Zackfox时,似乎从他的个人资料中拉了推文,而不是等待他发推文。我不确定如何解释。我已经发布了我的代码和终端进程。

如果您需要更多信息,请告诉我。

zabkfox.py:

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        if hasattr(status, 'retweeted_status'):
            print('retweet')
        else:
        #print data
            with open('tweet.txt','w') as tf:
                tf.write(status.text)

            with open('tweet.txt','r') as tf:
                contents = tf.read()
                newcontents = contents.replace('c','\U0001F171\uFE0F')

            print(newcontents)
            api.update_status(newcontents)

        return True

    def on_error(self, status):
        print(status.text)

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream.filter(follow=['1700626069'])

终端:

Documents/zabkfox/zabkfox.py 
this is my twitter bot
retweet
retweet
@za️kfox I liked this and then unliked this.  I had a good ️hu️kle. This is why I'm on twitter.
retweet
@za️kfox @bluefa️ebleedem Songs wa️k af
retweet
retweet
retweet
retweet

似乎只用他回复的推文来做。但这绝对不应该拖他的推文。应该等待他发推文。

1 个答案:

答案 0 :(得分:0)

找到了解决方法:

事实证明,这是在其他人回复他的地方收集推文。我只是添加了这个elif语句来解决它:

class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):

    if hasattr(status, 'retweeted_status'):
        print('retweet')

    elif hasattr(status, 'in_reply_to_user_id'):
        print('reply')

    else:
    #print data
        with open('tweet.txt','w') as tf:
            tf.write(status.text)

        with open('tweet.txt','r') as tf:
            contents = tf.read()
            newcontents = contents.replace('c','\U0001F171\uFE0F')

        print(newcontents)
        api.update_status(newcontents)

    return True