# -*- coding: utf-8 -*-
import io,sys,ssl,urllib.request,re,json,os
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding="utf-8")
b=r"D:\work buddy\2026-11-05-14-15-52"
todo=json.load(open(os.path.join(b,"todo_upgrade.json"),encoding="utf-8"))
rows=todo if isinstance(todo,list) else todo.get("items",todo.get("todo",[]))
url=None
for x in rows:
    if x.get("det")=="details/uw-c034479.html":
        url=x.get("url"); print("TODO URL:",url)
if not url:
    url="https://uwaterloo.ca/future-graduate-students/programs/by-faculty/engineering/systems-design-engineering-master-engineering-meng"
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0"})
try:
    html=urllib.request.urlopen(req,timeout=25,context=ctx).read().decode("utf-8","ignore")
    print("LEN",len(html))
    txt=re.sub(r"<script.*?</script>","",html,flags=re.S)
    txt=re.sub(r"<style.*?</style>","",txt,flags=re.S)
    txt=re.sub(r"<[^>]+>"," ",txt); txt=re.sub(r"\s+"," ",txt).strip()
    print("TEXT",len(txt))
    def seg(a,bb,label):
        i=txt.find(a); j=txt.find(bb,i+len(a)) if bb else len(txt)
        print("\n=== "+label+" ==="); print(txt[i:j if i>=0 else 0][:800] if i>=0 else "NOT FOUND: "+a)
    seg("Department/School","Application deadlines","概览")
    seg("Application deadlines","Application materials","截止")
    seg("Application materials","Admission requirements","材料")
    seg("Admission requirements","English language proficiency","录取")
    seg("Specialization options","Program overview","专项")
    for m in re.finditer("Co-op",txt):
        i=m.start(); print("\n[Co-op]",txt[max(0,i-70):i+110])
except Exception as e:
    print("ERR",type(e).__name__,e)
