# -*- coding: utf-8 -*-
"""r384：页面体检（禁词/长度/HTTP）+ PROGRAMS 回填（4 条）。"""
import io, os, re, shutil, urllib.request

BASE = r"D:\work buddy\2026-11-05-14-15-52"
IDS = ['geo-cs-ms', 'geo-dspp-ms', 'emory-ms-cs', 'emory-cs-phd']
BAD = ['编辑整理', '待补', 'TBD', 'TODO']

def strip_tags(s):
    s = re.sub(r'<script.*?</script>', '', s, flags=re.S)
    s = re.sub(r'<style.*?</style>', '', s, flags=re.S)
    s = re.sub(r'<[^>]+>', ' ', s)
    return re.sub(r'\s+', ' ', s)

FILES = {
 'geo-cs-ms': 'geo-computer-science-ms',
 'geo-dspp-ms': 'geo-data-science-for-public-policy-ms',
 'emory-ms-cs': 'emory-ms-computer-science',
 'emory-cs-phd': 'emory-cs-phd',
}
for i in IDS:
    p = os.path.join(BASE, 'details', FILES[i] + '.html')
    t = io.open(p, encoding='utf-8').read()
    txt = strip_tags(t)
    bad = [b for b in BAD if b in txt]
    try:
        with urllib.request.urlopen('http://127.0.0.1:8000/details/' + i + '.html', timeout=8) as r:
            code = r.status
            ln = len(r.read())
    except Exception as e:
        code = 'ERR:%s' % e
        ln = -1
    print('%-14s bytes=%6d chars=%5d bad=%s http=%s len=%s' % (i, len(t.encode('utf-8')), len(txt), bad, code, ln))

UPD = {
 'geo-cs-ms': {
   'ddl': '2027 春季：2026-10-01（最终轮）；2027 秋季：2027-01-15（优先轮）/ 2027-04-01（最终轮）；官网只写月日，年份按入学季推算，不设 rolling',
   'lang': 'TOEFL iBT 90（旧制）；2026-01-21 起新制 iBT 4.5；IELTS 7.0；在美国或英语为主要教学语言高校取得学位者可豁免',
   'tui': '每学分 $2,758（2026-27 研究生费率）；论文研究 $2,750/学期；活动费 $27/学期；学生医保 $4,450/年；申请费 $90',
   'dur': '30 学分；全日制 2 年（课程表按 Year 1/Year 2 编排）；coursework 或 coursework+论文（论文轨 24 学分）；另有加速 MS（B.S./M.S.）入口',
   'test': '官网未单列 GRE/GMAT（申请材料清单无相关条目）',
 },
 'geo-dspp-ms': {
   'ddl': 'Fall 2027：2026-12-01（Early Action）/ 2027-01-15（优先轮·奖学金）/ 2027-03-01（最终轮）；配套材料分别 2026-12-08 / 2027-01-23 / 2027-03-01',
   'lang': 'TOEFL 100（2026-01-21 起新制 iBT 5）；IELTS Academic 7.0（官网注明为严格门槛）；英语学位可豁免；送分码 5244',
   'tui': '每学分 $2,758，全日制 39 学分；医保强制（可豁免）；申请费 $90；merit 奖学金与助理岗另行评定',
   'dur': '39 学分；全日制 2 年 / 21 个月；含 0 学分 McCourt Foundations（开学前连续 5 天）与 120 小时实习要求',
   'test': 'opt（Fall 2027 起 GRE 不再必需但强烈建议；GMAT 同样接受，均无最低分）',
 },
 'emory-ms-cs': {
   'ddl': 'Spring 2027：2026-11-01 优先 / 2026-12-01 最终（押金 2026-12-15）；Fall 2027：2027-01-15 优先 / 2027-04-15 最终（押金 2027-05-15）',
   'lang': 'TOEFL iBT 最低 85（建议 90）；IELTS 7；PTE 60；Duolingo 120；英语教学学位可豁免',
   'tui': '2024-25 学年口径：全日制（每学期 ≥9 学分）$23,900/学期；非全日制 $2,656/学分；申请费 $75；录取自动评部分学费奖学金',
   'dur': '1.5–2 年；3 门核心 + 4 门选修 + 9 学分结题（论文/项目/加课三选一）+ 必修 practicum；平均班额 35 人',
   'test': 'notreq',
 },
 'emory-cs-phd': {
   'ddl': '2026-12-15（Fall 2027，官网原文 December 15 未标年份）；仅秋季入学；资助录取一般 2027-04-15 前发出',
   'lang': 'TOEFL iBT 85（建议 90）/ PBT 600；IELTS 7；接受 Duolingo 与 PTE；英语教学学位可豁免',
   'tui': '全额学费奖学金 + stipend：FY26-27 年度 $43,260（SEIU 集体协议，有效期至 2027-08-24）；含健康保险；申请费 $75',
   'dur': '平均 5 年；前 2–3 年课程（3 核心 + 4 选修 + 2 次 rotation），随后资格考试、论文提案与答辩',
   'test': 'notreq',
 },
}

main = os.path.join(BASE, 'index_main.html')
shutil.copy2(main, main + '.bak_r384')
s = io.open(main, encoding='utf-8').read()

def block_span(text, pid):
    st = text.find('{id:"%s"' % pid)
    if st < 0:
        raise SystemExit('missing id ' + pid)
    nx = text.find('{id:"', st + 6)
    en = nx if nx > 0 else text.find('];', st)
    return st, en

def esc(v):
    return v.replace('\\', '\\\\').replace('"', '\\"')

cnt = 0
for pid, fields in UPD.items():
    st, en = block_span(s, pid)
    seg = s[st:en]
    for k, v in fields.items():
        if k == 'test':
            new = 'test:{cls:"%s"},' % esc(v)
            seg2, n = re.subn(r'test:\{cls:"[^"]*"\},', new, seg)
        else:
            new = '%s:"%s",' % (k, esc(v))
            seg2, n = re.subn(r'\b%s:"[^"]*",' % k, new, seg, count=1)
        assert n >= 1, (pid, k, n)
        seg = seg2
        cnt += 1
    s = s[:st] + seg + s[en:]

io.open(main, 'w', encoding='utf-8', newline='\n').write(s)
print('backfill fields', cnt, 'bytes', os.path.getsize(main))
