# -*- coding: utf-8 -*-
import urllib.request, ssl, sys, io, time
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
slugs = [
 "applied-positive-psychology-mapp",
 "government-administration-mpa",
 "international-mpa",
 "health-care-innovation-mhci",
 "public-health-mph",
]
for s in slugs:
    url = f"https://catalog.upenn.edu/graduate/programs/{s}/"
    ok = False
    for attempt in range(3):
        try:
            req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)","Referer":"https://catalog.upenn.edu/graduate/programs/","Accept":"text/html"})
            r = urllib.request.urlopen(req, timeout=25, context=SCTX)
            body = r.read().decode("utf-8","ignore")
            if r.status == 200 and len(body) > 1000:
                title = ""
                if "<title>" in body:
                    title = body.split("<title>")[1].split("</title>")[0].strip()
                print(s, r.status, len(body), "|", title[:80])
                ok = True
                break
            else:
                print(s, "attempt", attempt, "status", r.status, "len", len(body))
                time.sleep(2)
        except Exception as e:
            print(s, "attempt", attempt, "ERR", repr(e)[:120])
            time.sleep(2)
    if not ok:
        print(s, "GIVE UP")
