# -*- coding: utf-8 -*-
import io, sys, os, re, ssl, urllib.request, json, 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/")
print("index status", st, "len", len(body))

# 提取所有 program 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

# 已录的 GaTech 项目 id（从 PROGRAMS 段 grep）
idx = open("index_main.html", encoding="utf-8").read()
seg = idx[idx.find("const PROGRAMS="):idx.find("const CATD=")]
done_ids = re.findall(r'id:"(gatech-[^"]+)"', seg)
done_slugs = set()
for did in done_ids:
    # gatech-cs-ms -> cs-ms
    s = did.replace("gatech-","")
    done_slugs.add(s)

print("\n已录 GaTech slug 数:", len(done_slugs))
for s in sorted(done_slugs):
    print("  DONE:", s)

# 筛硕士（ms/meng/mcp/march/mpp/msud/mseem/intatech 等结尾，排除 phd/doctoral）
cand = []
for slug, title in seen.items():
    if slug in done_slugs: continue
    low = slug.lower()
    # 硕士候选
    if re.search(r'(ms$|ms-|-ms$|meng|mcp|march|mpp|mpa|mfin|msba|mps|mat|mas|mps|mla|mm|mma|mps|mta|mt$|mfa|mls|mhm|mpm|msed|mscit|mscs|msee|msme|msce|msae|msie|msae|msen|mses|msen|msgh|mshe|msis|msit|msqm|msre|msrm|msus|msw|msys|mshp|msml|msds|msba|msba|msf|msa|msb|msc|msd|mse|msg|msh|msi|msj|msk|msl|msm|msn|mso|msp|msq|msr|mss|mst|msu|msv|msw|msx|msy|msz)', low) and "phd" not in low and "doctoral" not in low:
        cand.append((slug, title))

print("\n硕士候选总数:", len(cand))
# 按热门优先级排序关键词
hot = ["computer","comput","data","analytics","analytic","business","finance","economic","statistic","machine","engineer","electrical","mechanical","bio","biomed","manag","information","cyber","security","mathematic","applied","operations","supply","computational","robot","media","policy","public","health","international","urban","architect","design","chem","physic","material","envir","sustain","energy","earth","geol","science","technolog","learning"]
def score(item):
    slug, title = item
    low = (slug + " " + title).lower()
    s = 0
    for i, k in enumerate(hot):
        if k in low: s += (len(hot)-i)
    return -s
cand.sort(key=score)
for slug, title in cand[:40]:
    print("  CAND:", slug, "|", title[:80])
