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:
+32
-12
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user