import json, re
from pathlib import Path

root = Path(r"D:\work buddy\2026-11-05-14-15-52")
html = (root / "index_main.html").read_text(encoding="utf-8")

m = re.search(r"const\s+PROGRAMS\s*=\s*\[", html)
start = m.end()
depth = 1; i = start
while i < len(html) and depth > 0:
    c = html[i]
    if c == "[": depth += 1
    elif c == "]": depth -= 1
    i += 1
block = html[start:i-1]

items = {}
for m in re.finditer(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|$)', block):
    pid = m.group(1); body = m.group(2)
    def grab(key):
        mm = re.search(re.escape(key)+r':"([^"]*)"', body)
        return mm.group(1) if mm else ""
    items[pid] = {"s": grab("s"), "n": grab("n"), "en": grab("en"), "deg": grab("deg"), "url": grab("url")}

snap = json.loads((root / "_daily_snapshot.json").read_text(encoding="utf-8"))
# Snapshot was already overwritten by _r189_diff.py, so reload the OLD one is impossible.
# Instead, recompute: the "old" snapshot at 2026-11-25 09:30 had 786 entries.
# We have the new items list. We need old snapshot to diff.
# Since we overwrote, we'll just output all items with url in the "known changed" set
# OR we saved the old? No. Let's reconstruct: the diff we already ran printed 99 new + 14 url changed.
# We'll re-derive from the printed lists by reading _r189_diff output? No.
# Better: I have the diff printed above. Let's hardcode the new_ids and url_changed from that output.
# Actually simpler: regenerate diff by re-running against a backup of old snapshot. But we don't have backup.
# Pragmatic: the diff output above is the truth. Let's just generate rows for the 113 known changes.

school_map = {
    "col":"哥伦比亚","up":"宾大","cmu":"卡内基梅隆","usc":"南加州","nyu":"纽约","umich":"密歇根安娜堡",
    "jhu":"约翰霍普金斯","duke":"杜克","nu":"西北","cornell":"康奈尔","illinois":"UIUC","uchicago":"芝加哥",
    "mit":"麻省理工","princeton":"普林斯顿","harvard":"哈佛","yale":"耶鲁","caltech":"加州理工","stanford":"斯坦福",
    "dartmouth":"达特茅斯","brown":"布朗","rice":"莱斯","vanderbilt":"范德堡","wustl":"圣路易斯华盛顿",
    "notre":"圣母","emory":"埃默里","geo":"乔治城","berkeley":"伯克利","ucla":"加州洛杉矶","uva":"弗吉尼亚",
    "unc":"北卡教堂山","gatech":"佐治亚理工","ucsd":"加州圣地亚哥","ut":"多伦多","ubc":"UBC","mcg":"麦吉尔","uw":"滑铁卢"
}

# New IDs from diff output
new_ids = """illinois-ms-ece
ubc-meng-mech
mcg-msc-cs-thesis
ubc-msc-cs-thesis
mcgill-msc-ece-thesis
princeton-mse-mae
uva-ms-systems-engineering
mcgill-meng-ece-nonthesis
yale-mpp
brown-mph-inperson
uiuc-ms-me
berkeley-meng-me
ubc-mph
berkeley-meng-eecs
berkeley-meng-ieor
mcg-ma-econ-thesis
princeton-meng-mae
mit-mst
berkeley-meng-bioe
berkeley-meng-cee
princeton-mse-cee
princeton-meng-cbe
ubc-masc-me
berkeley-meng-mse
berkeley-meng-ne
princeton-mse-cbe
princeton-meng-cee
mcg-mist
harvard-edm
ucla-mlis
uva-msds
ubc-msc-stat-thesis
ubc-masc-ece
mcg-msc-nutrition-thesis
harvard-mpp
ubc-masc-civil
dartmouth-mph-campus
mit-tpp
illinois-msfe
princeton-mpa
emory-mph
nu-mim
brown-msds
notre-msba
notre-msf
ucsd-cse-ms
uva-me-bme
uchicago-msca
caltech-msee
caltech-ms-aero
uchicago-harris-mpp
vandy-ms-cs
caltech-ms-space
uchicago-harris-ma
dartmouth-ms-cs
uchicago-crown-ssa-ma
notre-msa
uchicago-harris-msdap
uchicago-harris-mscep
emory-msba
vandy-msbt
vandy-macc
duke-mms-fob
yale-mam
emory-mfin
uchicago-harris-macrm
cornell-mps-acct
nyu-msacc
umich-ross-macc
umich-ross-mscm
duke-mqm-ba
ucla-mfe
ucla-biostat-ms
ucla-hpm-ms
nyu-msqe
jhu-ma-econ
up-mse-ece
duke-mae
mcg-msc-ece
mcg-msc-me
mcg-msc-math
emory-msph-biostat
ubc-msc-cs-essay
ucsd-csme-ms
ucsd-css-ms
unc-stands-ms
dartmouth-medinfo-ms
berkeley-biostat-ma
umich-biostat-ms
umich-hds-ms
brown-scm-biostat
gatech-msbinf
col-stat-ma
brown-scm-cs
ucla-ece-ms
usc-spds-ms
berkeley-cbe-pdp
ucla-aero-ms
ut-mscsm""".strip().split("\n")

