# -*- 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_extra = [
    ("UT Astronomy MSc", "https://sgs.utoronto.ca/programs/astronomy-astrophysics/"),
    ("UT Applied Computing MScAC", "https://sgs.utoronto.ca/programs/applied-computing/"),
    ("UT Industrial Relations & HR MIRHR", "https://sgs.utoronto.ca/programs/industrial-relations-human-resources/"),
    ("UT Information Studies MISt", "https://sgs.utoronto.ca/programs/information-studies/"),
    ("UT Journalism MJ", "https://sgs.utoronto.ca/programs/journalism/"),
    ("UT Biomedical Communication MSc", "https://sgs.utoronto.ca/programs/biomedical-communication/"),
    ("UT Management & Professional Accounting", "https://sgs.utoronto.ca/programs/management-and-professional-accounting/"),
]

for name, u in ut_extra:
    s,n,h = fetch(u)
    print("===", name, s, n)
    if s!=200: continue
    t = clean(h)
    m = re.search(r'Quick Facts([\s\S]{0,1500})', t)
    if m:
        qf = re.sub(r'\s+',' ',m.group(1))[:500]
        print("  QF:", qf)
    for kw in ["Master of","MSc","MA","MISt","MJ","MIRHR","MScAC","credits","STEM","GRE","TOEFL","IELTS","deadline","minimum admission","on-campus","online","full-time"]:
        m2 = re.search(r".{0,40}"+re.escape(kw)+r".{0,100}", t, re.I)
        if m2: print("  ",kw,"→",m2.group(0).strip()[:160])
    print()
