# -*- 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=20):
    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)

candidates = [
    # Duke Quantitative Financial Economics
    ("Duke QFE", "https://gradschool.duke.edu/academics/social-sciences/economics/",
     "https://econ.duke.edu/graduate/phd-program/",
     "https://econ.duke.edu/masters/"),
    # UPenn EE MSE
    ("UPenn EE MSE", "https://catalog.upenn.edu/graduate/program-catalog/engineering-applied-science/electrical-engineering/",
     "https://catalogs.upenn.edu/grad/engineering/electrical-engineering/",
     "https://www.seas.upenn.edu/graduate/electrical-engineering/"),
    # UPenn Biomedical Informatics
    ("UPenn MSBMI", "https://catalog.upenn.edu/graduate/program-catalog/biomedical-graduate-studies/biomedical-informatics/",
     "https://www.med.upenn.edu/bigmaster/"),
    # NYU Economics MA
    ("NYU Econ MA", "https://bulletin.arts.nyu.edu/preview_program.php?catoid=18&poid=4737",
     "https://as.nyu.edu/departments/econ/graduate.html"),
    # UT Management & Innovation MMI
    ("UT MMI", "https://sgs.utoronto.ca/programs/management-and-professional-accounting/",
     "https://sgs.utoronto.ca/programs/management-innovation/"),
]

for name, *urls in candidates:
    print("===", name)
    for u in urls:
        s,n,h = fetch(u)
        print("  ", s, n, u[:90])
        if s==200 and n>1000:
            txt=re.sub(r"<script[\s\S]*?</script>"," ",h,flags=re.I)
            txt=re.sub(r"<style[\s\S]*?</style>"," ",txt,flags=re.I)
            txt=re.sub(r"<[^>]+>"," ",txt)
            txt=re.sub(r"\s+"," ",txt)
            for kw in ["Master of Science","MA in","MSE","MMI","credits","STEM","GRE","TOEFL","deadline","on-campus","online","part-time","full-time"]:
                m=re.search(r".{0,60}"+re.escape(kw)+r".{0,100}", txt, re.I)
                if m: print("     ",kw,"→",m.group(0).strip()[:160])
            break
