fixed Rate limit exceeded Error

This commit is contained in:
b3yond 2017-07-19 12:17:15 +02:00
parent dc8ba181f7
commit a3139b2380

View file

@ -83,6 +83,11 @@ class RetweetBot(object):
last_mention = "" last_mention = ""
return last_mention return last_mention
def save_last_mention(self):
""" Saves the last retweeted tweet in last_mention. """
with open(self.historypath, "w") as f:
f.write(str(self.last_mention))
def format_mastodon(self, status): def format_mastodon(self, status):
""" """
Bridge your Retweets to mastodon. Bridge your Retweets to mastodon.
@ -106,7 +111,7 @@ class RetweetBot(object):
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:
print("[ERROR] Rate Limit exceeded, trying again in a minute") traceback.print_exc()
sleep(60) sleep(60)
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
print("[ERROR] Bad Connection.") print("[ERROR] Bad Connection.")
@ -122,11 +127,15 @@ class RetweetBot(object):
while 1: while 1:
try: try:
self.api.PostRetweet(status.id) self.api.PostRetweet(status.id)
if status.id > self.last_mention:
self.last_mention = status.id
return self.format_mastodon(status) return self.format_mastodon(status)
# maybe one day we get rid of this error. If not, try to uncomment # maybe one day we get rid of this error. If not, try to uncomment
# these lines. # these lines.
except twitter.error.TwitterError: except twitter.error.TwitterError:
print("[ERROR] probably you already retweeted this tweet.") print("[ERROR] probably you already retweeted this tweet.")
if status.id > self.last_mention:
self.last_mention = status.id
return None return None
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
print("[ERROR] Bad Connection.") print("[ERROR] Bad Connection.")
@ -172,7 +181,8 @@ class RetweetBot(object):
mastodon.append(toot) mastodon.append(toot)
# save the id so it doesn't get crawled again # save the id so it doesn't get crawled again
self.last_mention = status.id print status.id, self.last_mention # debug
self.save_last_mention()
# Return Retweets for tooting on mastodon # Return Retweets for tooting on mastodon
return mastodon return mastodon
@ -182,8 +192,7 @@ class RetweetBot(object):
print("[ERROR] Shit went wrong, closing down.") print("[ERROR] Shit went wrong, closing down.")
if self.no_shutdown_contact: if self.no_shutdown_contact:
return return
with open(self.historypath, "w") as f: self.save_last_mention()
f.write(str(self.last_mention))
self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.api.PostDirectMessage("Help! I broke down. restart me pls :$",
self.user_id, self.screen_name) self.user_id, self.screen_name)
@ -201,5 +210,5 @@ if __name__ == "__main__":
bot.flow() bot.flow()
sleep(10) sleep(10)
except: except:
traceback.print_exc() traceback.print_exc()
bot.shutdown() bot.shutdown()