# -*- coding: utf-8 -*-
"""汇总 _profile_raw.json：数量、时间分布、时长、下载候选列表"""
import json, sys, io, os

path = r"D:\Deepseek\douyin_fengdaquan\_profile_raw.json"
data = json.load(open(path, encoding="utf-8"))
aw = data["awemes"]
print("作品总数:", len(aw))

videos = [a for a in aw if (a.get("video") or {}).get("duration")]
images = [a for a in aw if not (a.get("video") or {}).get("duration")]
print("视频:", len(videos), " 图文:", len(images))

from collections import Counter
c = Counter()
for a in aw:
    ct = a.get("create_time") or 0
    import datetime
    c[datetime.datetime.fromtimestamp(ct).strftime("%Y-%m")] += 1
for k in sorted(c):
    print("  ", k, c[k])

tot = sum((a.get("video") or {}).get("duration", 0) for a in videos) / 1000.0
print("视频总时长: %.1f 分钟 (%.2f 小时)" % (tot / 60, tot / 3600))
print("平均时长: %.1f 秒" % (tot / max(1, len(videos))))

# 下载候选
rows = []
for a in aw:
    v = a.get("video") or {}
    if not v.get("duration"):
        continue
    rates = v.get("bit_rate") or []
    best = max(rates, key=lambda b: b.get("bit_rate") or 0) if rates else None
    url = ((best or {}).get("play_addr") or v.get("play_addr") or {}).get("url_list", [""])[0]
    rows.append({
        "id": a["aweme_id"],
        "desc": (a.get("desc") or "").replace("\n", " "),
        "date": __import__("datetime").datetime.fromtimestamp(a.get("create_time") or 0).strftime("%Y-%m-%d"),
        "dur_s": round(v.get("duration", 0) / 1000.0, 1),
        "digg": (a.get("statistics") or {}).get("digg_count", 0),
        "rate": (best or {}).get("bit_rate") or 0,
        "url": url,
    })
rows.sort(key=lambda r: r["date"], reverse=True)
json.dump(rows, open(r"D:\Deepseek\douyin_fengdaquan\_download_list.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
print("\n下载候选:", len(rows))
for r in rows[:10]:
    print("  %s %5.1fs %6d赞  %s" % (r["date"], r["dur_s"], r["digg"], r["desc"][:40]))
print("\n最早:", rows[-1]["date"] if rows else "-", " 最新:", rows[0]["date"] if rows else "-")
