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,6 +216,7 @@ class RetweetBot(object):
mentions = self.crawl_mentions() mentions = self.crawl_mentions()
mastodon = [] mastodon = []
if mentions is not None:
for status in mentions: for status in mentions:
# Is the Text of the Tweet in the triggerlist? # Is the Text of the Tweet in the triggerlist?
if self.trigger.is_ok(status.text): if self.trigger.is_ok(status.text):