Allow to set requests limit

This commit is contained in:
Antonio J. Delgado 2024-08-03 10:27:05 +03:00
parent 0de622561b
commit 783eecd9a2
2 changed files with 18 additions and 13 deletions

View file

@ -50,3 +50,6 @@ Customize the templates (if you know HTML, CSS and Jinja2) and run the command.
--config FILE Read configuration from FILE. --config FILE Read configuration from FILE.
--help Show this message and exit. --help Show this message and exit.
``` ```
## Notes
- Clean the folder ~/.mastodon_email_bridge_sent_items every now and then, but you can check the generated HTML files to test new templates.
- The posts that have been sent by email are stored in an SQLite3 database in ~/.mastodon_email_bridge_sent_items.db if you want a post to be sent again you can remove it from there and run again the script.

View file

@ -74,10 +74,11 @@ class MastodonEmailBridge:
self.session.headers.update({'Authorization': f"Bearer {self.config['token']}"}) self.session.headers.update({'Authorization': f"Bearer {self.config['token']}"})
count=1 count=1
self._log.debug( self._log.debug(
"Getting URL 'https://%s/api/v1/timelines/home?limit=1'", "Getting URL 'https://%s/api/v1/timelines/home?limit=%s'",
self.config['server'] self.config['server'],
self.config['limit_per_request']
) )
next_url = f"https://{self.config['server']}/api/v1/timelines/home?limit=1" next_url = f"https://{self.config['server']}/api/v1/timelines/home?limit={self.config['limit_per_request']}"
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
@ -127,16 +128,16 @@ class MastodonEmailBridge:
error error
) )
return url return url
data = result.json()[0] for data in result.json():
if int(data['id']) not in self.sent_items: if int(data['id']) not in self.sent_items:
self.send_mail(data) self.send_mail(data)
else: else:
self._log.debug("Skipping post %s that was sent in the past", data['id']) self._log.debug("Skipping post %s that was sent in the past", data['id'])
if 'Link' in result.headers: if 'Link' in result.headers:
for link in result.headers['Link'].split(', '): for link in result.headers['Link'].split(', '):
slink = link.split('; ') slink = link.split('; ')
if slink[1] == 'rel="next"': if slink[1] == 'rel="next"':
next_url = slink[0].replace('<', '').replace('>', '') next_url = slink[0].replace('<', '').replace('>', '')
return next_url return next_url
def send_mail(self, data): def send_mail(self, data):
@ -246,6 +247,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('--limit-per-request', '-R', default=40, help='Mastodon token with read access.')
@click.option('--wait', '-w', default=5, help='Seconds to wait between requests to avoid rate limits.') @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(