# -*- coding: utf-8 -*-
import io, sys, os, json, urllib.request, ssl, re, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
todo={x["det"]:x for x in json.load(open("todo_upgrade.json",encoding="utf-8"))}
def get(url):
    req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
    r=urllib.request.urlopen(req,timeout=15,context=ctx); return r.read()
def clean(b):
    t=b.decode("utf-8","ignore")
    t=re.sub(r'<script[\s\S]*?</script>',' ',t); t=re.sub(r'<style[\s\S]*?</style>',' ',t)
    t=re.sub(r'<[^>]+>',' ',t); t=html.unescape(t); return re.sub(r'\s+',' ',t)
# AM html links
x=todo["details/usc-ca09c65.html"]
b=get(x["url"]); open("_usc5_am.html","wb").write(b)
print("--- AM math grad links ---")
for m in re.finditer(r'href="([^"]+)"',b.decode("utf-8","ignore")):
    u=html.unescape(m.group(1))
    if "mathematics" in u.lower() and any(k in u.lower() for k in ["degree","admiss","master","requirement","program"]):
        print(u)
# comds academics + grad applicants
for tag,url in [("comds2","https://annenberg.usc.edu/academics/communication-data-science-ms"),("annadm","https://annenberg.usc.edu/admissions/graduate-applicants")]:
    try:
        b=get(url); open(f"_usc5_{tag}.txt","w",encoding="utf-8").write(clean(b)); print(tag,"200",len(b))
    except Exception as e:
        print(tag,"ERR",type(e).__name__,str(e)[:50])
