# -*- 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_CS", "https://catalog.gatech.edu/programs/computer-science-ms/"),
    ("GT_ANALYTICS", "https://catalog.gatech.edu/programs/analytics-ms/"),
    ("GT_ME", "https://catalog.gatech.edu/programs/mechanical-engineering-ms/"),
    ("GT_BME", "https://catalog.gatech.edu/programs/biomedical-engineering-ms/"),
    ("USC_CMB", "https://www.qcb-dornsife.usc.edu/masters"),
]
for name, u in urls:
    req = urllib.request.Request(u, headers={"User-Agent":"Mozilla/5.0"})
    with urllib.request.urlopen(req, timeout=15, 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("="*80); print(name, u, "len", len(text))
    # find program description
    m = re.search(r"Program Description|Overview|degree", text, re.I)
    if m:
        print(text[max(0,m.start()-50):m.start()+1800])
    else:
        print(text[:1800])
