import ssl, urllib.request, re, sys, io
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 fetch(u):
    req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
    return urllib.request.urlopen(req, context=ctx, timeout=25).read().decode("utf-8", "ignore")

def text(h):
    h = re.sub(r'<script.*?</script>', ' ', h, flags=re.S|re.I)
    h = re.sub(r'<style.*?</style>', ' ', h, flags=re.S|re.I)
    h = re.sub(r'<[^>]+>', ' ', h)
    h = re.sub(r'&nbsp;', ' ', h); h = re.sub(r'&amp;', '&', h); h = re.sub(r'&#39;', "'", h); h = re.sub(r'&#8217;', "'", h)
    h = re.sub(r'\s+', ' ', h)
    return h.strip()

urls = [
 "https://socialsciences.uchicago.edu/admissions/deadlines-decisions",
 "https://socialsciences.uchicago.edu/admissions/application-materials/english-proficiency-requirements",
 "https://grad.uchicago.edu/admissions/apply/english-language-requirements/required-scores/",
 "https://socialsciences.uchicago.edu/admissions/estimated-cost-attendance",
]
for u in urls:
    try:
        h = fetch(u); t = text(h)
        print("="*25, u, "len=", len(h), "textlen=", len(t))
        chunks = re.split(r'(?<=[.!?])\s+', t)
        kw = re.compile(r'(deadline|January|February|March|November|December|October|Autumn|Spring|Round|TOEFL|IELTS|minimum|\d{2}\b|score|tuition|\$\d|quarter|cost of attendance|fee)', re.I)
        seen=set()
        for c in chunks:
            if kw.search(c) and c not in seen and len(c)<400:
                seen.add(c); print("-", c.strip())
        print()
    except Exception as e:
        print("ERR", u, type(e).__name__, str(e)[:120])
