Add param for wait between requests and wait until rate limit is reset
This commit is contained in:
parent
afcb2c833b
commit
c66f8d9456
1 changed files with 18 additions and 3 deletions
|
@ -65,8 +65,8 @@ class MastodonEmailBridge:
|
||||||
next_url = self.process_url(next_url)
|
next_url = self.process_url(next_url)
|
||||||
while next_url != "" and (count < self.config['limit'] or self.config['limit'] == 0):
|
while next_url != "" and (count < self.config['limit'] or self.config['limit'] == 0):
|
||||||
count = count + 1
|
count = count + 1
|
||||||
self._log.debug("Waiting 2 seconds for next request...")
|
self._log.debug("Waiting %s seconds for next request...", self.config['wait'])
|
||||||
time.sleep(2)
|
time.sleep(self.config['wait'])
|
||||||
self._log.debug("Getting URL '%s'", next_url)
|
self._log.debug("Getting URL '%s'", next_url)
|
||||||
next_url = self.process_url(next_url)
|
next_url = self.process_url(next_url)
|
||||||
|
|
||||||
|
@ -95,7 +95,21 @@ class MastodonEmailBridge:
|
||||||
# self._log.debug("%s = %s", header, result.headers[header])
|
# self._log.debug("%s = %s", header, result.headers[header])
|
||||||
if 'X-RateLimit-Remaining' in result.headers and int(result.headers['X-RateLimit-Remaining']) < 10:
|
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("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
|
return url
|
||||||
data = result.json()[0]
|
data = result.json()[0]
|
||||||
if int(data['id']) not in self.sent_items:
|
if int(data['id']) not in self.sent_items:
|
||||||
|
@ -216,6 +230,7 @@ class MastodonEmailBridge:
|
||||||
help='Mastodon server full qualified name.'
|
help='Mastodon server full qualified name.'
|
||||||
)
|
)
|
||||||
@click.option('--limit', '-L', default=0, help='Mastodon token with read access.')
|
@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('--recipient', '-r', required=True, help='Recipient email to get the posts.')
|
||||||
@click.option(
|
@click.option(
|
||||||
'--sender',
|
'--sender',
|
||||||
|
|
Loading…
Reference in a new issue