# -*- 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__}"

st, body = get("https://catalog.gatech.edu/programs/")
# 提取所有 slug
slugs = re.findall(r'href="(/programs/([^/]+)/?)"[^>]*>([^<]+)</a>', body)
seen = {}
for path, slug, title in slugs:
    title = h.unescape(title).strip()
    if slug and slug not in seen:
        seen[slug] = title

# 已录 det 路径（全名 slug 形式）
idx = open("index_main.html", encoding="utf-8").read()
seg = idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
done_urls = re.findall(r'url:"details/gatech-([^"]+)\.html"', seg)
done_slugs = set(u.replace("gatech-","") for u in done_urls)
print("已录 det slug 数:", len(done_slugs))
for s in sorted(done_slugs): print("  DONE:", s)

# 筛硕士候选，排除已录、排除 certificate/minor/undergraduate/phd
cand = []
for slug, title in seen.items():
    if slug in done_slugs: continue
    low = slug.lower()
    if any(k in low for k in ["certificate","minor","undergraduate","phd","doctoral","bs-","-bs","b-","-b","minor"]): continue
    # 硕士类
    if re.search(r'(ms|meng|mcp|march|mpp|mpa|mfin|msba|mps|mat|mas|mfa|mls|mhm|mpm|msed|mscit|mbid|pmase|msus|msus|master)', low):
        cand.append((slug, title))

print("\n候选:", len(cand))
for slug, title in cand:
    print("  ", slug, "|", title[:80])
