# -*- coding: utf-8 -*-
import io, sys, os, 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")
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE

targets=[
 ("ut-cheme-masc","chemical-engineering"),
 ("ut-math-msc","mathematics"),
 ("ut-biochem-msc","biochemistry"),
 ("ut-mgmt-mmgt","master-of-management"),
 ("ut-hpme-msc","health-policy-management-and-evaluation"),
]
for tid,slug in targets:
    url=f"https://www.sgs.utoronto.ca/programs/{slug}/"
    req=urllib.request.Request(url,headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0"})
    raw=urllib.request.urlopen(req,timeout=20,context=ctx).read().decode("utf-8","ignore")
    open(f"_38_{slug}.html","w",encoding="utf-8").write(raw)
    # extract Quick Facts block
    m=re.search(r'Quick Facts(.*?)(?:Research Opportunities|Application Requirements|Program Description|Faculty)', raw, re.S|re.I)
    block=m.group(1) if m else raw
    # strip tags
    text=re.sub(r"<[^>]+>"," | ",block)
    text=html.unescape(text)
    text=re.sub(r"\s+\|"," |",text)
    text=re.sub(r"\|\s+","|",text)
    text=re.sub(r"[ \t]+"," ",text)
    text=re.sub(r"\n+","\n",text)
    print("="*60)
    print(tid, slug, len(raw))
    # print relevant lines
    for line in text.split("\n"):
        if any(k in line.lower() for k in ["degree","duration","program","degree","language","test","gpa","average","minimum","start","entry","cost","tuition","international","canadian","master","phd","engineer","science","master","mail","application","deadline"]):
            s=line.strip()
            if 3<len(s)<400:
                print(" ",s)
