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:
+21
-1
@@ -6,6 +6,7 @@ Create YouTube Music playlist "Core Fest 2025" with top 3 songs from each festiv
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
from ytmusicapi import YTMusic
|
from ytmusicapi import YTMusic
|
||||||
from ytmusicapi.auth.browser import setup_browser
|
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)")
|
print(f" ⚠ No new songs to add for {band} (may be duplicates)")
|
||||||
continue
|
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:
|
try:
|
||||||
yt.add_playlist_items(playlist_id, songs_to_add)
|
yt.add_playlist_items(playlist_id, songs_to_add)
|
||||||
print(f" ✓ Added {len(songs_to_add)} song(s):")
|
print(f" ✓ Added {len(songs_to_add)} song(s):")
|
||||||
@@ -174,9 +180,23 @@ def main():
|
|||||||
artist = result.get("artists", [{}])[0].get("name", "Unknown")
|
artist = result.get("artists", [{}])[0].get("name", "Unknown")
|
||||||
print(f" - {title} by {artist}")
|
print(f" - {title} by {artist}")
|
||||||
total_added += len(songs_to_add)
|
total_added += len(songs_to_add)
|
||||||
|
added_successfully = True
|
||||||
|
break
|
||||||
except Exception as e:
|
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}")
|
print(f" ✗ Error adding songs: {e}")
|
||||||
failed_bands.append(band)
|
failed_bands.append(band)
|
||||||
|
break
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" ✗ Error searching for {band}: {e}")
|
print(f" ✗ Error searching for {band}: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user