Spaces:
Running
Running
Commit ·
656ce65
1
Parent(s): ff37c87
Auto-convert JSON cookies to Netscape format
Browse files
app.py
CHANGED
|
@@ -32,8 +32,38 @@ COOKIES_FILE_PATH = os.path.join(UPLOAD_FOLDER, 'cookies.txt')
|
|
| 32 |
yt_cookies_content = os.environ.get('YT_COOKIES')
|
| 33 |
if yt_cookies_content:
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with open(COOKIES_FILE_PATH, 'w', encoding='utf-8') as f:
|
| 36 |
-
f.write(
|
| 37 |
print("[startup] Successfully wrote cookies.txt from YT_COOKIES environment variable.")
|
| 38 |
except Exception as e:
|
| 39 |
print(f"[startup] Failed to write cookies.txt: {e}")
|
|
|
|
| 32 |
yt_cookies_content = os.environ.get('YT_COOKIES')
|
| 33 |
if yt_cookies_content:
|
| 34 |
try:
|
| 35 |
+
content_to_write = yt_cookies_content.strip()
|
| 36 |
+
# If it looks like a JSON array/object, try to convert it to Netscape format
|
| 37 |
+
if (content_to_write.startswith('[') and content_to_write.endswith(']')) or content_to_write.startswith('{'):
|
| 38 |
+
import json
|
| 39 |
+
try:
|
| 40 |
+
cookies_list = json.loads(content_to_write)
|
| 41 |
+
if isinstance(cookies_list, list):
|
| 42 |
+
netscape_lines = [
|
| 43 |
+
"# Netscape HTTP Cookie File",
|
| 44 |
+
"# http://curl.haxx.se/rfc/cookie_spec.html",
|
| 45 |
+
"# This is a generated file! Do not edit.\n"
|
| 46 |
+
]
|
| 47 |
+
for c in cookies_list:
|
| 48 |
+
if isinstance(c, dict):
|
| 49 |
+
domain = c.get('domain', '')
|
| 50 |
+
flag = "TRUE" if domain.startswith('.') or not c.get('hostOnly', True) else "FALSE"
|
| 51 |
+
path = c.get('path', '/')
|
| 52 |
+
secure = "TRUE" if c.get('secure', False) else "FALSE"
|
| 53 |
+
try:
|
| 54 |
+
expiry = str(int(float(c.get('expirationDate', 0))))
|
| 55 |
+
except (ValueError, TypeError):
|
| 56 |
+
expiry = "0"
|
| 57 |
+
name = c.get('name', '')
|
| 58 |
+
value = c.get('value', '')
|
| 59 |
+
netscape_lines.append(f"{domain}\t{flag}\t{path}\t{secure}\t{expiry}\t{name}\t{value}")
|
| 60 |
+
content_to_write = "\n".join(netscape_lines)
|
| 61 |
+
print("[startup] Successfully parsed JSON cookies and converted to Netscape format.")
|
| 62 |
+
except Exception as json_err:
|
| 63 |
+
print(f"[startup] Failed to parse JSON cookies (treating as raw Netscape format): {json_err}")
|
| 64 |
+
|
| 65 |
with open(COOKIES_FILE_PATH, 'w', encoding='utf-8') as f:
|
| 66 |
+
f.write(content_to_write)
|
| 67 |
print("[startup] Successfully wrote cookies.txt from YT_COOKIES environment variable.")
|
| 68 |
except Exception as e:
|
| 69 |
print(f"[startup] Failed to write cookies.txt: {e}")
|