Repository navigation
Expand file tree
/
Copy pathbatch_reels.py
More file actions
57 lines (48 loc) · 2.37 KB
/
Copy pathbatch_reels.py
File metadata and controls
57 lines (48 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Transcribe several public Instagram Reels in one batch, each with its own spend cap.
Cost: AI transcription for every item, 2 credits per started minute, 60 second minimum.
PER_ITEM_MAX (default 4) caps each Reel: 4 covers up to 2 minutes. A Reel over its
cap fails with BUDGET_EXCEEDED and costs nothing. Worst case: items x PER_ITEM_MAX.
Plan: paid plans only. Items per batch: 10 on Starter, 25 on Pro, 50 on Scale.
Setup: python -m pip install transcriptdock
Run: TRANSCRIPTDOCK_API_KEY=td_live_... PER_ITEM_MAX=4 python batch_reels.py URL [URL ...]
"""
import os
import sys
import time
from uuid import uuid4
from transcriptdock import TranscriptDock
urls = sys.argv[1:]
if not urls:
sys.exit("Usage: python batch_reels.py URL [URL ...]")
per_item_max = int(os.environ.get("PER_ITEM_MAX", "4"))
print(f"{len(urls)} Reel(s), at most {per_item_max} credits each, "
f"{len(urls) * per_item_max} in the worst case.", file=sys.stderr)
client = TranscriptDock(os.environ["TRANSCRIPTDOCK_API_KEY"])
# One key for the whole batch. Save it: resending the same body with it
# returns the original batch instead of creating a second one.
batch_key = str(uuid4())
print("Idempotency-Key:", batch_key, file=sys.stderr)
batch = client.create_batch(
{"items": [{"source": {"url": u}, "mode": "auto", "max_credits": per_item_max} for u in urls],
"metadata": {"example": "batch_reels.py"}},
idempotency_key=batch_key,
)
# Batches have no long poll. A batch.completed webhook avoids polling.
deadline = time.monotonic() + 30 * 60
while batch["status"] not in ("succeeded", "partial_success", "failed", "cancelled"):
if time.monotonic() > deadline:
sys.exit(f"Batch {batch['id']} is still {batch['status']}. Check it again later.")
time.sleep(5)
batch = client.get_batch(batch["id"])
print(f"{batch['status']}: {batch['succeeded_items']} done, "
f"{batch['failed_items']} failed of {batch['total_items']}", file=sys.stderr)
charged = 0
for item in batch["items"]:
job = client.get_job(item["job_id"])
charged += job["billing"]["credits_charged"]
print(f"\n# {urls[item['item_index']]}")
if job["status"] != "succeeded":
print(f"({job['status']}: {(job.get('error') or {}).get('code')})")
continue
print(client.export_transcript(job["result_id"], "txt"))
print(f"\nCharged {charged} credit(s) in total.", file=sys.stderr)