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

todo = json.load(open("todo_upgrade.json", encoding="utf-8"))
print("todo 总数(去重):", len(todo))
print("样例字段:", list(todo[0].keys()))

# disc 分布
from collections import Counter
disc = Counter(t.get("disc","?") for t in todo)
print("\ntodo disc 分布:", dict(disc.most_common()))

# 已在 PROGRAMS 的去重（按 det / url）
html = open("index_main.html", encoding="utf-8").read()
m = re.search(r"const PROGRAMS=\[(.*?)\n\];", html, re.S).group(1)
prog_urls = set(re.findall(r'url:"(details/[^"]+)"', m))
prog_en = set(re.findall(r'en:"([^"]+)"', m))
print("\nPROGRAMS url 数:", len(prog_urls))

# 热门榜关键词
hot = {
 "cs": ["computer", "data science", "statistics", "statistical", "machine", "artificial intelligence", "information system", "computational"],
 "ee": ["electrical", "computer engineering", "electronic"],
 "biz": ["finance", "business", "economics", "management", "accounting", "marketing", "analytic", "analytics", "quantitative", "supply"],
 "eng": ["mechanical", "civil", "chemical", "materials", "engineering", "aerospace", "biomedical", "environmental"],
 "sci": ["biology", "biochemistry", "biostat", "physics", "chemistry", "math", "science", "biomedical"],
}
def is_hot(t):
    en = (t.get("en","") or "").lower()
    disc = t.get("disc","")
    return disc in ("cs","eng","biz","sci") or any(k in en for k in hot["cs"]+hot["ee"]+hot["biz"]+hot["eng"]+hot["sci"])

remain = [t for t in todo if t.get("det") not in prog_urls]
hot_remain = [t for t in remain if is_hot(t)]
print("todo 中未入库(按 det):", len(remain))
print("其中热门榜相关:", len(hot_remain))

# 按校统计剩余热门
by_school = Counter(t.get("s","?") for t in hot_remain)
print("\n剩余热门按校 TOP15:", by_school.most_common(15))

# 历史节奏：详情页 mtime 按天（PROGRAMS 命中）
det_dir = "details"
prog_seg = m
chunks = prog_seg.split('{id:"')[1:]
prog_det = {}
for c in chunks:
    um = re.search(r'url:"(details/[^"]+)"', c)
    if um: prog_det[um.group(1)] = c.split('"',1)[0]
day_count = Counter()
for rel in prog_det:
    p = rel.replace("/", os.sep)
    if os.path.exists(p):
        d = datetime.datetime.fromtimestamp(os.path.getmtime(p)).strftime("%Y-%m-%d")
        day_count[d]+=1
print("\nPROGRAMS 详情页落盘按天:")
for d in sorted(day_count): print(" ", d, day_count[d])
