我曾经使用过一些可靠的Python代码,可以在Tweepy流监听器上实现自动回复功能,但由于Twitter API在八月份进行了更改,它已经不再起作用。
我正在重新构建它,每10秒获取我的最新提及(理想情况下它应该是小于10秒的,因为我希望做到几乎即时的回复),并检查是否在最近的十秒钟内出现...如果在,则脚本假设它是一条新推文并进行回复。
from tweepy import OAuthHandler
from tweepy import API
from datetime import datetime, time, timedelta
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
account_screen_name = ''
account_user_id = '897579556009332736'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterApi = API(auth)
mentions = twitterApi.mentions_timeline(count=1)
now = datetime.now()
for mention in mentions:
if now < (mention.created_at + timedelta(hours=1) + timedelta(seconds=10)):
print "there's a mention in the last 10 seconds"
# do magic reply stuff here!
else:
print "do nothing, no recent tweets/the last mention was more than 10 seconds ago so it isn't new"
这个方法每12秒可以运行一次;但是如果时间间隔更短,它会达到速率限制(例如,10秒的时间间隔最终会达到速率限制)...那么,有没有更好的方法只检索最近的提及,以便基于提及进行回复?我觉得我可能正在以非常低效的方式进行操作(例如,此方法实际上获取了最后20个提及!!),而API可能有更好的方法,我可以更频繁地使用而不会达到速率限制?