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

# 1. 提取 UT Language and Literacies / Social Justice Education Quick Facts
print("=== UT remaining OISE ===")
for slug, label in [("language-and-literacies", "Language and Literacies Education"),
                    ("social-justice-education", "Social Justice Education")]:
    url = f"https://www.sgs.utoronto.ca/programs/{slug}/"
    html = fetch(url)
    qi = html.find("Quick Facts")
    if qi > 0:
        chunk = html[qi:qi+5000]
        rows = re.findall(r"<td class='[^']*'>([^<]+)</td>\s*<td class='[^']*'>(.*?)</td>", chunk, re.S)
        print(f"\n--- {label} ---")
        for k,v in rows[:8]:
            k2 = k.strip()
            v2 = re.sub(r"<[^>]+>", " ", v).strip()
            v2 = re.sub(r"\s+", " ", v2)
            if k2: print(f"  {k2}: {v2[:120]}")

# 2. 重探 JHU e-catalogue AAP 索引页找正确路径
print("\n=== JHU AAP index ===")
try:
    idx_html = fetch("https://e-catalogue.jhu.edu/arts-sciences/advanced-academic-programs/programs/")
    # 找所有项目链接
    links = re.findall(r'href="([^"]*advanced-academic-programs/programs/[^"]+)"[^>]*>([^<]+)</a>', idx_html)
    print(f"found {len(links)} links")
    for href, text in links:
        if any(k in text.lower() for k in ["bioethic","international","study","writing","communication","energy","environment"]):
            print(f"  {text.strip()}: {href}")
except Exception as e:
    print("JHU index FAIL:", str(e)[:80])
