# -*- coding: utf-8 -*-
import io, sys, ssl, urllib.request, re, html as _html
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
slugs = [
    "electrical-computer-engineering",
    "mechanical-engineering",
    "materials-science-engineering",
    "civil-engineering",
    "chemical-biomolecular-engineering",
    "applied-physical-sciences",
    "chemistry",
    "public-policy",
    "political-science",
    "communication",
]
for s in slugs:
    url = f"https://catalog.unc.edu/graduate/schools-departments/{s}/"
    try:
        req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
        r = urllib.request.urlopen(req, timeout=15, context=ctx)
        b = r.read()
        h = b.decode("utf-8", errors="ignore")
        # strip tags
        text = re.sub(r"<script[\s\S]*?</script>"," ",h,flags=re.I)
        text = re.sub(r"<style[\s\S]*?</style>"," ",text,flags=re.I)
        text = re.sub(r"<[^>]+>"," ",text)
        text = _html.unescape(text)
        text = re.sub(r"\s+"," ",text)
        # find master's related keywords
        i = text.lower().find("master")
        seg = text[max(0,i-100):i+800] if i>=0 else "(no master keyword)"
        # look for excluded markers
        excl = "terminal" in text.lower() or "does not offer" in text.lower() or "only" in text.lower()[:5000]
        print(f"=== {s} | {len(b)}B | master@{i} | excl_marker={excl}")
        print(seg[:900])
        print()
    except Exception as e:
        print(f"=== {s} | ERR {e}")
