# -*- coding: utf-8 -*-
import io, sys, os, json, ssl, urllib.request, re, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
cands={c["det"]:c for c in json.load(open("_cands25.json",encoding="utf-8"))}
targets=[
 "details/usc-c531fe9.html","details/usc-cac5b60.html","details/usc-ccfb8b7.html","details/usc-c8eee33.html",
 "details/uw-c2f6425.html","details/wustl-stats-data-science-polsci-conc-ma.html",
 "details/geo-cybersecurity-risk-management-ms.html","details/cmu-c46c856.html",
 "details/cornell-c4ed07c.html","details/cornell-c7c652f.html","details/cornell-c3b1b9a.html",
]
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
for det in targets:
    c=cands.get(det)
    if not c: print("MISS",det); continue
    url=c["url"]; tag=os.path.basename(det).replace(".html","")
    try:
        req=urllib.request.Request(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'})
        b=urllib.request.urlopen(req,timeout=10,context=ctx).read()
        raw=b.decode('utf-8',errors='replace')
        open(f"_27_{tag}.txt","w",encoding="utf-8").write(raw)
        t=re.sub(r'<script[\s\S]*?</script>',' ',raw); t=re.sub(r'<style[\s\S]*?</style>',' ',t)
        t=html.unescape(re.sub(r'\s+',' ',re.sub(r'<[^>]+>',' ',t))).strip()
        print(f'{tag:38} {len(b):7}B textlen {len(t)} | {c["en"][:30]}')
    except Exception as e:
        print(f'{tag:38} FAIL {type(e).__name__} {e}')
