import csv
import hashlib
import json
import sys
from datetime import datetime
from pathlib import Path
manifest, target = map(Path, sys.argv[1:3])
entries = json.loads(manifest.read_text(encoding="utf-8"))
if not isinstance(entries, list) or not entries:
raise SystemExit("Manifest must contain reviewed video records.")
rows, seen, requests = [], set(), set()
for entry in entries:
expected = entry.get("expected_aweme_id")
expected_author = entry.get("expected_author_uid")
phrase = entry.get("required_caption_phrase")
if not isinstance(expected, str) or not expected.isascii() or not expected.isdigit() or not any(c != "0" for c in expected) or expected in seen:
raise SystemExit("Each expected_aweme_id must be a unique digit string.")
if not isinstance(expected_author, str) or not expected_author.isascii() or not expected_author.isdigit() or not any(c != "0" for c in expected_author):
raise SystemExit("expected_author_uid must be a verified digit string, not nickname/sec_uid.")
if not isinstance(phrase, str) or not phrase.strip():
raise SystemExit("Set the exact caption phrase you want to check.")
collected = entry.get("collected_at")
if not isinstance(collected, str):
raise SystemExit("Record actual collected_at with timezone.")
moment = datetime.fromisoformat(collected.replace("Z", "+00:00"))
if moment.tzinfo is None:
raise SystemExit("collected_at requires timezone.")
source = manifest.parent / entry["file"]
source_bytes = source.read_bytes(); digest = hashlib.sha256(source_bytes).hexdigest()
raw = json.loads(source_bytes)
if not isinstance(raw, dict) or raw.get("error") is not None or raw.get("status") != "succeeded" or raw.get("action") != "douyin.app.v3.fetch_one_video_by_share_url":
raise SystemExit("Expected successful Douyin share-link detail.")
request_id = raw.get("request_id")
if not isinstance(request_id, str) or not request_id.strip() or request_id in requests:
raise SystemExit("Each observation needs a distinct request_id.")
post = raw.get("data", {}).get("aweme_detail")
if not isinstance(post, dict) or post.get("aweme_id") != expected:
raise SystemExit("Missing/filtered detail or aweme_id differs from the manifest.")
author = post.get("author")
if author is not None and not isinstance(author, dict):
raise SystemExit("Invalid author object.")
uid = (author or {}).get("uid")
if uid is not None and (not isinstance(uid, str) or not uid.isascii() or not uid.isdigit() or not any(c != "0" for c in uid)):
raise SystemExit("Invalid returned author uid.")
caption = post.get("desc")
if caption is not None and not isinstance(caption, str):
raise SystemExit("Invalid caption.")
if caption is not None and caption.strip() in {"[redacted]", "[redacted-depth]"}:
caption = None
author_check = "missing" if uid is None else ("match" if uid == expected_author else "mismatch")
phrase_check = "missing" if caption is None else ("present" if phrase in caption else "absent")
stats = post.get("statistics")
if stats is not None and not isinstance(stats, dict):
raise SystemExit("Invalid statistics.")
counts = []
for key in ["digg_count", "comment_count", "share_count", "collect_count", "play_count"]:
value = (stats or {}).get(key)
if value is not None and (type(value) is not int or not 0 <= value <= 9007199254740991):
raise SystemExit(f"Invalid counter: {key}")
counts.append(None if value is None else int(value))
rows.append([expected, expected_author, uid, author_check, phrase, phrase_check, caption,
*counts, collected, entry["file"], request_id, digest, "", ""])
seen.add(expected)
requests.add(request_id)
def cell(value):
if value is None:
return ""
value = str(value)
return "'" + value if value.lstrip().startswith(("=", "+", "-", "@")) else value
with target.open("x", encoding="utf-8", newline="") as output:
writer = csv.writer(output)
writer.writerow(["aweme_id", "expected_author_uid", "actual_author_uid", "author_check", "required_caption_phrase", "phrase_check", "caption", "likes", "comments", "shares", "collects", "plays", "collected_at", "source_file", "request_id", "source_sha256", "review_decision", "review_reason"])
writer.writerows([[cell(value) for value in row] for row in rows])
print(f"Wrote {len(rows)} delivery-review rows; no API calls made.")