From 2e84351348ec449f68376cfcf7d7c467fa0b9d9b Mon Sep 17 00:00:00 2001 From: Frank Schwenk Date: Tue, 18 Nov 2025 15:42:01 +0100 Subject: [PATCH] Add retry logic for HTTP 409 Conflict errors - Retry up to 3 times with 5 second delay on HTTP 409 errors - Only retry on Conflict errors, other errors fail immediately - Provides clear feedback on retry attempts --- create_playlist.py | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/create_playlist.py b/create_playlist.py index 20c2b86..7b6b717 100644 --- a/create_playlist.py +++ b/create_playlist.py @@ -6,6 +6,7 @@ Create YouTube Music playlist "Core Fest 2025" with top 3 songs from each festiv import os import sys import json +import time from ytmusicapi import YTMusic from ytmusicapi.auth.browser import setup_browser @@ -165,18 +166,37 @@ def main(): print(f" ⚠ No new songs to add for {band} (may be duplicates)") continue - # Add songs to playlist - try: - yt.add_playlist_items(playlist_id, songs_to_add) - print(f" ✓ Added {len(songs_to_add)} song(s):") - for result in search_results[:len(songs_to_add)]: - title = result.get("title", "Unknown") - artist = result.get("artists", [{}])[0].get("name", "Unknown") - print(f" - {title} by {artist}") - total_added += len(songs_to_add) - except Exception as e: - print(f" ✗ Error adding songs: {e}") - failed_bands.append(band) + # Add songs to playlist with retry logic for HTTP 409 conflicts + max_retries = 3 + retry_delay = 5 # seconds + added_successfully = False + + for attempt in range(1, max_retries + 1): + try: + yt.add_playlist_items(playlist_id, songs_to_add) + print(f" ✓ Added {len(songs_to_add)} song(s):") + for result in search_results[:len(songs_to_add)]: + title = result.get("title", "Unknown") + artist = result.get("artists", [{}])[0].get("name", "Unknown") + print(f" - {title} by {artist}") + total_added += len(songs_to_add) + added_successfully = True + break + except Exception as e: + error_msg = str(e) + # Check if it's an HTTP 409 Conflict error + if "409" in error_msg or "Conflict" in error_msg: + if attempt < max_retries: + print(f" ⚠ HTTP 409 Conflict on attempt {attempt}/{max_retries}. Retrying in {retry_delay} seconds...") + time.sleep(retry_delay) + else: + print(f" ✗ Error adding songs after {max_retries} attempts: {e}") + failed_bands.append(band) + else: + # For other errors, don't retry + print(f" ✗ Error adding songs: {e}") + failed_bands.append(band) + break except Exception as e: print(f" ✗ Error searching for {band}: {e}")