If twitter is waiting on rate limit exceeded, it doesn't block the rest anymore.

This commit is contained in:
b3yond 2017-09-17 19:07:04 +02:00
parent 820dfcd504
commit f2d82285af

View file

@ -47,6 +47,7 @@ class RetweetBot(object):
self.no_shutdown_contact = True self.no_shutdown_contact = True
self.last_mention = self.get_history(self.historypath) self.last_mention = self.get_history(self.historypath)
self.trigger = trigger self.trigger = trigger
self.waitcounter = 0
if logpath: if logpath:
self.logpath = logpath self.logpath = logpath
else: else:
@ -118,6 +119,17 @@ class RetweetBot(object):
with open(self.historypath, "w") as f: with open(self.historypath, "w") as f:
f.write(str(self.last_mention)) f.write(str(self.last_mention))
def waiting(self):
"""
If the counter is not 0, you should be waiting instead.
:return: self.waitcounter(int): if 0, do smth.
"""
if self.waitcounter > 0:
sleep(1)
self.waitcounter -= 1
return self.waitcounter
def format_mastodon(self, status): def format_mastodon(self, status):
""" """
Bridge your Retweets to mastodon. Bridge your Retweets to mastodon.
@ -136,16 +148,17 @@ class RetweetBot(object):
:return: list of Status objects :return: list of Status objects
""" """
while 1: try:
try: if not self.waiting():
mentions = self.api.GetMentions(since_id=self.last_mention) mentions = self.api.GetMentions(since_id=self.last_mention)
return mentions return mentions
except twitter.TwitterError: except twitter.TwitterError:
self.log("Twitter API Error: Rate Limit Exceeded.") self.log("Twitter API Error: Rate Limit Exceeded.")
sleep(120) self.waitcounter += 60*15
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
self.log("Twitter API Error: Bad Connection.") self.log("Twitter API Error: Bad Connection.")
sleep(10) self.waitcounter += 10
return None
def retweet(self, status): def retweet(self, status):
""" """
@ -203,18 +216,19 @@ class RetweetBot(object):
mentions = self.crawl_mentions() mentions = self.crawl_mentions()
mastodon = [] mastodon = []
for status in mentions: if mentions is not None:
# Is the Text of the Tweet in the triggerlist? for status in mentions:
if self.trigger.is_ok(status.text): # Is the Text of the Tweet in the triggerlist?
# Retweet status if self.trigger.is_ok(status.text):
toot = self.retweet(status) # Retweet status
if toot: toot = self.retweet(status)
mastodon.append(toot) if toot:
mastodon.append(toot)
# save the id so it doesn't get crawled again # save the id so it doesn't get crawled again
if status.id > self.last_mention: if status.id > self.last_mention:
self.last_mention = status.id self.last_mention = status.id
self.save_last_mention() self.save_last_mention()
# Return Retweets for tooting on mastodon # Return Retweets for tooting on mastodon
return mastodon return mastodon