# -*- coding: utf-8 -*-
"""追加 UCSD：SCHOOLS 一行 + CATD 244 行。原 CATD 末尾 ]; 保留，不重复闭合。"""
import io, sys, os, json, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
BASE = r"D:\work buddy\2026-11-05-14-15-52"
F = os.path.join(BASE, "index_main.html")
r = open(F, encoding="utf-8").read()
catd = json.load(open(os.path.join(BASE, "pending_ucsd_catd.json"), encoding="utf-8"))

school_anchor = ' {id:"gatech",cn:"佐治亚理工学院",en:"Georgia Tech",country:"US",qs:"2027 USNews 并列第 29"},\n];'
assert school_anchor in r, "SCHOOLS 锚点未找到"
r = r.replace(school_anchor,
              ' {id:"gatech",cn:"佐治亚理工学院",en:"Georgia Tech",country:"US",qs:"2027 USNews 并列第 29"},\n'
              ' {id:"ucsd",cn:"加州大学圣地亚哥分校",en:"UC San Diego",country:"US",qs:"2027 USNews 并列第 29"},\n];')

m = None
for mm in re.finditer(r'\["gatech",[^\]]*\],?', r):
    m = mm
assert m, "CATD gatech 末行未找到"
last_txt = m.group(0)
last_nc = last_txt[:-1] if last_txt.endswith(",") else last_txt

def fmt(c):
    return f'["{c[0]}", "{c[1]}", "{c[2]}", "{c[3]}", "{c[4]}", "{c[5]}", "{c[6]}", "{c[7]}"],'

new_rows = "\n".join(fmt(c) for c in catd)
replacement = last_nc + ",\n" + new_rows
r = r[:m.start()] + replacement + r[m.end():]

open(F, "w", encoding="utf-8", newline="").write(r)
print("SCHOOLS ucsd 已加；CATD 追加", len(catd), "行")
