sunnymh-manga-dl/export_cookies.py
2026-04-11 16:55:13 +08:00

93 lines
2.8 KiB
Python

"""
Opens a browser to m.happymh.com, waits for you to pass Cloudflare,
then saves cookies to cookies.txt in Netscape format.
Install:
pip install playwright
playwright install chromium
Usage:
python export_cookies.py
"""
import time
from pathlib import Path
try:
from playwright.sync_api import sync_playwright
except ImportError:
print("Playwright not installed. Run:")
print(" pip install playwright")
print(" playwright install chromium")
raise SystemExit(1)
COOKIES_FILE = Path(__file__).parent / "cookies.txt"
TARGET_URL = "https://m.happymh.com"
def cookies_to_netscape(cookies):
"""Convert Playwright cookies to Netscape cookies.txt format."""
lines = ["# Netscape HTTP Cookie File", ""]
for c in cookies:
domain = c["domain"]
# Netscape format: leading dot means accessible to subdomains
if not domain.startswith("."):
domain = "." + domain
flag = "TRUE" # accessible to subdomains
path = c.get("path", "/")
secure = "TRUE" if c.get("secure", False) else "FALSE"
expires = str(int(c.get("expires", 0)))
name = c["name"]
value = c["value"]
lines.append(f"{domain}\t{flag}\t{path}\t{secure}\t{expires}\t{name}\t{value}")
return "\n".join(lines) + "\n"
def main():
print("Opening browser to m.happymh.com...")
print("Once the page loads (past Cloudflare), press ENTER here to save cookies.\n")
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context(
user_agent=(
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) "
"Version/16.0 Mobile/15E148 Safari/604.1"
),
viewport={"width": 390, "height": 844},
is_mobile=True,
)
page = context.new_page()
page.goto(TARGET_URL)
input(">>> Page opened. Pass Cloudflare if needed, then press ENTER to save cookies... ")
cookies = context.cookies()
if not cookies:
print("No cookies found!")
browser.close()
return
# Check for cf_clearance
cookie_names = [c["name"] for c in cookies]
if "cf_clearance" in cookie_names:
print("cf_clearance cookie found (Cloudflare passed)")
else:
print("Warning: cf_clearance not found. You may still be on the challenge page.")
answer = input("Save anyway? [y/N] ").strip().lower()
if answer != "y":
browser.close()
return
text = cookies_to_netscape(cookies)
COOKIES_FILE.write_text(text)
print(f"\nSaved {len(cookies)} cookies to {COOKIES_FILE}")
browser.close()
if __name__ == "__main__":
main()