繁体   English   中英

过滤来自 Tweepy stream - Python 的多个转发

[英]Filtering multiple retweets from Tweepy stream - Python

我正在使用 tweepy 3.10 制作一个转推机器人,该机器人目前从 stream 运行。 它可以过滤掉我自己的转发,但是如果其他人转发我转发的内容,它就会崩溃。 如何最好地过滤掉我已经转发的项目?

我尝试添加:

if tweettext.startswith("rt @") == True:
   return

但这并没有以我希望的方式过滤。 目前的逻辑是:

def on_status(self, tweet):
    # This tweet is a reply or I'm its author so, ignore it
    if tweet.in_reply_to_status_id is not None or \
            tweet.user.id == self.me.id:
        return
    else:
        # Retweets based on the above logic
        tweet.retweet()

    print(f"{tweet.user.name}:{tweet.text}")

对于将来遇到这个问题的人来说,解决方案非常简单。 添加“尝试” function 以忽略尝试转发但无法转发时是否生成错误:

def on_status(self, tweet):
    # This tweet is a reply or I'm its author so, ignore it
    if tweet.in_reply_to_status_id is not None or \
            tweet.user.id == self.me.id:
        return
    try:
        # Retweets based on the above logic
        tweet.retweet()
    # checking if the tweet is a retweet (exception added)
    except Exception:
        return

    print(f"{tweet.user.name}:{tweet.text}")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM