# -*- coding: utf-8 -*-
import re, pathlib, shutil, sys, io, datetime
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = pathlib.Path(r"D:\work buddy\2026-11-05-14-15-52")
P = BASE / "index_main.html"
bak = BASE / ("index_main.html.bak_r347")
shutil.copy2(P, bak)
t = P.read_text(encoding="utf-8")
seg = t[t.index("const PROGRAMS=["):t.index("const CATD=[")]
prefix = seg[:seg.index('{id:"')]
blocks = re.split(r'(?=\{id:")', seg)[1:]

UPD = {
 "emory-cs-ms": {
   "lang": "TOEFL iBT 85 / IELTS 7.0 / Duolingo 120（非英语授课本科背景须提交，具体豁免以招生页为准）",
   "test": "notreq",
   "tui": "$24,900/学期（2026-27）；按学期计费，项目级总价官网未单列，不估算",
   "dur": "全日制 1.5–2 年；30 学分（三核心领域各 1 门 + 项目/课程/论文三路径）；practicum 必修",
 },
 "rice-mcs-ms": {
   "lang": "官网未披露（项目页未公布最低总分/小分；国际生按 Rice 研究生英语政策提交 TOEFL/IELTS/Duolingo）",
   "test": "opt",
   "tui": "官网未披露（项目级费率与总额未单列，以 Rice Bursar 公布的研究生费率与注册学分为准，不估算）",
   "dur": "30 semester hours，非论文，通常 3 学期；可全日制或 part-time，须至少 1 个秋季/春季学期住校",
 },
 "rice-mds-ms": {
   "lang": "官网未披露（项目页未公布最低总分/小分；国际生按 Rice 研究生英语政策提交 TOEFL/IELTS/Duolingo）",
   "test": "opt",
   "tui": "官网未披露（项目级费率与总额未单列，以 Rice Bursar 公布的研究生费率与注册学分为准，不估算）",
   "dur": "31 semester hours，非论文，连续 3 学期（fall/spring/second fall）；仅秋季入学、全日制",
 },
}

def setk(blk, k, v):
    if k == "test":
        blk2 = re.sub(r'test:\{cls:"(?:[^"\\]|\\.)*"\}', 'test:{cls:"' + v + '"}', blk, count=1)
        if blk2 == blk:
            blk2 = re.sub(r'test:"(?:[^"\\]|\\.)*"', 'test:{cls:"' + v + '"}', blk, count=1)
        return blk2
    return re.sub(k + r':"(?:[^"\\]|\\.)*"', lambda m: k + ':"' + v + '"', blk, count=1)

cnt = 0
newblocks = []
for b in blocks:
    m = re.search(r'{id:"([^"]+)"', b)
    pid = m.group(1) if m else ""
    if pid in UPD:
        for k, v in UPD[pid].items():
            nb = setk(b, k, v)
            if nb != b:
                cnt += 1
            b = nb
    newblocks.append(b)
new_seg = prefix + "".join(newblocks)
t2 = t[:t.index("const PROGRAMS=[")] + new_seg + t[t.index("const CATD=["):]
P.write_text(t2, encoding="utf-8")
print("fields updated", cnt, "bytes", P.stat().st_size)
