From ffc80a5081faa59986d97455c5b8302090d8bfe2 Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Wed, 15 Mar 2023 07:20:49 +0000 Subject: [PATCH] Be timezone aware in processing ratelimits --- get_context.py | 6 ++++-- requirements.txt | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/get_context.py b/get_context.py index 13c2291..9f6f882 100644 --- a/get_context.py +++ b/get_context.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from datetime import datetime, timedelta +from dateutil import parser import itertools import json import os @@ -584,8 +585,9 @@ def get(url, headers = {}, timeout = 5, max_tries = 5): response = requests.get( url, headers= h, timeout=timeout) if response.status_code == 429: if max_tries > 0: - reset = datetime.strptime(response.headers['x-ratelimit-reset'], '%Y-%m-%dT%H:%M:%S.%fZ') - wait = (reset - datetime.now()).total_seconds() + 1 + reset = parser.parse(response.headers['x-ratelimit-reset']) + now = datetime.now(datetime.now().astimezone().tzinfo) + wait = (reset - now).total_seconds() + 1 log(f"Rate Limit hit requesting {url}. Waiting {wait} sec to retry at {response.headers['x-ratelimit-reset']}") time.sleep(wait) return get(url, headers, timeout, max_tries - 1) diff --git a/requirements.txt b/requirements.txt index 2856bcc..c585338 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ idna==3.4 requests==2.28.2 six==1.16.0 urllib3==1.26.14 +python-dateutil==2.8.2 \ No newline at end of file