diff --git a/apps/musicseerr/tasks.py b/apps/musicseerr/tasks.py
index 56ec05c..13f6f8f 100644
--- a/apps/musicseerr/tasks.py
+++ b/apps/musicseerr/tasks.py
@@ -275,31 +275,35 @@ def scrape_spotify_playlist(url: str, proxy_url: str) -> list:
playlist_id = match.group(1)
embed_url = f"https://open.spotify.com/embed/playlist/{playlist_id}"
- proxies = {"http": proxy_url, "https": proxy_url} if proxy_url else None
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
-
- logger.info(f"Scraping Spotify playlist via embed URL: {embed_url}")
- try:
- response = requests.get(embed_url, headers=headers, proxies=proxies, timeout=15)
- if response.status_code == 200:
- next_data_match = re.search(r'', response.text)
- if next_data_match:
- data = json.loads(next_data_match.group(1))
- entity = data['props']['pageProps']['state']['data']['entity']
- tracks = entity.get('tracks', entity.get('trackList', []))
- track_queries = []
- for t in tracks:
- title = t.get('title', '').strip()
- subtitle = t.get('subtitle', '').replace('\xa0', ' ').strip()
- if title and subtitle:
- track_queries.append(f"{subtitle} - {title}")
- elif title:
- track_queries.append(title)
- return track_queries
- except Exception as e:
- logger.warning(f"Failed to scrape Spotify playlist: {e}")
+ # Try with proxy first, then without proxy
+ for use_proxy in [True, False]:
+ current_proxies = {"http": proxy_url, "https": proxy_url} if (use_proxy and proxy_url) else None
+ proxy_desc = f"via proxy {proxy_url}" if current_proxies else "directly"
+ logger.info(f"Scraping Spotify playlist via embed URL: {embed_url} ({proxy_desc})")
+ try:
+ response = requests.get(embed_url, headers=headers, proxies=current_proxies, timeout=15)
+ if response.status_code == 200:
+ next_data_match = re.search(r'', response.text)
+ if next_data_match:
+ data = json.loads(next_data_match.group(1))
+ entity = data['props']['pageProps']['state']['data']['entity']
+ tracks = entity.get('tracks', entity.get('trackList', []))
+ track_queries = []
+ for t in tracks:
+ title = t.get('title', '').strip()
+ subtitle = t.get('subtitle', '').replace('\xa0', ' ').strip()
+ if title and subtitle:
+ track_queries.append(f"{subtitle} - {title}")
+ elif title:
+ track_queries.append(title)
+ return track_queries
+ logger.warning(f"Failed to scrape Spotify playlist {proxy_desc}: HTTP {response.status_code}")
+ except Exception as e:
+ logger.warning(f"Failed to scrape Spotify playlist {proxy_desc}: {e}")
+
return []