# -*- coding: utf-8 -*-
"""_meta.json -> 工具包的 _works.json（语料项目根目录）"""
import json, os, sys

sys.stdout.reconfigure(encoding="utf-8")
ROOT = r"D:\Deepseek\tiktok_sea_corpus"
meta = json.load(open(os.path.join(ROOT, "_meta.json"), encoding="utf-8"))
cards = {c["id"]: c for c in json.load(open(os.path.join(ROOT, "_corpus_cards.json"), encoding="utf-8"))}

rows = []
for aid, m in meta.items():
    if m.get("error"):
        continue
    c = cards.get(aid, {})
    rows.append({
        "id": aid,
        "desc": m.get("desc") or c.get("desc", ""),
        "created": m.get("created", ""),
        "duration_ms": m.get("duration_ms", 0),
        "is_image": m.get("is_image", False),
        "play": m.get("play", ""),
        "digg": m.get("digg", 0),
        "comment": m.get("comment", 0),
        "author": m.get("author", ""),
        "keywords": c.get("keywords", []),
    })
rows.sort(key=lambda r: r["created"], reverse=True)
json.dump(rows, open(os.path.join(ROOT, "_works.json"), "w", encoding="utf-8"), ensure_ascii=False, indent=1)
v = [r for r in rows if not r["is_image"] and r["play"]]
print(f"语料 {len(rows)} 条（视频 {len(v)}），总时长 {sum(r['duration_ms'] for r in v)/60000:.1f} 分钟")
print(f"作者数 {len({r['author'] for r in rows})}")
