From 00e03209de2c28ea8194e5235d7a5f2d4f4fe810 Mon Sep 17 00:00:00 2001 From: ramforth Date: Wed, 29 Oct 2025 13:18:18 +0100 Subject: [PATCH] Feat: Implement exponential backoff for YouTube API calls to handle quota errors. --- main.py | 68 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 4506160..a546318 100644 --- a/main.py +++ b/main.py @@ -45,33 +45,53 @@ def get_authenticated_service(): API_SERVICE_NAME, API_VERSION, credentials=credentials) def get_live_chat_id(youtube, video_id): - try: - response = youtube.videos().list( - part='liveStreamingDetails', - id=video_id - ).execute() + retries = 0 + max_retries = 5 + while retries < max_retries: + try: + response = youtube.videos().list( + part='liveStreamingDetails', + id=video_id + ).execute() - if 'items' in response and response['items']: - video = response['items'][0] - if 'liveStreamingDetails' in video and 'activeLiveChatId' in video['liveStreamingDetails']: - return video['liveStreamingDetails']['activeLiveChatId'] - return None - except googleapiclient.errors.HttpError as e: - console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]") - return None + if 'items' in response and response['items']: + video = response['items'][0] + if 'liveStreamingDetails' in video and 'activeLiveChatId' in video['liveStreamingDetails']: + return video['liveStreamingDetails']['activeLiveChatId'] + return None + except googleapiclient.errors.HttpError as e: + if e.resp.status == 403 and "quotaExceeded" in str(e.content): + console.print(f"[red]Quota exceeded. Retrying in {2**retries} seconds...[/red]") + time.sleep(2**retries) + retries += 1 + else: + console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]") + return None + console.print(f"[red]Failed to get live chat ID after {max_retries} retries due to quota issues.[/red]") + return None def fetch_live_chat_messages(youtube, live_chat_id, page_token=None): - try: - request = youtube.liveChatMessages().list( - liveChatId=live_chat_id, - part='snippet,authorDetails', - pageToken=page_token - ) - response = request.execute() - return response - except googleapiclient.errors.HttpError as e: - console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]") - return None + retries = 0 + max_retries = 5 + while retries < max_retries: + try: + request = youtube.liveChatMessages().list( + liveChatId=live_chat_id, + part='snippet,authorDetails', + pageToken=page_token + ) + response = request.execute() + return response + except googleapiclient.errors.HttpError as e: + if e.resp.status == 403 and "quotaExceeded" in str(e.content): + console.print(f"[red]Quota exceeded. Retrying in {2**retries} seconds...[/red]") + time.sleep(2**retries) + retries += 1 + else: + console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]") + return None + console.print(f"[red]Failed to fetch live chat messages after {max_retries} retries due to quota issues.[/red]") + return None def main(): youtube = get_authenticated_service()