import json
from pathlib import Path

from flashdata import FlashData

input_file = Path("video-ids.txt")
receipt = Path("bulk-job.json")

with FlashData(api_key="your-flashdata-api-key") as client:
    if receipt.exists():
        created = json.loads(receipt.read_text(encoding="utf-8"))
    else:
        if not input_file.is_file() or not input_file.stat().st_size:
            raise ValueError("Save a nonempty video-ids.txt first")
        # Reserve the receipt before submitting. Reruns reuse this job.
        with receipt.open("x", encoding="utf-8") as saved:
            created = client.bulk.create({
                "source": "youtube_download",
                "options": {
                    "download_type": "audio_video",
                    "output_format": "mp4",
                    "video_quality": "<=1080",
                    "storage": {
                        "storage_type": "s3",
                        "storage_url": "your-media-bucket",
                        "object_prefix": "bulk-videos/run-01",
                        "region": "us-east-1",
                        "access_key": "your-storage-access-key",
                        "secret_key": "your-storage-secret-key",
                    },
                },
            })
            json.dump(created, saved)

    bulk_id = created["job"]["id"]
    print("Bulk job:", bulk_id, flush=True)
    if client.bulk.get(bulk_id)["status"] == "awaiting_upload":
        client.bulk.upload_file(created, input_file)
        client.bulk.start(bulk_id)

    # A polling timeout leaves the job running. Rerun to keep watching it.
    job = client.bulk.wait(bulk_id, timeout=3600)
    manifest = client.bulk.manifest(bulk_id)
    Path("manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8")

    # Each row includes the video ID, outcome and delivery result or error.
    with Path("results.jsonl.tmp").open("w", encoding="utf-8") as output:
        for row in client.bulk.rows(bulk_id):
            output.write(json.dumps(row, ensure_ascii=False) + "\n")
    Path("results.jsonl.tmp").replace("results.jsonl")
    print("Saved manifest.json and results.jsonl. Counts:", job["counts"])
