| from urllib.parse import urlparse |
| import time |
|
|
| from test import get_cookies |
|
|
|
|
| def infer_base_domain(original_source_url): |
| """ |
| Infers a correct base domain for YouTube/Google cookies from a |
| potentially problematic original_source_url. |
| Prioritizes common YouTube/Google domains. |
| """ |
| if "youtube.com" in original_source_url: |
| return "youtube.com" |
| elif "google.com" in original_source_url: |
| return "google.com" |
| elif "youtube.com" in original_source_url: |
| return "youtube.com" |
| elif "googleusercontent.com" in original_source_url: |
| return "googleusercontent.com" |
|
|
| |
| |
| parsed_url = urlparse(original_source_url) |
| if parsed_url.hostname: |
| return parsed_url.hostname |
| return "unknown.com" |
|
|
|
|
| def convert_raw_cookie_string_to_netscape_fixed_domain(raw_cookie_str, original_source_url): |
| """ |
| Converts a raw cookie string to Netscape format, inferring a correct |
| base domain for YouTube/Google cookies. |
| """ |
| base_domain = infer_base_domain(original_source_url) |
|
|
| lines = [ |
| "# Netscape HTTP Cookie File", |
| "# http://curl.haxx.se/rfc/cookie_spec.html", |
| "# This file was generated by script - Domain inferred for YouTube/Google compatibility." |
| ] |
|
|
| for raw_cookie in raw_cookie_str.split(';'): |
| cookie = raw_cookie.strip().split('=', 1) |
| if len(cookie) != 2: |
| continue |
|
|
| name, value = cookie[0], cookie[1] |
|
|
| |
| |
| domain_field = f".{base_domain}" |
|
|
| |
| |
| is_httponly = name.startswith("__Secure-") or name.startswith("__Host-") or name.startswith( |
| "SID") or name.startswith("SSID") or name.startswith("LSID") |
|
|
| if is_httponly: |
| |
| |
| if domain_field.startswith("."): |
| domain_field = f"#HttpOnly_{domain_field}" |
| else: |
| domain_field = f"#HttpOnly_.{domain_field}" |
|
|
| |
| include_subdomains = "TRUE" |
|
|
| |
| path = "/" |
|
|
| |
| secure = "TRUE" if name.startswith("__Secure-") else "FALSE" |
|
|
| |
| expires = str(int(time.time()) + 180 * 24 * 3600) |
|
|
| |
| line = f"{domain_field}\t{include_subdomains}\t{path}\t{secure}\t{expires}\t{name}\t{value}" |
| lines.append(line) |
|
|
| return lines |
|
|
|
|
| |
| |
| |
| |
|
|
| cookie_entry_data = get_cookies() |
|
|
| raw_str = cookie_entry_data['youtube']['raw_cookies'] |
| source = cookie_entry_data['youtube']['source_url'] |
|
|
| |
| output_lines = convert_raw_cookie_string_to_netscape_fixed_domain(raw_str, source) |
|
|
| |
| with open("cookies.txt", "w", encoding="utf-8") as f: |
| f.write("\n".join(output_lines)) |
|
|
| print("✅ Saved as cookies.txt in Netscape format with inferred domains.") |