""" Extract unique video URLs from all NitroGen shards. Reads metadata.json from each tar.gz without fully extracting. Outputs a JSON file mapping video_id -> {url, source, game, ...}. """ import tarfile import json import os import sys from pathlib import Path from collections import defaultdict ACTIONS_DIR = Path(__file__).parent / "actions" OUTPUT_FILE = Path(__file__).parent / "video_urls.json" def extract_urls_from_shard(shard_path: Path) -> dict: videos = {} try: with tarfile.open(shard_path, "r:gz") as tar: for member in tar: if member.name.endswith("metadata.json"): f = tar.extractfile(member) if f is None: continue meta = json.loads(f.read().decode("utf-8")) ov = meta.get("original_video", {}) vid = ov.get("video_id") if vid and vid not in videos: videos[vid] = { "url": ov.get("url", ""), "source": ov.get("source", ""), "video_id": vid, "resolution": ov.get("resolution"), "game": meta.get("game", ""), } except Exception as e: print(f" [ERROR] {shard_path.name}: {e}", file=sys.stderr) return videos def main(): shards = sorted(ACTIONS_DIR.glob("SHARD_*.tar.gz")) print(f"Found {len(shards)} shards in {ACTIONS_DIR}") all_videos = {} for i, shard in enumerate(shards): print(f"[{i+1:3d}/{len(shards)}] Processing {shard.name} ...") videos = extract_urls_from_shard(shard) all_videos.update(videos) print(f" Found {len(videos)} videos (total unique: {len(all_videos)})") with open(OUTPUT_FILE, "w") as f: json.dump(all_videos, f, indent=2) print(f"\nDone! {len(all_videos)} unique videos saved to {OUTPUT_FILE}") sources = defaultdict(int) for v in all_videos.values(): sources[v["source"]] += 1 print("Sources breakdown:") for src, cnt in sorted(sources.items(), key=lambda x: -x[1]): print(f" {src}: {cnt}") if __name__ == "__main__": main()