# -*- coding: utf-8 -*-
import io,sys,ssl,urllib.request,re
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
def get(u):
    req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
    with urllib.request.urlopen(req,timeout=25,context=ctx) as r:
        return r.status,r.geturl(),r.read().decode("utf-8","replace")
def clean(h):
    t=re.sub(r"<script[\s\S]*?</script>","",h)
    t=re.sub(r"<style[\s\S]*?</style>","",t)
    t=re.sub(r"<[^>]+>"," ",t); return re.sub(r"\s+"," ",t)
cands=[
 "https://cam.uchicago.edu/",
 "https://cam.uchicago.edu/academics/ms-in-computational-and-applied-mathematics/",
 "https://cam.uchicago.edu/academics/",
]
for u in cands:
    try:
        st,final,h=get(u); t=clean(h)
        print("\n====",u,st,"final",final,"len",len(t))
        print("TITLE:",(re.search(r"<title>(.*?)</title>",h,re.S).group(1).strip() if re.search(r"<title>(.*?)</title>",h,re.S) else ""))
        for kw in ["Master of Science","MS in","deadline","GRE","TOEFL","IELTS","tuition","credit","STEM","full-time","part-time","application fee","recommendation"]:
            m=re.search(re.escape(kw),t)
            if m:
                a=max(0,m.start()-90); b=min(len(t),m.end()+180)
                print(" -",kw,"…",t[a:b].strip()[:260])
        if u.endswith("/"):
            print("\n -- links --")
            for mm in re.findall(r'href="([^"]+)"',h):
                if any(x in mm.lower() for x in ["ms","master","graduate","admiss","program"]):
                    print("   ",mm)
    except Exception as e:
        print("\n====",u,"ERR",repr(e))
