# -*- coding: utf-8 -*-
import io, re, shutil, os
P = r"D:\work buddy\2026-11-05-14-15-52\index_main.html"
BAK = P + ".bak_r368"
if not os.path.exists(BAK):
    shutil.copy2(P, BAK)
html = io.open(P, encoding="utf-8").read()
M = {
    "ut-management-phd": "ut",
    "uw-accounting-phd": "uw",
    "usc-pibbs-phd": "usc",
    "upenn-bioe-phd": "up",
    "upenn-bio-phd": "up",
    "upenn-bgs-immunology-phd": "up",
    "columbia-biosci-phd": "col",
    "cmu-mspm-ms": "cmu",
}
head = html[: html.index("const PROGRAMS=[")]
seg = html[html.index("const PROGRAMS=["): html.index("const CATD=[")]
tail = html[html.index("const CATD=["):]
parts = seg.split('{id:"')
assert parts[0].strip().endswith("const PROGRAMS=[")
out = [parts[0]]
done = []
for b in parts[1:]:
    rid = b.split('"', 1)[0]
    if rid in M:
        pat = re.compile(r'(?<![A-Za-z])s:"[^"]*"')
        assert len(pat.findall(b)) == 1, "s 字段不唯一: %s" % rid
        b2 = pat.sub('s:"%s"' % M[rid], b, count=1)
        out.append(b2)
        done.append((rid, M[rid]))
    else:
        out.append(b)
new_seg = '{id:"'.join(out)
new_html = head + new_seg + tail
io.open(P, "w", encoding="utf-8", newline="").write(new_html)
print("已修正:", done)
print("字节: %d -> %d" % (len(html), len(new_html)))
