-
-
Save itsthejoker/a037cab09441d559213f59d27c03cc38 to your computer and use it in GitHub Desktop.
Reddit APIException Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import re | |
import praw | |
_pattern = re.compile('again in (?P<number>[0-9]+) (?P<unit>\w+)s?\.$', re.IGNORECASE) | |
def handle_rate_limit(exc): | |
time_map = { | |
'second': 1, | |
'minute': 60, | |
'hour': 60 * 60, | |
} | |
matches = _pattern.search(exc.message) | |
delay = matches[0] * time_map[matches[1]] | |
time.sleep(delay + 1) | |
def do_stuff(): | |
pass | |
while True: | |
try: | |
do_stuff() | |
break | |
except praw.exceptions.APIException as e: | |
if e.error_type == 'RATELIMIT': | |
handle_rate_limit(e) | |
else: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment