Skip to content

Instantly share code, notes, and snippets.

@itsthejoker
Last active October 20, 2017 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsthejoker/a037cab09441d559213f59d27c03cc38 to your computer and use it in GitHub Desktop.
Save itsthejoker/a037cab09441d559213f59d27c03cc38 to your computer and use it in GitHub Desktop.
Reddit APIException Handler
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