Commit ·
413a6af
1
Parent(s): 0224158
Create Dockerfile
Browse files- app.py +1 -1
- convert_cookie_for_youtube.py +103 -0
- convert_raw_cookie_string_to_netscape.py +0 -39
- test.py +12 -38
app.py
CHANGED
|
@@ -43,7 +43,7 @@ async def save_cookies(request: Request):
|
|
| 43 |
|
| 44 |
# Validate structure
|
| 45 |
if "data" in data and "raw_cookies_string" in data and "source_url" in data:
|
| 46 |
-
save_data["
|
| 47 |
"session_data": data["data"],
|
| 48 |
"raw_cookies": data["raw_cookies_string"],
|
| 49 |
"source_url": data["source_url"]
|
|
|
|
| 43 |
|
| 44 |
# Validate structure
|
| 45 |
if "data" in data and "raw_cookies_string" in data and "source_url" in data:
|
| 46 |
+
save_data["youtube"] = {
|
| 47 |
"session_data": data["data"],
|
| 48 |
"raw_cookies": data["raw_cookies_string"],
|
| 49 |
"source_url": data["source_url"]
|
convert_cookie_for_youtube.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from urllib.parse import urlparse
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
from test import get_cookies
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def infer_base_domain(original_source_url):
|
| 8 |
+
"""
|
| 9 |
+
Infers a correct base domain for YouTube/Google cookies from a
|
| 10 |
+
potentially problematic original_source_url.
|
| 11 |
+
Prioritizes common YouTube/Google domains.
|
| 12 |
+
"""
|
| 13 |
+
if "youtube.com" in original_source_url:
|
| 14 |
+
return "youtube.com"
|
| 15 |
+
elif "google.com" in original_source_url:
|
| 16 |
+
return "google.com"
|
| 17 |
+
elif "youtube.com" in original_source_url: # Catches other YouTube domains if any
|
| 18 |
+
return "youtube.com"
|
| 19 |
+
elif "googleusercontent.com" in original_source_url: # Generic googleusercontent fallback
|
| 20 |
+
return "googleusercontent.com"
|
| 21 |
+
|
| 22 |
+
# Fallback if no specific match, but this might still give bad results
|
| 23 |
+
# if the original_source_url itself is completely unrecognizable.
|
| 24 |
+
parsed_url = urlparse(original_source_url)
|
| 25 |
+
if parsed_url.hostname:
|
| 26 |
+
return parsed_url.hostname
|
| 27 |
+
return "unknown.com" # Should ideally not be hit for YouTube/Google cookies
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def convert_raw_cookie_string_to_netscape_fixed_domain(raw_cookie_str, original_source_url):
|
| 31 |
+
"""
|
| 32 |
+
Converts a raw cookie string to Netscape format, inferring a correct
|
| 33 |
+
base domain for YouTube/Google cookies.
|
| 34 |
+
"""
|
| 35 |
+
base_domain = infer_base_domain(original_source_url)
|
| 36 |
+
|
| 37 |
+
lines = [
|
| 38 |
+
"# Netscape HTTP Cookie File",
|
| 39 |
+
"# http://curl.haxx.se/rfc/cookie_spec.html",
|
| 40 |
+
"# This file was generated by script - Domain inferred for YouTube/Google compatibility."
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
for raw_cookie in raw_cookie_str.split(';'):
|
| 44 |
+
cookie = raw_cookie.strip().split('=', 1)
|
| 45 |
+
if len(cookie) != 2:
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
name, value = cookie[0], cookie[1]
|
| 49 |
+
|
| 50 |
+
# Determine domain field. Use a leading dot for general cookies.
|
| 51 |
+
# This handles the . prefix for subdomains correctly.
|
| 52 |
+
domain_field = f".{base_domain}"
|
| 53 |
+
|
| 54 |
+
# Heuristics for HttpOnly based on common cookie prefixes
|
| 55 |
+
# Note: True HttpOnly status isn't available from raw string, this is a guess.
|
| 56 |
+
is_httponly = name.startswith("__Secure-") or name.startswith("__Host-") or name.startswith(
|
| 57 |
+
"SID") or name.startswith("SSID") or name.startswith("LSID")
|
| 58 |
+
|
| 59 |
+
if is_httponly:
|
| 60 |
+
# Netscape format for HttpOnly is #HttpOnly_.domain
|
| 61 |
+
# Ensure we don't duplicate the leading dot if it's already there from base_domain
|
| 62 |
+
if domain_field.startswith("."):
|
| 63 |
+
domain_field = f"#HttpOnly_{domain_field}"
|
| 64 |
+
else:
|
| 65 |
+
domain_field = f"#HttpOnly_.{domain_field}"
|
| 66 |
+
|
| 67 |
+
# The 'flag' column (Include Subdomains) is generally TRUE for Netscape format.
|
| 68 |
+
include_subdomains = "TRUE"
|
| 69 |
+
|
| 70 |
+
# The 'path' column is usually '/' for general site cookies.
|
| 71 |
+
path = "/"
|
| 72 |
+
|
| 73 |
+
# The 'secure' column. Guess based on __Secure- prefix, but better if known from source.
|
| 74 |
+
secure = "TRUE" if name.startswith("__Secure-") else "FALSE"
|
| 75 |
+
|
| 76 |
+
# Expiration: A fixed, far-future date is often sufficient for yt-dlp.
|
| 77 |
+
expires = str(int(time.time()) + 180 * 24 * 3600) # 180 days from now
|
| 78 |
+
|
| 79 |
+
# Construct the line
|
| 80 |
+
line = f"{domain_field}\t{include_subdomains}\t{path}\t{secure}\t{expires}\t{name}\t{value}"
|
| 81 |
+
lines.append(line)
|
| 82 |
+
|
| 83 |
+
return lines
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# --- Main Execution Block (adapt to your API structure) ---
|
| 87 |
+
# Assuming 'cookie_entry' is received at your API endpoint
|
| 88 |
+
# For testing purposes, using your provided cookie_entry_data:
|
| 89 |
+
# from test import get_cookies # Uncomment if get_cookies is defined elsewhere
|
| 90 |
+
|
| 91 |
+
cookie_entry_data = get_cookies()
|
| 92 |
+
|
| 93 |
+
raw_str = cookie_entry_data['youtube']['raw_cookies']
|
| 94 |
+
source = cookie_entry_data['youtube']['source_url']
|
| 95 |
+
|
| 96 |
+
# Convert to Netscape format
|
| 97 |
+
output_lines = convert_raw_cookie_string_to_netscape_fixed_domain(raw_str, source)
|
| 98 |
+
|
| 99 |
+
# Save to file (this would happen on your API server)
|
| 100 |
+
with open("cookies.txt", "w", encoding="utf-8") as f:
|
| 101 |
+
f.write("\n".join(output_lines))
|
| 102 |
+
|
| 103 |
+
print("✅ Saved as cookies.txt in Netscape format with inferred domains.")
|
convert_raw_cookie_string_to_netscape.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
| 1 |
-
from urllib.parse import urlparse
|
| 2 |
-
import time
|
| 3 |
-
|
| 4 |
-
import requests
|
| 5 |
-
|
| 6 |
-
from test import API_ENDPOINT_GET, get_cookies
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def convert_raw_cookie_string_to_netscape(raw_cookie_str, source_url):
|
| 10 |
-
domain = urlparse(source_url).hostname
|
| 11 |
-
lines = []
|
| 12 |
-
for cookie in raw_cookie_str.split(';'):
|
| 13 |
-
name_value = cookie.strip().split('=', 1)
|
| 14 |
-
if len(name_value) != 2:
|
| 15 |
-
continue
|
| 16 |
-
name, value = name_value
|
| 17 |
-
# Netscape format: domain \t include_subdomains \t path \t secure \t expires \t name \t value
|
| 18 |
-
include_subdomains = "TRUE"
|
| 19 |
-
path = "/"
|
| 20 |
-
secure = "FALSE"
|
| 21 |
-
expires = str(int(time.time()) + 3600 * 24 * 180) # 180 days from now
|
| 22 |
-
line = f"{domain}\t{include_subdomains}\t{path}\t{secure}\t{expires}\t{name}\t{value}"
|
| 23 |
-
lines.append(line)
|
| 24 |
-
return lines
|
| 25 |
-
|
| 26 |
-
cookie_data_list = get_cookies()
|
| 27 |
-
|
| 28 |
-
# Build full Netscape-format text
|
| 29 |
-
output_lines = ["# Netscape HTTP Cookie File", "# Generated by script"]
|
| 30 |
-
for cookie_entry in cookie_data_list:
|
| 31 |
-
raw_str = cookie_entry['raw_cookies_string']
|
| 32 |
-
source = cookie_entry['source_url']
|
| 33 |
-
output_lines.extend(convert_raw_cookie_string_to_netscape(raw_str, source))
|
| 34 |
-
|
| 35 |
-
# Save to file
|
| 36 |
-
with open("cookies.txt", "w") as f:
|
| 37 |
-
f.write("\n".join(output_lines))
|
| 38 |
-
|
| 39 |
-
print("✅ Saved as cookies.txt in Netscape format.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test.py
CHANGED
|
@@ -1,13 +1,7 @@
|
|
| 1 |
import requests
|
| 2 |
-
import json
|
| 3 |
|
| 4 |
BASE_URL = "https://amit0987-cookies-recever-api.hf.space"
|
| 5 |
-
API_ENDPOINT_POST = f"{BASE_URL}/save-cookies"
|
| 6 |
-
API_ENDPOINT_GET = f"{BASE_URL}/cookies"
|
| 7 |
|
| 8 |
-
HEADERS = {"Content-Type": "application/json"}
|
| 9 |
-
|
| 10 |
-
# Sample test payload
|
| 11 |
sample_data = {
|
| 12 |
"data": {
|
| 13 |
"session": "test_session_123_from_script",
|
|
@@ -18,43 +12,23 @@ sample_data = {
|
|
| 18 |
"source_url": "https://script-test.com"
|
| 19 |
}
|
| 20 |
|
| 21 |
-
def post_cookies(
|
| 22 |
-
print(f"\n--- Sending POST to {API_ENDPOINT_POST} ---")
|
| 23 |
try:
|
| 24 |
-
response = requests.post(
|
| 25 |
response.raise_for_status()
|
| 26 |
-
print("
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
print(f"❌ HTTP Error: {e.response.status_code} - {e.response.text}")
|
| 30 |
-
except requests.exceptions.RequestException as e:
|
| 31 |
-
print(f"❌ Request Failed: {e}")
|
| 32 |
-
return None
|
| 33 |
|
| 34 |
def get_cookies():
|
| 35 |
-
print(f"\n--- Fetching with GET from {API_ENDPOINT_GET} ---")
|
| 36 |
try:
|
| 37 |
-
response = requests.get(
|
| 38 |
response.raise_for_status()
|
| 39 |
-
print("
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
print("⚠️ Failed to decode JSON from GET response.")
|
| 44 |
-
except requests.exceptions.RequestException as e:
|
| 45 |
-
print(f"❌ Request Failed: {e}")
|
| 46 |
-
return None
|
| 47 |
-
|
| 48 |
-
def main():
|
| 49 |
-
post_resp = post_cookies(sample_data)
|
| 50 |
-
if post_resp:
|
| 51 |
-
print("🔍 POST Response:", post_resp.text)
|
| 52 |
-
|
| 53 |
-
stored_data = get_cookies()
|
| 54 |
-
if stored_data:
|
| 55 |
-
print("📦 Stored Cookies:", json.dumps(stored_data, indent=2))
|
| 56 |
-
else:
|
| 57 |
-
print("⚠️ No cookies retrieved or data might be empty.")
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
|
|
|
|
|
|
| 1 |
import requests
|
|
|
|
| 2 |
|
| 3 |
BASE_URL = "https://amit0987-cookies-recever-api.hf.space"
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
sample_data = {
|
| 6 |
"data": {
|
| 7 |
"session": "test_session_123_from_script",
|
|
|
|
| 12 |
"source_url": "https://script-test.com"
|
| 13 |
}
|
| 14 |
|
| 15 |
+
def post_cookies():
|
|
|
|
| 16 |
try:
|
| 17 |
+
response = requests.post(f"{BASE_URL}/save-cookies", json=sample_data, timeout=10)
|
| 18 |
response.raise_for_status()
|
| 19 |
+
print("[POST] ✅", response.json())
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print("[POST] ❌", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def get_cookies():
|
|
|
|
| 24 |
try:
|
| 25 |
+
response = requests.get(f"{BASE_URL}/cookies", timeout=10)
|
| 26 |
response.raise_for_status()
|
| 27 |
+
print("[GET] ✅", response.json())
|
| 28 |
+
return response.json()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print("[GET] ❌", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
+
# post_cookies()
|
| 34 |
+
get_cookies()
|