From 8bde1b535625870bdfcb35d4a049c370c4091e58 Mon Sep 17 00:00:00 2001 From: muskit <15199219+muskit@users.noreply.github.com> Date: Sun, 20 Aug 2023 16:06:17 -0700 Subject: [PATCH] prevent rapid api calls when running into post rate limit --- src/twapi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/twapi.py b/src/twapi.py index 5491d64..c7afa1a 100644 --- a/src/twapi.py +++ b/src/twapi.py @@ -93,7 +93,7 @@ class TwAPI: tweet = self.client.create_tweet(text=text, media_ids=media_ids, in_reply_to_tweet_id=reply_to_tweet, quote_tweet_id=quote_tweet_id) return tweet except tweepy.TooManyRequests as e: - wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1 + wait_for = abs(float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp()) + 1 print(f'\thit rate limit -- attempting to create Tweet again in {wait_for} seconds...') await asyncio.sleep(wait_for) return await self.post_tweet(text=text, media_ids=media_ids, reply_to_tweet=reply_to_tweet) @@ -164,4 +164,4 @@ class TwAPI: return False print(f'Posting {ttweet.username}/{ttweet.tweet_id}...') - return await self.post_ttweet(ttweet) \ No newline at end of file + return await self.post_ttweet(ttweet)