# -*- coding: utf-8 -*-
import ssl, io, sys, urllib.request, re
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

urls = [
    ("duke-gsas-econ", "https://gradschool.duke.edu/academics/programs-degrees/economics-masters/"),
    ("duke-econ-grad", "https://econ.duke.edu/graduate"),
    ("duke-econ-masters", "https://econ.duke.edu/graduate/masters-program"),
    ("duke-medphys-gsas", "https://gradschool.duke.edu/academics/programs-degrees/medical-physics-ms/"),
    ("uchicago-crown-ssl", "https://crownschool.uchicago.edu/academic-programs/curriculum-maps/social-sector-leadership-and-nonprofit-management-ssl-full-time"),
    ("harvard-gsas-cse", "https://gsas.harvard.edu/program/computational-science-and-engineering"),
    ("harvard-gsas-eas", "https://gsas.harvard.edu/program/engineering-and-applied-sciences"),
]
for tag, u in urls:
    try:
        req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
        r = urllib.request.urlopen(req, timeout=15, context=ctx)
        b = r.read()
        txt = b.decode("utf-8", "ignore")
        title = re.search(r"<title>(.*?)</title>", txt, re.S)
        t = re.sub(r"\s+", " ", title.group(1)).strip()[:70] if title else ""
        print(f"OK  {r.status} len={len(b):6} {tag:20} {u}")
        print(f"        title: {t}")
    except Exception as e:
        print(f"ERR {type(e).__name__:20} {tag:20} {u}  {str(e)[:60]}")
