# -*- coding: utf-8 -*-
import urllib.request, ssl, gzip, io, sys, re, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
hdr={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36","Accept-Language":"en-US,en;q=0.9"}
def fetch(u, t=25):
    try:
        r=urllib.request.urlopen(urllib.request.Request(u,headers=hdr),timeout=t,context=ctx)
        b=r.read()
        if r.headers.get("Content-Encoding")=="gzip": b=gzip.decompress(b)
        return r.status, len(b), b.decode("utf-8","ignore")
    except Exception as e:
        return -1, 0, str(e)

def clean(h):
    t=re.sub(r"<script[\s\S]*?</script>"," ",h,flags=re.I)
    t=re.sub(r"<style[\s\S]*?</style>"," ",t,flags=re.I)
    t=re.sub(r"<[^>]+>"," ",t)
    return re.sub(r"\s+"," ",t)

# UT SGS 项目路径（按 slug 猜测）
ut_programs = [
    ("UT MMI Management & Innovation", "https://sgs.utoronto.ca/programs/management-innovation/"),
    ("UT MScSM Sustainability Management", "https://sgs.utoronto.ca/programs/sustainability-management/"),
    ("UT MFAcc Forensic Accounting", "https://sgs.utoronto.ca/programs/forensic-accounting/"),
    ("UT MMPA Management & Professional Accounting", "https://sgs.utoronto.ca/programs/management-and-professional-accounting/"),
    ("UT Near East Civilizations MA", "https://sgs.utoronto.ca/programs/near-eastern-civilizations/"),
]

for name, u in ut_programs:
    s,n,h = fetch(u)
    print("===", name, s, n)
    if s!=200: continue
    t = clean(h)
    # Quick Facts 表
    m = re.search(r'Quick Facts([\s\S]{0,2000})', t)
    if m:
        qf = m.group(1)[:800]
        print("  QF:", qf[:600])
    for kw in ["Master of","MSc","MA","MMI","MScSM","MFAcc","MMPA","credits","STEM","GRE","TOEFL","IELTS","deadline","domestic","international","full-time","part-time","on-campus","online","admission"]:
        m2 = re.search(r".{0,50}"+re.escape(kw)+r".{0,120}", t, re.I)
        if m2: print("  ",kw,"→",m2.group(0).strip()[:180])
    print()
