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

ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
def get(url, timeout=25):
    req = urllib.request.Request(url, headers={"User-Agent":"Mozilla/5.0"})
    try:
        r = urllib.request.urlopen(req, timeout=timeout, context=ctx)
        return r.status, r.read().decode("utf-8", errors="replace")
    except Exception as e:
        return None, f"ERR {type(e).__name__}"

# GaTech 剩余候选
cands = [
    "masters-industrial-design",
    "global-media-cultures-ms",
    "applied-languages-intercultural-studies-ms",
    "history-sociology-technology-science-ms",
]
for slug in cands:
    url = f"https://catalog.gatech.edu/programs/{slug}/"
    st, body = get(url)
    if st == 200:
        m = re.search(r'<title>([^<]+)</title>', body)
        title = m.group(1) if m else "?"
        rows = re.findall(r'<tr[^>]*>(.*?)</tr>', body, re.S)
        courses = []
        for row in rows:
            cells = re.findall(r'<t[dh][^>]*>(.*?)</t[dh]>', row, re.S)
            cells = [h.unescape(re.sub(r'<[^>]+>','',c)).strip() for c in cells]
            if len(cells) >= 2 and re.match(r'^[A-Z]{2,4}\s*\d{3,4}', cells[0]):
                courses.append(cells)
        print(f"OK {len(body):>7}  {slug}")
        print(f"     title: {title[:100]}")
        print(f"     courses: {len(courses)}")
        for c in courses[:8]:
            print("       ", " | ".join(c[:3]))
        open(f"_56_{slug}.html","w",encoding="utf-8").write(body)
    else:
        print(f"FAIL {body[:60]}  {slug}")
