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
This commit is contained in:
Frank Schwenk
2025-11-18 15:42:01 +01:00
parent 90ae7ec90a
commit 2e84351348
+21 -1
View File
@@ -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,7 +166,12 @@ def main():
print(f" ⚠ No new songs to add for {band} (may be duplicates)")
continue
# Add songs to playlist
# 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):")
@@ -174,9 +180,23 @@ def main():
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}")