# -*- coding: utf-8 -*-
import io, sys, os, re, json, urllib.request, ssl, socket, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
todo=json.load(open("todo_upgrade.json",encoding="utf-8"))
pick=["nyu-cfc91bd.html","nyu-c3472a1.html","nyu-c10e6fd.html","nyu-cb71085.html","nyu-c34f620.html","nyu-c568b0a.html","nyu-cd3cb5d.html"]
def base(d): return os.path.basename(d or "")
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
socket.setdefaulttimeout(10)
hdr={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36"}
for it in todo:
    det=it.get("det","")
    if base(det) not in pick: continue
    url=it["url"]
    try:
        req=urllib.request.Request(url,headers=hdr)
        r=urllib.request.urlopen(req,context=ctx)
        b=r.read(); txt=b.decode("utf-8","ignore")
        open("_24_"+os.path.basename(det).replace(".html",".txt"),"w",encoding="utf-8").write(txt)
        plain=html.unescape(re.sub(r"<script.*?</script>","",txt,flags=re.S))
        plain=html.unescape(re.sub(r"<style.*?</style>","",plain,flags=re.S))
        plain=re.sub(r"<[^>]+>"," ",plain); plain=re.sub(r"\s+"," ",plain)
        print("\n====",det,it["deg"],it["en"],"| HTTP",r.status,"| raw",len(b),"| plain",len(plain))
        print("URL:",url)
        print(plain[:600])
    except Exception as e:
        print("\n====",det,"ERR",str(e)[:60])
