# -*- coding: utf-8 -*-
import io, sys, re, ssl, urllib.request
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
URL = "https://graduateschool.nd.edu/degree-programs/electrical-engineering-msee---masters-traditional/"
req = urllib.request.Request(URL, headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"})
try:
    raw = urllib.request.urlopen(req, timeout=20, context=ctx).read().decode("utf-8","ignore")
    print("HTML len", len(raw))
    txt = re.sub(r"<script.*?</script>", " ", raw, flags=re.S|re.I)
    txt = re.sub(r"<style.*?</style>", " ", txt, flags=re.S|re.I)
    txt = re.sub(r"<[^>]+>", " ", txt)
    txt = re.sub(r"\s+", " ", txt).strip()
    print("TEXT len", len(txt))
    print("="*60)
    print(txt[:4500])
    print("="*60)
    for kw in ["Residency","Online","On-campus","Full-time","Part-time","credit","Credit","deadline","Deadline","GRE","TOEFL","IELTS","Duolingo","tuition","Tuition","GPA","recommendation","transcript","Statement","resume","Resume","application fee","degree","Degree"]:
        for m in re.finditer(kw, txt):
            i=m.start(); print("KW",kw,"=>", txt[max(0,i-60):i+130].strip())
            break
    links = re.findall(r'href="([^"]+)"', raw)
    links = [l for l in links if any(k in l for k in ["admiss","tuition","financial","application","degree","handbook","electrical","ee","engineering"])]
    print("LINKS:")
    for l in sorted(set(links))[:40]: print("  ", l)
except Exception as e:
    print("ERR", type(e).__name__, str(e)[:200])
