# -*- coding: utf-8 -*-
"""合并两次抓取结果，得到全量作品清单（313）"""
import json, sys

ROOT = r"D:\Deepseek\douyin_fengdaquan"
old = json.load(open(ROOT + r"\_download_list.json", encoding="utf-8"))
new = json.load(open(ROOT + r"\_profile_full_list.json", encoding="utf-8"))

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

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"\_all_works.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
