From 51592d0ffab7e882006589fd06045f1811909c7e Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Fri, 19 Dec 2025 21:36:36 +0100 Subject: [PATCH] fix yt-dlp error in prod --- main.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 01be767..772a713 100644 --- a/main.py +++ b/main.py @@ -195,11 +195,43 @@ def download_youtube_video(url, output_dir="."): # 1. Handle Cookies from ENV (Easier for deployment) cookies_path = '/app/cookies.txt' - if os.environ.get("YOUTUBE_COOKIES") and not os.path.exists(cookies_path): + + # Check for JSON cookies first (from user input) + if os.environ.get("YOUTUBE_COOKIES"): print("🍪 Found YOUTUBE_COOKIES env var, creating cookies.txt...") try: - with open(cookies_path, 'w') as f: - f.write(os.environ.get("YOUTUBE_COOKIES")) + cookies_content = os.environ.get("YOUTUBE_COOKIES") + # If it looks like JSON, convert to Netscape format + if cookies_content.strip().startswith('['): + import json + try: + cookies_json = json.loads(cookies_content) + with open(cookies_path, 'w') as f: + f.write("# Netscape HTTP Cookie File\n") + for cookie in cookies_json: + domain = cookie.get('domain', '') + # Initial dot is required for some tools + if not domain.startswith('.'): + domain = '.' + domain + + flag = 'TRUE' if domain.startswith('.') else 'FALSE' + path = cookie.get('path', '/') + secure = 'TRUE' if cookie.get('secure', False) else 'FALSE' + expiration = str(int(cookie.get('expirationDate', 0))) if cookie.get('expirationDate') else '0' + name = cookie.get('name', '') + value = cookie.get('value', '') + + f.write(f"{domain}\t{flag}\t{path}\t{secure}\t{expiration}\t{name}\t{value}\n") + print("✅ Converted JSON cookies to Netscape format.") + except json.JSONDecodeError: + # Fallback if not valid JSON, assume it's already Netscape + with open(cookies_path, 'w') as f: + f.write(cookies_content) + else: + # Assume Netscape format + with open(cookies_path, 'w') as f: + f.write(cookies_content) + except Exception as e: print(f"⚠️ Failed to write cookies file: {e}")