# URL changed from diff output
url_changed = {
    "duke-mids": ("details/duke-c68493d.html", "details/duke-mids.html"),
    "uchicago-mscapp": ("details/uchicago-computational-analysis-public-policy.html", "details/uchicago-mscapp.html"),
    "notre-mscse": ("details/notre-mscse-computer-science-engineering.html", "details/notre-mscse.html"),
    "notre-msee": ("details/notre-electrical-engineering-msee---masters-traditional.html", "details/notre-msee.html"),
    "notre-msme": ("details/notre-mechanical-engineering-msme---masters-traditional.html", "details/notre-msme.html"),
    "mit-mban": ("details/mit-business-analytics.html", "details/mit-mban.html"),
    "cornell-mps-mgmt": ("details/cornell-c3b1b9a.html", "details/cornell-mps-mgmt.html"),
    "usc-stats-ms": ("details/usc-cf67a4b.html", "details/usc-stats-ms.html"),
    "usc-msmkt": ("details/usc-ccfb8b7.html", "details/usc-msmkt.html"),
    "princeton-cs-mse": ("details/princeton-computer-science.html", "details/princeton-cs-mse.html"),
    "usc-phds-ms": ("details/usc-public-health-data-science-ms.html", "details/usc-phds-ms.html"),
    "cmu-mspm": ("details/cmu-product-management-ms.html", "details/cmu-mspm.html"),
    "usc-abe-ms": ("details/usc-applied-biostatistics-epidemiology-ms.html", "details/usc-abe-ms.html"),
    "duke-msec": ("details/duke-cfae4d5.html", "details/duke-msec.html"),
}

def find_official_url(details_path):
    p = root / details_path
    if not p.exists(): return ""
    try:
        content = p.read_text(encoding="utf-8", errors="ignore")
    except: return ""
    urls = re.findall(r'https?://[^"\'<>\s]+', content)
    # Filter
    excluded = ("fonts.g", "w3.org", "schema.org", "googleapis", "gstatic", "cdnjs",
                "jsdelivr", "unpkg", "jquery", "bootstrap", "tailwind", "example.com",
                "localhost", "127.0.0.1", "file://")
    best = ""
    for u in urls:
        if any(x in u.lower() for x in excluded): continue
        if ".edu" in u.lower():
            return u
        if not best: best = u
    return best

print("=== NEW (99) ===")
for pid in new_ids:
    rec = items.get(pid)
    if not rec: continue
    sname = school_map.get(rec["s"], rec["s"])
    url = rec["url"]
    file_link = "file:///D:/work%20buddy/2026-11-05-14-15-52/" + url
    official = find_official_url(url)
    line = f"{sname} | {rec['n']} | {rec['deg']} | {file_link}"
    if official: line += f" | 官网: {official}"
    print(line)

print()
print("=== URL CHANGED (14) ===")
for pid, (old_u, new_u) in url_changed.items():
    rec = items.get(pid)
    if not rec: continue
    sname = school_map.get(rec["s"], rec["s"])
    file_link = "file:///D:/work%20buddy/2026-11-05-14-15-52/" + new_u
    official = find_official_url(new_u)
    line = f"[升级] {sname} | {rec['n']} | {rec['deg']} | {file_link}"
    if official: line += f" | 官网: {official}"
    print(line)
