# -*- coding: utf-8 -*-
import os, re, sys, hashlib
base = r"D:\work buddy\2026-11-05-14-15-52"
cache = os.path.join(base, "cache")
out = os.path.join(cache, "_r351_txt")
os.makedirs(out, exist_ok=True)
for u in sys.argv[1:]:
    key = "r349-" + re.sub(r'[^A-Za-z0-9]+', '-', u.replace("https://", "").replace("http://", ""))[:60] + "-" + hashlib.md5(u.encode()).hexdigest()[:6]
    p = os.path.join(cache, key + ".html")
    if not os.path.exists(p):
        print("MISS", u)
        continue
    txt = open(p, encoding="utf-8", errors="ignore").read()
    plain = re.sub(r'<script.*?</script>|<style.*?</style>', ' ', txt, flags=re.S | re.I)
    plain = re.sub(r'</(p|div|li|tr|h\d|td|th)>', '\n', plain, flags=re.I)
    plain = re.sub(r'<[^>]+>', ' ', plain)
    plain = plain.replace('&nbsp;', ' ').replace('&amp;', '&')
    plain = re.sub(r'[ \t]+', ' ', plain)
    plain = re.sub(r'\n\s*\n+', '\n', plain).strip()
    slug = re.sub(r'[^A-Za-z0-9]+', '-', u.split("//")[1])[:50] + "-" + hashlib.md5(u.encode()).hexdigest()[:6]
    o = os.path.join(out, slug + ".txt")
    open(o, "w", encoding="utf-8").write(u + "\n\n" + plain)
    print(o, len(plain))
