# -*- coding: utf-8 -*-
"""Remove duplicate old entries for up-msmp and duke-msec (keep the new verified version)"""
import re
IDX = r"D:\work buddy\2026-11-05-14-15-52\index_main.html"
html = open(IDX, "r", encoding="utf-8").read()

# Find and remove the OLD entries (the first occurrence of each id)
# Old up-msmp: {id:"up-msmp",s:"up",n:"医学物理理学硕士",en:"MS in Medical Physics",dept:"Perelman 医学院 · 放射肿瘤学系",...}
# Old duke-msec: {id:"duke-msec",s:"duke",n:"经济学与计算硕士（MSEC）",en:"Master of Science in Economics and Computation",dept:"经济系 × 计算机系联合",...}

# Pattern to match old up-msmp entry: from {id:"up-msmp" to the closing }
# We need to find the FIRST occurrence and remove the whole object
def remove_first_occurrence(html, id_marker):
    idx = html.find(id_marker)
    if idx < 0:
        return html, False
    # Find the opening { before id_marker
    start = html.rfind('{', 0, idx)
    # Find the matching closing }
    depth = 1
    i = start + 1
    while i < len(html) and depth > 0:
        c = html[i]
        if c == '{': depth += 1
        elif c == '}': depth -= 1
        i += 1
    end = i  # position after }
    # Also remove trailing comma and whitespace/newline
    while end < len(html) and html[end] in ' \t\n\r,':
        end += 1
    # But don't remove the comma before the next entry if we're removing from middle
    # Actually, find the comma before start too
    before = start - 1
    while before >= 0 and html[before] in ' \t\n\r':
        before -= 1
    if before >= 0 and html[before] == ',':
        start = before
    # Also handle newline after
    after = end
    while after < len(html) and html[after] in ' \t\n\r':
        after += 1
    html = html[:start] + html[after:]
    return html, True

html, ok1 = remove_first_occurrence(html, 'id:"up-msmp",s:"up",n:"医学物理理学硕士"')
print("Removed old up-msmp:", ok1)
html, ok2 = remove_first_occurrence(html, 'id:"duke-msec",s:"duke",n:"经济学与计算硕士')
print("Removed old duke-msec:", ok2)

open(IDX, "w", encoding="utf-8").write(html)
print("Done")
