From 2888185635431ce935d8b0e52a0ab6479709e3f8 Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Tue, 7 Mar 2023 12:47:10 +0000 Subject: [PATCH] deal better with rate limits --- get_context.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/get_context.py b/get_context.py index fccad5b..7eebb34 100644 --- a/get_context.py +++ b/get_context.py @@ -7,6 +7,7 @@ import os import re import sys import requests +import time def pull_context( @@ -17,6 +18,7 @@ def pull_context( reply_interval_hours, max_home_timeline_length, ): + if reply_interval_hours > 0: """pull the context toots of toots user replied to, from their original server, and add them to the local server.""" @@ -314,6 +316,11 @@ def get_toot_context(server, toot_id, toot_url): except Exception as ex: print(f"Error parsing context for toot {toot_url}. Exception: {ex}") return [] + elif resp.status_code == 429: + reset = datetime.strptime(resp.headers['x-ratelimit-reset'], '%Y-%m-%dT%H:%M:%S.%fZ') + print(f"Rate Limit hit when getting context for {toot_url}. Waiting to retry at {resp.headers['x-ratelimit-reset']}") + time.sleep((reset - datetime.now()).total_seconds() + 1) + return get_toot_context(server, toot_id, toot_url) print( f"Error getting context for toot {toot_url}. Status code: {resp.status_code}" @@ -362,6 +369,11 @@ def add_context_url(url, server, access_token): "Make sure you have the read:search scope enabled for your access token." ) return False + elif resp.status_code == 429: + reset = datetime.strptime(resp.headers['x-ratelimit-reset'], '%Y-%m-%dT%H:%M:%S.%fZ') + print(f"Rate Limit hit when adding url {search_url}. Waiting to retry at {resp.headers['x-ratelimit-reset']}") + time.sleep((reset - datetime.now()).total_seconds() + 1) + return add_context_url(url, server, access_token) else: print( f"Error adding url {search_url} to server {server}. Status code: {resp.status_code}"