From ed2e408d1e22fac7359b7cd83a8522429fd6d32b Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Thu, 28 Mar 2024 13:54:29 +0100 Subject: [PATCH] Adding sleep kwarg to YoutubeAPIClient --- minet/youtube/client.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/minet/youtube/client.py b/minet/youtube/client.py index dc7da65a28..17aec678dc 100644 --- a/minet/youtube/client.py +++ b/minet/youtube/client.py @@ -43,6 +43,7 @@ YouTubeExclusiveMemberError, YouTubeUnknown403Error, YouTubeAccessNotConfiguredError, + YouTubeAPILimitReached, ) from minet.youtube.types import ( YouTubeVideo, @@ -72,7 +73,7 @@ def get_channel_id(scraper: YouTubeScraper, channel_target: str) -> str: class YouTubeAPIClient(object): - def __init__(self, key): + def __init__(self, key, sleep: bool = True): if not isinstance(key, list): key = [key] @@ -80,6 +81,7 @@ def __init__(self, key): self.current_key = key[0] self.pool_manager = create_pool_manager() self.scraper = YouTubeScraper() + self.sleep = sleep # YouTube's API is known to crash sometimes... self.retryer = create_request_retryer( @@ -113,6 +115,9 @@ def request_json(self, url): elif reason == "quotaExceeded": # Current key is exhausted, disabling it and switching to another if there is one if not self.rotate_key(): + if not self.sleep: + raise YouTubeAPILimitReached + # If all keys are exhausted, start waiting until tomorrow and reset keys sleep_time = seconds_to_midnight_pacific_time() + 10