# -*- coding: utf-8 -*-
import json, io, sys, os, 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")
raw = open(f, encoding="utf-8").read()

# 幂等：博士 slug 已在则中止
if "uchicago-accounting-phd.html" in raw:
    print("芝大博士已追加，跳过"); sys.exit(0)

rows = json.load(open(os.path.join(base,"pending_uchicago_phd_catd.json"),encoding="utf-8"))
def js(s): return json.dumps(s, ensure_ascii=False)
cat_lines = "\n".join("[" + ", ".join(js(x) for x in r) + "]," for r in rows)

c0 = raw.index("const CATD=["); c1 = raw.index("\n];", c0)
raw = raw[:c1] + "\n" + cat_lines + raw[c1:]

open(f,"w",encoding="utf-8",newline="").write(raw)
print("CATD 追加芝大博士", len(rows), "条")

# 复核
r3 = open(f,encoding="utf-8").read()
cc0=r3.index("const CATD=["); cc1=r3.index("const VERIFIED_DETAILS")
pat=re.compile(r'\["(?P<s>[^"]+)",\s*"(?P<col>[^"]*)",\s*"(?P<en>[^"]*)",\s*"(?P<cn>[^"]*)",\s*"(?P<deg>[^"]*)",\s*"(?P<url>[^"]*)",\s*"(?P<disc>[^"]*)",\s*"(?P<det>[^"]*)"\]')
rows3=[m.groupdict() for m in pat.finditer(r3[cc0:cc1])]
uch=[g for g in rows3 if g["s"]=="uchicago"]
print("CATD 总数:", len(rows3), " uchicago:", len(uch))
missing=[g["det"] for g in uch if not os.path.exists(os.path.join(base,g["det"]))]
print("uchicago 死链:", missing)
