# -*- coding: utf-8 -*-
"""构建最终作品清单：冯大全本人 312 条（292 旧抓 + 20 新抓），排除他人视频"""
import json, datetime, sys

ROOT = r"D:\Deepseek\douyin_fengdaquan"
sys.stdout.reconfigure(encoding="utf-8")

raw = json.load(open(ROOT + r"\_profile_all2.json", encoding="utf-8"))["awemes"]
old = json.load(open(ROOT + r"\_download_list.json", encoding="utf-8"))

rows = {}
for a in raw:
    if (a.get("author") or {}).get("nickname") != "冯大全":
        continue
    v = a.get("video") or {}
    rates = sorted(((b.get("bit_rate") or 0, (b.get("play_addr") or {}).get("url_list", [""])[0]) for b in (v.get("bit_rate") or [])), reverse=True)
    rows[a["aweme_id"]] = {
        "id": a["aweme_id"],
        "desc": (a.get("desc") or "").replace("\n", " "),
        "created": datetime.datetime.fromtimestamp(a["create_time"]).strftime("%Y-%m-%d"),
        "duration_ms": v.get("duration") or 0,
        "is_image": not v.get("duration"),
        "play": (rates[0][1] if rates else ((v.get("play_addr") or {}).get("url_list") or [""])[0]),
        "digg": (a.get("statistics") or {}).get("digg_count", 0),
        "comment": (a.get("statistics") or {}).get("comment_count", 0),
        "images": [im.get("url_list", [""])[0] for im in (a.get("images") or []) if im.get("url_list")],
        "src": "profile",
    }

for r in old:
    if r["id"] in rows:
        continue
    rows[r["id"]] = {
        "id": r["id"], "desc": r["desc"], "created": r["date"],
        "duration_ms": int(r.get("dur_s", 0) * 1000),
        "is_image": not r.get("dur_s"),
        "play": r.get("play") or r.get("url") or "",
        "digg": r.get("digg", 0), "comment": r.get("comment", 0),
        "images": [], "src": "old",
    }

allrows = sorted(rows.values(), key=lambda x: x["created"], reverse=True)
vids = [r for r in allrows if not r["is_image"]]
print("冯大全作品:", len(allrows), "视频:", len(vids), "图文:", len(allrows) - len(vids))
print("视频总时长: %.1f 分钟" % (sum(r["duration_ms"] for r in vids) / 60000))
print("日期范围:", allrows[-1]["created"], "→", allrows[0]["created"])
print("无播放链接:", sum(1 for r in vids if not r["play"]))
json.dump(allrows, open(ROOT + r"\_works_final.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
