From c66f8d94569ed16a564a86c6cd67597e28070f1f Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Fri, 2 Aug 2024 19:31:09 +0300 Subject: [PATCH] Add param for wait between requests and wait until rate limit is reset --- .../mastodon_email_bridge.py | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mastodon_email_bridge/mastodon_email_bridge.py b/mastodon_email_bridge/mastodon_email_bridge.py index bd8ada3..193a9a3 100755 --- a/mastodon_email_bridge/mastodon_email_bridge.py +++ b/mastodon_email_bridge/mastodon_email_bridge.py @@ -65,8 +65,8 @@ class MastodonEmailBridge: next_url = self.process_url(next_url) while next_url != "" and (count < self.config['limit'] or self.config['limit'] == 0): count = count + 1 - self._log.debug("Waiting 2 seconds for next request...") - time.sleep(2) + self._log.debug("Waiting %s seconds for next request...", self.config['wait']) + time.sleep(self.config['wait']) self._log.debug("Getting URL '%s'", next_url) next_url = self.process_url(next_url) @@ -95,7 +95,21 @@ class MastodonEmailBridge: # self._log.debug("%s = %s", header, result.headers[header]) if 'X-RateLimit-Remaining' in result.headers and int(result.headers['X-RateLimit-Remaining']) < 10: self._log.warning("X-RateLimit-Reset: %s", result.headers['X-RateLimit-Reset']) - self._log.warning('Wait until that time to try again.') + try: + reset_time = time.mktime( + time.strptime( + result.headers['X-RateLimit-Reset'], + '%Y-%m-%dT%H:%M:%S.%fZ' + ) + ) + self._log.warning('Waiting until that time to try again.') + time.sleep(reset_time - time.time() + 2) + except Exception as error: + self._log.error( + "Error trying to convert given reset time '%s'. %s", + result.headers['X-RateLimit-Reset'], + error + ) return url data = result.json()[0] if int(data['id']) not in self.sent_items: @@ -216,6 +230,7 @@ class MastodonEmailBridge: help='Mastodon server full qualified name.' ) @click.option('--limit', '-L', default=0, help='Mastodon token with read access.') +@click.option('--wait', '-w', default=5, help='Seconds to wait between requests to avoid rate limits.') @click.option('--recipient', '-r', required=True, help='Recipient email to get the posts.') @click.option( '--sender',