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

slugs = ["education", "nursing", "music-performance", "global-professional-law", "journalism",
         "management-innovation", "information-studies", "biostatistics", "epidemiology",
         "marketing", "physiotherapy", "mechanical-engineering", "electrical-computer-engineering",
         "astronomy-astrophysics", "industrial-relations-human-resources", "near-eastern-civilizations"]

# 已录 det 列表（用于去重）
idx = open("index_main.html", encoding="utf-8").read()
prog_seg = idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
catd_seg = idx[idx.find("const CATD="):idx.find("const VERIFIED_DETAILS=")]
existing_dets = set(re.findall(r'details/([a-z0-9\-]+\.html)', prog_seg + catd_seg))

results = []
for slug in slugs:
    url = f"https://www.sgs.utoronto.ca/programs/{slug}/"
    try:
        req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
        resp = urllib.request.urlopen(req, timeout=12, context=SCTX)
        html = resp.read().decode("utf-8", errors="ignore")
        size = len(html)
        # 提取 Quick Facts 里的学位
        deg_match = re.findall(r'<td[^>]*>(M[^\s<]{1,20}|PhD|EdD|MA|MSc|MEng|MASc|MMus|MMin|MN|MPH|MSW|LLM|MBA)</td>', html)
        # 提取 title
        title_m = re.search(r'<h1[^>]*>(.*?)</h1>', html, re.S)
        title = re.sub(r'<[^>]+>', '', title_m.group(1)).strip() if title_m else "?"
        # 找该 slug 对应的 det（看是否已在 PROGRAMS/CATD）
        results.append({"slug": slug, "status": resp.status, "size": size, "title": title[:60], "degs": list(set(deg_match))[:8]})
        print(f"200 {slug:35s} {size:>7d}B | {title[:50]} | degs: {list(set(deg_match))[:6]}")
    except Exception as e:
        print(f"ERR {slug:35s} {type(e).__name__}: {str(e)[:60]}")
        results.append({"slug": slug, "error": str(e)[:80]})

json.dump(results, open("_probe67.json", "w", encoding="utf-8"), ensure_ascii=False, indent=2)
