# -*- coding: utf-8 -*-
import urllib.request, ssl, re, io, sys, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

SCTX = ssl.create_default_context()
SCTX.check_hostname = False
SCTX.verify_mode = ssl.CERT_NONE

# NYU Wagner MPA PNM / HPM
urls = [
    ("NYU MPA PNM", "https://bulletins.nyu.edu/graduate/public-service/programs/public-nonprofit-management-policy-mpa/"),
    ("NYU MPA HPM", "https://bulletins.nyu.edu/graduate/public-service/programs/health-policy-management-mpa/"),
    ("JHU Bioethics MA", "https://e-catalogue.jhu.edu/arts-sciences/full-time-residential-programs/degree-programs/bioethics/"),
    ("JHU International Studies MA", "https://e-catalogue.jhu.edu/arts-sciences/full-time-residential-programs/degree-programs/international-studies/"),
]

for name, url in urls:
    try:
        req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
        resp = urllib.request.urlopen(req, timeout=15, context=SCTX)
        html = resp.read().decode("utf-8", errors="ignore")
        print(f"\n=== {name} ({resp.status}, {len(html)}B) ===")
        heads = re.findall(r'<h[12][^>]*>(.*?)</h[12]>', html, re.S)
        heads = [re.sub(r'<[^>]+>', '', h).strip() for h in heads]
        heads = [h for h in heads if h and len(h) > 3]
        print("Headers:", heads[:8])
        # credits
        for kw in ["credit", "45 credit", "deadline", "TOEFL", "IELTS", "GRE", "tuition"]:
            m = re.findall(r'[^.]{0,60}' + kw + r'[^.]{0,100}', html, re.I)
            if m:
                s = re.sub(r'<[^>]+>', ' ', m[0])
                s = re.sub(r'\s+', ' ', s).strip()
                if len(s) > 15:
                    print(f"  [{kw}] {s[:130]}")
    except Exception as e:
        print(f"\n=== {name} ERR: {type(e).__name__}: {str(e)[:80]} ===")
