# -*- 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.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)
base="https://cam.uchicago.edu/academics/ms-in-computational-and-applied-mathematics"
for p in ["", "/applying-to-uchicago-cam-ms/"]:
    st,h=get(base+p); t=clean(h)
    print("\n====",p or "/")
    for kw in ["STEM","OPT","TOEFL","IELTS","Duolingo","English","tuition","fee waiver","waiver","scholarship","financial aid","quarter","credit","nine","two years","full-time","part-time","online","distance"]:
        for m in re.finditer(re.escape(kw),t):
            a=max(0,m.start()-110); b=min(len(t),m.end()+190)
            print(" -",kw,"…",t[a:b].strip()[:300]); break
# FAQ tail
st,h=get(base+"/applying-to-uchicago-cam-ms/"); t=clean(h)
print("\n=== applying tail ===")
print(t[3600:6566])
# PSD language requirements
for u in ["https://physicalsciences.uchicago.edu/academics/application-requirements",
          "https://grad.uchicago.edu/admissions/apply/english-language-requirements/"]:
    try:
        st2,h2=get(u); t2=clean(h2)
        print("\n====",u,st2,"len",len(t2))
        for kw in ["TOEFL","IELTS","90","7.0","Duolingo","minimum","English"]:
            m=re.search(re.escape(kw),t2)
            if m:
                a=max(0,m.start()-80); b=min(len(t2),m.end()+160)
                print(" -",kw,"…",t2[a:b].strip()[:240])
    except Exception as e:
        print("\n====",u,"ERR",repr(e))
