Add param for wait between requests and wait until rate limit is reset

This commit is contained in:
Antonio J. Delgado 2024-08-02 19:31:09 +03:00
parent afcb2c833b
commit c66f8d9456

View file

@ -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',