# -*- 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 Adult Education / Higher Education 的 Quick Facts
for key, url in [
    ("ut-adult-edu", "https://www.sgs.utoronto.ca/programs/adult-education/"),
    ("ut-higher-edu", "https://www.sgs.utoronto.ca/programs/higher-education/"),
]:
    html = fetch(url)
    # 找 "Quick Facts" 后面的内容
    qi = html.find("Quick Facts")
    if qi > 0:
        chunk = html[qi:qi+3000]
        # 提取 dt/dd 或 p 标签
        items = re.findall(r'<(?:dt|th|strong|b)[^>]*>(.*?)</(?:dt|th|strong|b)>\s*(?:<dd[^>]*>|<td[^>]*>|<p[^>]*>)(.*?)(?:</dd>|</td>|</p>)', chunk, re.S)
        print(f"=== {key} Quick Facts ({len(items)} items) ===")
        for k,v in items[:12]:
            k2 = re.sub(r'<[^>]+>','',k).strip()
            v2 = re.sub(r'<[^>]+>','',v).strip()
            if k2: print(f"  {k2}: {v2[:100]}")
    # 找项目标题（og:title 或 h1/h2）
    og = re.search(r'<meta[^>]+property="og:title"[^>]+content="([^"]+)"', html)
    if og: print(f"  og:title: {og.group(1)}")
    # 找学位
    deg = re.findall(r'(MA|MEd|MHSc|MMus|MEng|MASc|MS|PhD|EdD|Master[^<]{0,40})', html[:5000])
    print(f"  deg hints: {list(set(deg))[:8]}")

# 2. 探3个更多 OISE slug
print("\n=== more OISE slugs ===")
for slug in ["counselling-psychology", "curriculum-and-pedagogy", "developmental-education-and-psychology", "language-and-literacies", "social-justice-education"]:
    url = f"https://www.sgs.utoronto.ca/programs/{slug}/"
    try:
        h = fetch(url)
        og = re.search(r'<meta[^>]+property="og:title"[^>]+content="([^"]+)"', h)
        print(f"  {slug}: 200 {len(h)} | og: {og.group(1) if og else '?'}")
    except Exception as e:
        print(f"  {slug}: FAIL {str(e)[:50]}")
