Repository navigation
Expand file tree
/
Copy pathexamples.sh
More file actions
182 lines (159 loc) · 5.67 KB
/
Copy pathexamples.sh
File metadata and controls
182 lines (159 loc) · 5.67 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash
# Transcript Dock Instagram examples with cURL.
#
# Usage:
# export TRANSCRIPTDOCK_API_KEY="td_live_..."
# bash examples.sh help
#
# Instagram uses AI transcription only: 2 credits per started minute with a
# 60 second minimum, on paid plans. captions_only is not supported for Instagram
# and answers 422 CAPABILITY_UNAVAILABLE. Every job below sends max_credits.
# Nothing runs unless you name a command.
#
# Needs: bash, curl. The "check", "wait" and "batch-status" commands also need jq.
set -euo pipefail
API="https://www.transcriptdock.com" # always www: the apex domain redirects
# A fresh idempotency key. Set IDEMPOTENCY_KEY yourself to replay a request
# whose response you lost: same key and same body returns the original job.
new_key() {
if [ -n "${IDEMPOTENCY_KEY:-}" ]; then
printf '%s' "$IDEMPOTENCY_KEY"
elif command -v uuidgen >/dev/null 2>&1; then
uuidgen
elif [ -r /proc/sys/kernel/random/uuid ]; then
cat /proc/sys/kernel/random/uuid
else
printf 'td-%s-%s%s' "$(date +%s)" "$RANDOM" "$RANDOM"
fi
}
need_key() {
: "${TRANSCRIPTDOCK_API_KEY:?Set TRANSCRIPTDOCK_API_KEY to your td_live_ key}"
}
# Refuse values that would break the hand written JSON bodies below.
safe() {
case "$1" in
*'"'* | *'\'* ) echo "Refusing a value with quotes or backslashes: $1" >&2; exit 2 ;;
esac
printf '%s' "$1"
}
# GET with the API key. --fail-with-body prints the error envelope on 4xx/5xx.
get() {
need_key
curl -sS --fail-with-body \
-H "Authorization: Bearer $TRANSCRIPTDOCK_API_KEY" \
"$API$1"
}
# POST JSON with the API key and an idempotency key (printed to stderr so you can save it).
post() {
need_key
local key
key="$(new_key)"
echo "Idempotency-Key: $key" >&2
curl -sS --fail-with-body -X POST \
-H "Authorization: Bearer $TRANSCRIPTDOCK_API_KEY" \
-H "Idempotency-Key: $key" \
-H "Content-Type: application/json" \
-d "$2" \
"$API$1"
}
# Accept only a positive whole number (used for max_credits and limits).
num() {
case "$1" in
'' | *[!0-9]* | 0) echo "Expected a positive whole number, got: $1" >&2; exit 2 ;;
esac
printf '%s' "$1"
}
need_jq() {
command -v jq >/dev/null 2>&1 || { echo "This command needs jq." >&2; exit 2; }
}
usage() {
cat <<'EOF'
Commands (cost in brackets):
check Is Instagram open to this key, and how many credits are left? (needs jq) [free]
reel URL [MAX_CREDITS] AI transcription of a public Reel or video post, mode auto.
Default cap 20 (up to 10 minutes). [<= MAX_CREDITS]
transcribe URL [MAX_CREDITS] The same with mode transcribe. [<= MAX_CREDITS]
batch URL [URL...] Several Reels in one request, each capped at PER_ITEM_MAX (default 4,
up to 2 minutes each). [<= PER_ITEM_MAX per item]
batch-status BATCH_ID Batch counters and items (needs jq). [free]
job JOB_ID Read a job once. [free]
wait JOB_ID Long poll until the job is final (needs jq). [free]
cancel JOB_ID Cancel before AI transcription starts. [free]
transcript RESULT_ID The transcript as JSON, with word timing. [free]
export RESULT_ID FORMAT txt, json, srt or vtt. [free]
EOF
}
cmd="${1:-help}"
shift || true
case "$cmd" in
check)
need_jq
get "/v1/capabilities" | jq '{instagram, transcribe_credits_per_minute, minimum_billable_seconds}'
get "/v1/usage" | jq '{plan, credits_remaining, credits_reserved}'
;;
reel | transcribe)
# With no caption path on Instagram, auto goes straight to AI transcription.
# The media is measured first; above the cap the job fails with BUDGET_EXCEEDED
# and nothing is charged.
url="$(safe "${1:?URL required, e.g. https://www.instagram.com/reel/REEL_ID/}")"
max="$(num "${2:-20}")"
mode="auto"
if [ "$cmd" = "transcribe" ]; then mode="transcribe"; fi
post "/v1/jobs" "{\"source\":{\"url\":\"$url\"},\"mode\":\"$mode\",\"max_credits\":$max}"
;;
batch)
# Items per batch: 10 on Starter, 25 on Pro, 50 on Scale.
[ "$#" -ge 1 ] || { echo "batch needs at least one URL" >&2; exit 2; }
per_item="$(num "${PER_ITEM_MAX:-4}")"
items=""
for u in "$@"; do
u="$(safe "$u")"
items="$items{\"source\":{\"url\":\"$u\"},\"mode\":\"auto\",\"max_credits\":$per_item},"
done
post "/v1/batches" "{\"items\":[${items%,}]}"
;;
batch-status)
need_jq
get "/v1/batches/${1:?BATCH_ID required}" | jq '{status, total_items, succeeded_items, failed_items, cancelled_items, items}'
;;
job)
get "/v1/jobs/${1:?JOB_ID required}"
;;
wait)
# AI transcription takes roughly a quarter of the media length.
need_jq
id="${1:?JOB_ID required}"
deadline=$(( $(date +%s) + ${TIMEOUT_SECONDS:-900} ))
while :; do
body="$(get "/v1/jobs/$id?wait=25")"
status="$(printf '%s' "$body" | jq -r .status)"
echo "status: $status" >&2
case "$status" in
succeeded | failed | cancelled) printf '%s\n' "$body"; break ;;
esac
if [ "$(date +%s)" -ge "$deadline" ]; then echo "Still running. Run wait again later." >&2; exit 1; fi
sleep 1
done
;;
cancel)
# Works until AI transcription starts. Later: 409 CANCELLATION_NOT_ALLOWED.
need_key
curl -sS --fail-with-body -X POST \
-H "Authorization: Bearer $TRANSCRIPTDOCK_API_KEY" \
"$API/v1/jobs/${1:?JOB_ID required}/cancel"
;;
transcript)
get "/v1/transcripts/${1:?RESULT_ID required}"
;;
export)
get "/v1/transcripts/${1:?RESULT_ID required}/export?format=${2:-txt}"
;;
help | -h | --help)
usage
;;
*)
echo "Unknown command: $cmd" >&2
usage
exit 2
;;
esac