# -*- coding: utf-8 -*-
import re, io, sys, json, os
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()

clean = json.load(open(os.path.join(base,"clean_list.json"), encoding="utf-8"))
remove_det = {g["det"] for g in clean["remove"]}
review_det = {g["det"] for g in clean["review"]}
drop_det = remove_det | review_det
print("review 待剔除:", len(review_det), " 合计剔除集:", len(drop_det))

# CATD 重建
c0 = raw.index("const CATD=["); c1 = raw.index("\n];", c0)
head = raw[:c0]; tail = raw[c1:]
body = raw[c0+len("const CATD=["):c1]
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>[^"]*)"\]')
kept, dropped = [], []
for m in pat.finditer(body):
    if m.group("det") in review_det:
        dropped.append(m.group("det"))
    else:
        kept.append(m.group(0))
print("本轮剔除 review:", len(dropped), " CATD 保留:", len(kept))
new_catd = "const CATD=[\n" + ",\n".join(kept) + "\n"
raw2 = head + new_catd + tail

# 同步 VERIFIED_DETAILS
v0 = raw2.index("const VERIFIED_DETAILS=new Set(["); v1 = raw2.index("]);", v0)
vdet = re.findall(r'"(details/[^"]+\.html)"', raw2[v0:v1])
vkeep = [d for d in vdet if d not in drop_det]
print("VERIFIED_DETAILS:", len(vdet), "->", len(vkeep))
new_v = "const VERIFIED_DETAILS=new Set([" + ",".join('"'+d+'"' for d in vkeep) + "])"
raw2 = raw2[:v0] + new_v + raw2[v1+2:]

open(f, "w", encoding="utf-8", newline="").write(raw2)

# 复核
r3 = open(f, encoding="utf-8").read()
cc0 = r3.index("const CATD=["); cc1 = r3.index("const VERIFIED_DETAILS")
rows=[m.groupdict() for m in pat.finditer(r3[cc0:cc1])]
missing=[g["det"] for g in rows if not os.path.exists(os.path.join(base,g["det"]))]
from collections import Counter
print("复核 CATD:", len(rows), " 死链:", len(missing))
print("按学校:", dict(Counter(g["s"] for g in rows)))
# 残留临床/MBA 复查
resid=[g for g in rows if re.search(r"MBA|Business Administration|Nurse|MSN|DNP|DNAP|MD-PhD|Physician Assistant|\bMD\b|Physical Therapy|Occupational Therapy|Speech-Language|Dentistry|Veterinary|Genetic Counsel|Perfusion", g["en"]+" "+g["deg"]+" "+g["cn"], re.I)]
print("残留排除项:", len(resid))
for g in resid: print("  残留:", g["s"], g["cn"], g["en"], g["deg"])
