# -*- coding: utf-8 -*-
import re, io, sys, os, json
from collections import Counter
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
h = open('index_main.html', encoding='utf-8').read()
p0 = h.index("const PROGRAMS=["); p1 = h.index("const CATD=[")
seg = h[p0:p1]
blocks = re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|,?\n\];)', seg)

def g(b,k):
    m=re.search(k+r':"([^"]*)"',b); return m.group(1) if m else ''

rows=[]
for pid,b in blocks:
    rows.append({'id':pid,'s':g(b,'s'),'n':g(b,'n'),'en':g(b,'en'),
        'dept':g(b,'dept'),'college':g(b,'college'),'deg':g(b,'deg'),
        'dur':g(b,'dur'),'rank':g(b,'rank'),'ddl':g(b,'ddl'),'lang':g(b,'lang'),
        'tui':g(b,'tui'),'test':g(b,'cls'),'url':g(b,'url')})

print('PROGRAMS 总数:', len(rows))

# n 是否含中文
def has_cn(s): return bool(re.search(r'[一-鿿]', s))
n_no_cn=[r for r in rows if not has_cn(r['n'])]
print('中文名 n 全英文/无中文:', len(n_no_cn))
for r in n_no_cn[:15]: print('   ', r['id'], '|', r['n'][:60])

# 字段空/待补统计
def empty(v): return not v.strip() or '待补' in v or '待确认' in v or v.strip()==''
for f in ['dept','college','dur','rank','ddl','lang','tui','test']:
    n=sum(1 for r in rows if empty(r[f]))
    print(f'  缺/待补 {f}: {n}')

# 详情页六卡标题覆盖
det_dir='details'
files=[f for f in os.listdir(det_dir) if f.endswith('.html')]
print('\ndetails 磁盘 html:', len(files))
cards=['申请要求','费用','课程设置','培养方案','毕业走向','院校与城市速览','官方链接']
miss_card=Counter()
progs_url=set(r['url'] for r in rows)
sample=0
for fn in files:
    if fn=='col-cs.html': continue
    txt=open(os.path.join(det_dir,fn),encoding='utf-8',errors='ignore').read()
    for c in cards:
        if c not in txt: miss_card[c]+=1
print('缺卡标题的详情页数量:')
for c in cards: print(f'  {c}: {miss_card[c]}')

# PROGRAMS.url 对应详情页是否存在
missing=[r['id'] for r in rows if r['url'] and not os.path.exists(os.path.join(det_dir,r['url'].split("/")[-1]))]
print('\nPROGRAMS 详情页磁盘缺失:', len(missing))
