# -*- 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://ee.nd.edu/graduate/m-s-in-electrical-engineering-msee/"
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[:5000])
    print("="*60)
    for kw in ["credit","Credit","thesis","Thesis","coursework","Online","online","part-time","Part-time","full-time","Full-time","concentration","Circuit","Materials","Devices","Systems","tuition","Tuition","deadline","Deadline","GRE","TOEFL","IELTS","GPA"]:
        for m in re.finditer(kw, txt):
            i=m.start(); print("KW",kw,"=>", txt[max(0,i-50):i+120].strip())
            break
    links = re.findall(r'href="([^"]+)"', raw)
    links = [l for l in links if any(k in l for k in ["graduate","admiss","tuition","handbook","m-s","master","degree","brochure"])]
    print("LINKS:")
    for l in sorted(set(links))[:40]: print("  ", l)
except Exception as e:
    print("ERR", type(e).__name__, str(e)[:200])
