import requests, sys, re, os, hashlib
H = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.9",
}
out = "_r374_txt"
os.makedirs(out, exist_ok=True)
for u in sys.argv[1:]:
    try:
        r = requests.get(u, headers=H, timeout=25)
        slug = re.sub(r"https?://", "", u)
        slug = re.sub(r"[^a-zA-Z0-9]+", "-", slug)[:80]
        txt = r.text
        txt = re.sub(r"(?is)<(script|style)[^>]*>.*?</\1>", " ", txt)
        txt = re.sub(r"<[^>]+>", " ", txt)
        txt = txt.replace("&nbsp;", " ").replace("&amp;", "&").replace("&#8217;", "'").replace("&rsquo;", "'")
        txt = re.sub(r"[ \t]+", " ", txt)
        txt = re.sub(r"\n\s*\n+", "\n", txt)
        p = os.path.join(out, slug + ".txt")
        open(p, "w", encoding="utf-8").write(txt)
        print(r.status_code, len(r.text), len(txt), p)
    except Exception as e:
        print("ERR", type(e).__name__, u)
