Skip to content

Instantly share code, notes, and snippets.

@sticks-stuff
Created October 10, 2020 23:47
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 sticks-stuff/62a6ad7d20b8faa1beb8cb49d8b4eb3d to your computer and use it in GitHub Desktop.
Save sticks-stuff/62a6ad7d20b8faa1beb8cb49d8b4eb3d to your computer and use it in GitHub Desktop.
PRAW RateLimit Wrapper - Auto-retry functions that get rate-limited by reddit after the ratelimit is over - wrap your functions in handle_ratelimit()
def handle_ratelimit(func, *args, **kwargs):
while True:
try:
func(*args, **kwargs)
break
except praw.exceptions.RedditAPIException as exception:
totalLength = str(exception.items[0].message).split('you are doing that too much. try again in ', 1)[1]
minutesToSleep = totalLength[0].partition("minutes.")[0]
secondsToSleep = int(minutesToSleep) * 60
print("Sleeping for " + str(secondsToSleep) + " seconds")
time.sleep(secondsToSleep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment