昨天我用Python编写了一个推特机器人,该机器人接收了唐纳德·特朗普的最新推文,并在Google Translate中进行了45次翻译,并将最终的英文翻译发给他。一切正常,除了我现在需要在代码开头添加某种“侦听器”以自动检测他何时鸣叫,以便其余代码可以发挥作用的事实。我已经在互联网上浏览了一段时间,但似乎找不到任何类型的事件处理程序,该事件处理程序可以使脚本检测到他何时鸣叫。所以这就是为什么我来找你们。有什么方法可以使用Tweepy或其他Python库来主动检测某人何时鸣叫?我将包含我的代码,以便你们在他发布Tweet时可以确切地看到我想要发生的事情。带有注释,因此希望它不会太复杂而难以理解。谢谢!
import tweepy
from googletrans import Translator
#Keys for accessing the Twitter API
consumer_key = 'PLACEHOLDER'
consumer_secret = 'PLACEHOLDER'
access_token = 'PLACEHOLDER'
access_token_secret = 'PLACEHOLDER'
#Setting up authentification
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#Scrapes my timeline for DT's latest tweet. I want this to be done AUTOMATICALLY when he tweets.
tweet = api.user_timeline(screen_name = 'realDonaldTrump', count = 1, include_rts = False, tweet_mode = 'extended')
#Translates the text from the .json file that is pulle from the previous line using the Google translate library.
for status in tweet:
translator = Translator()
translation = translator.translate(translation.text, 'mn')
translation = translator.translate(status._json["full_text"], 'ja')
#There are more translations in the actual code, but to reduce the length and complexity, I've taken those out. They don't matter to the specific question.
#Include his handle in a message so that the tweet is tweeted back at him once the translation is complete.
message = ('@realDonaldTrump', translation.text)
#Tweets the message back at him under that specific tweet using it's ID.
send = api.update_status(message, status._json["id"])
我只希望代码能够实时刮掉DT的其中一条推文的时间表。谢谢!