# -*- coding: utf-8 -*-
import sys, io, re, os
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
BASE = r"D:\work buddy\2026-11-05-14-15-52"

FEE = ('2026-27 官方口径：多数硕士/博士项目学费与杂费约 <b>$23,131/年</b>（加州居民）、<b>$38,233/年</b>（非居民/国际生）；'
       '职业学位与自支持学位另计，以 Registrar 年度费率表为准。'
       '<a href="https://grad.ucla.edu/funding/tuition/" target="_blank">UCLA Graduate Division 学费页 ↗</a>')

FILES = ["details/ucla-ece-ms.html","details/ucla-aero-ms.html",
         "details/ucla-stats-ms.html","details/ucla-biostat-ms.html","details/ucla-slavic-ma.html"]

pats = [
 (r'(<b>Tuition:</b><br>)([\s\S]*?)(</div>)', "A"),
 (r'(<h3>Tuition / Funding</h3>)([\s\S]*?)(</div>)', "B"),
]

ok = 0
for rel in FILES:
    fp = os.path.join(BASE, rel.replace("/", os.sep))
    s = open(fp, encoding="utf-8").read()
    if "23,131" in s:
        print("already", rel); ok += 1; continue
    done = False
    for p, tag in pats:
        m = re.search(p, s)
        if m:
            s = s[:m.start(2)] + FEE + s[m.end(2):]
            done = True
            print("patted", tag, rel)
            break
    if not done:
        print("!! miss", rel); continue
    if "数据说明" not in s:
        s = s.replace("</body>",
          '<div class="note">📋 数据说明：学费口径摘自 UCLA Graduate Division 学费页（实抓 2026-12-02，2026-27 学年）；'
          '官网未公布字段标注待补，不做估算填充。</div>\n</body>')
    open(fp, "w", encoding="utf-8").write(s)
    ok += 1
    print("  size", os.path.getsize(fp))
print("done:", ok, "/5")
