From 783ee4480ee06a86f22989e5ff477e70670aba20 Mon Sep 17 00:00:00 2001 From: Khoa Vo Date: Thu, 1 Jan 2026 16:24:09 +0700 Subject: [PATCH] Fix NAS 500 Error: Forward yt-dlp http_headers to request --- backend/api/routes.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/api/routes.py b/backend/api/routes.py index a1f33ea..4fe681a 100644 --- a/backend/api/routes.py +++ b/backend/api/routes.py @@ -657,6 +657,7 @@ async def stream_audio(id: str): info = ydl.extract_info(url, download=False) stream_url = info.get('url') ext = info.get('ext') + http_headers = info.get('http_headers', {}) # Get headers required for the URL # Determine MIME type if ext == 'm4a': @@ -672,7 +673,7 @@ async def stream_audio(id: str): raise ydl_error if stream_url: - cache_data = {"url": stream_url, "mime": mime_type} + cache_data = {"url": stream_url, "mime": mime_type, "headers": http_headers} cache.set(cache_key, cache_data, ttl_seconds=3600) if not stream_url: @@ -682,7 +683,10 @@ async def stream_audio(id: str): # Pre-open the connection to verify it works and get headers try: - external_req = requests.get(stream_url, stream=True, timeout=30) + # Use headers from yt-dlp (User-Agent, etc.) + req_headers = cached_data.get('headers', {}) if 'cached_data' in locals() else http_headers + + external_req = requests.get(stream_url, stream=True, timeout=30, headers=req_headers) external_req.raise_for_status() # Check for 403/404 immediately except requests.exceptions.HTTPError as http_err: print(f"DEBUG: Stream Pre-flight HTTP Error: {http_err}")