# -*- coding: utf-8 -*-
import urllib.request, ssl, re, sys, io, 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
hdr = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}

def fetch(url):
    req = urllib.request.Request(url, headers=hdr)
    r = urllib.request.urlopen(req, timeout=15, context=SCTX)
    return r.read().decode("utf-8", errors="ignore")

# JHU 三个项目页
jhu_progs = [
    ("jhu-comm", "https://e-catalogue.jhu.edu/arts-sciences/advanced-academic-programs/programs/communication-master-arts/"),
    ("jhu-energy", "https://e-catalogue.jhu.edu/arts-sciences/advanced-academic-programs/programs/energy-policy-climate-master-science/"),
    ("jhu-env", "https://e-catalogue.jhu.edu/arts-sciences/advanced-academic-programs/programs/environmental-sciences-policy-master-science/"),
]

for key, url in jhu_progs:
    try:
        html = fetch(url)
        print(f"=== {key}: 200 {len(html)} ===")
        # 提取关键信息
        # 找 credits / requirements
        credits = re.findall(r"(\d+)\s*credits?", html)
        print(f"  credits: {credits[:5]}")
        # 找 degree requirements 段
        req_i = html.find("Degree Requirements")
        if req_i > 0:
            chunk = html[req_i:req_i+1500]
            text = re.sub(r"<[^>]+>", " ", chunk)
            text = re.sub(r"\s+", " ", text).strip()
            print(f"  requirements: {text[:300]}")
        # 找 admission
        adm_i = html.find("Admissions")
        if adm_i > 0:
            chunk = html[adm_i:adm_i+800]
            text = re.sub(r"<[^>]+>", " ", chunk)
            text = re.sub(r"\s+", " ", text).strip()
            print(f"  admissions: {text[:200]}")
    except Exception as e:
        print(f"=== {key}: FAIL {str(e)[:80]} ===")
