From 4b26e9922d25192792a25f466bf67ec94ae72030 Mon Sep 17 00:00:00 2001 From: William Lam Date: Mon, 28 Dec 2020 10:42:23 -0500 Subject: [PATCH] Rate limit errors on message sending are now handled properly --- hon_patch_notes_game_bot/communications.py | 21 +++++++++++++++++++-- hon_patch_notes_game_bot/main.py | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/hon_patch_notes_game_bot/communications.py b/hon_patch_notes_game_bot/communications.py index 5825b51..8b1ec6e 100644 --- a/hon_patch_notes_game_bot/communications.py +++ b/hon_patch_notes_game_bot/communications.py @@ -1,6 +1,7 @@ """ This module contains functions related to communications across the Reddit platform """ +import time from praw.exceptions import RedditAPIException @@ -67,11 +68,27 @@ def send_message_to_winners(reddit, winners_list, version_string, gold_coin_rewa ) try: reddit.redditor(recipient).message(subject=subject_line, message=message) - except RedditAPIException as redditError: + + # Reddit API Exception + except RedditAPIException as redditException: print( - f"{redditError}\n{recipient} was not sent a message, continuing to next recipient" + f"{redditException}\n{recipient} was not sent a message, continuing to next recipient" ) + + # Rate limit error handling + # Reference: https://github.com/GrafeasGroup/tor/blob/5ee21af7cc752abc6e7ae6aa47228105e6ec2e05/tor/core/helpers.py#L160-L171 + if redditException.error_type == "RATELIMIT": + # Sleep for the rate limit duration + totalLength = str(redditException.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) + continue + except Exception as error: print( f"{error}\n{recipient} was not sent a message, continuing to next recipient" diff --git a/hon_patch_notes_game_bot/main.py b/hon_patch_notes_game_bot/main.py index 6d0a82c..eb1a32f 100644 --- a/hon_patch_notes_game_bot/main.py +++ b/hon_patch_notes_game_bot/main.py @@ -226,6 +226,8 @@ def main(): # noqa: C901 gold_coin_reward=gold_coin_reward, ) + print("Reddit bot script ended gracefully") + if __name__ == "__main__": main()