# -*- coding: utf-8 -*-
import urllib.request, ssl, sys, io, re, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
SCTX = ssl.create_default_context(); SCTX.check_hostname=False; SCTX.verify_mode=ssl.CERT_NONE
urls = [
    ("GT_SEEM", "https://catalog.gatech.edu/programs/sustainable-energy-environmental-management-ms/"),
    ("NYU_STAT", "https://bulletins.nyu.edu/graduate/arts-science/programs/applied-statistics-social-science-research-ms/"),
    ("UNC_AS", "https://catalog.unc.edu/graduate/schools-departments/asian-american-identity/"),
    ("USC_PHDS", "https://catalog.unc.edu/graduate/schools-departments/philosophy/"),
]
for name, u in urls:
    try:
        req = urllib.request.Request(u, headers={"User-Agent":"Mozilla/5.0"})
        with urllib.request.urlopen(req, timeout=12, context=SCTX) as r:
            body = r.read().decode("utf-8", errors="ignore")
        body = re.sub(r"<script[\s\S]*?</script>", " ", body, flags=re.I)
        body = re.sub(r"<style[\s\S]*?</style>", " ", body, flags=re.I)
        text = re.sub(r"<[^>]+>", " ", body)
        text = html.unescape(text); text = re.sub(r"\s+", " ", text).strip()
        print("="*70); print(name, r.status, len(body), u)
        # find program description
        m = re.search(r"(Program Description|Overview|The Master|degree program)", text, re.I)
        if m: print(text[m.start():m.start()+1200])
        else: print(text[:1200])
    except Exception as e:
        print(name, "ERR", type(e).__name__, str(e)[:80], u)
