# -*- 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
for u in ["https://cs.uchicago.edu/academics/masters/","http://masters.cs.uchicago.edu/"]:
    try:
        req=urllib.request.Request(u,headers={"User-Agent":"Mozilla/5.0"})
        r=urllib.request.urlopen(req,context=ctx,timeout=15)
        d=r.read().decode("utf-8","ignore")
        links=re.findall(r'href="([^"]+)"',d)
        rel=sorted({x for x in links if any(k in x for k in ["master","admiss","degree","tuition","deadline","application","curricul","capp","ms"]) and "mailto" not in x})
        h=re.sub(r"<script.*?</script>"," ",d,flags=re.S|re.I); h=re.sub(r"<style.*?</style>"," ",h,flags=re.S|re.I)
        t=re.sub(r"<[^>]+>"," ",h); t=re.sub(r"\s+"," ",t)
        print("OK",r.status,u,"textlen",len(t))
        for x in rel[:30]: print("   ",x)
        print("   T:",t[:300],"\n")
    except Exception as e:
        print("FAIL",u,repr(e)[:90])
