← back to Crcp Pitch Video
heygen/poll.sh
47 lines
#!/bin/bash
# Silent poller: check the 3 HeyGen jobs, download each when completed.
export PATH="$HOME/.local/bin:$PATH"
export HEYGEN_API_KEY=$(grep -m1 '^HEYGEN_API_KEY=' ~/Projects/secrets-manager/.env | cut -d= -f2- | tr -d '"'\''')
cd ~/Projects/crcp-pitch-video
mkdir -p out
LOG=heygen/poll.log
: > "$LOG"
declare_done() { echo "$1" >> heygen/done.txt; }
: > heygen/done.txt
ids=$(cut -f2 heygen/jobs.tsv)
deadline=$(( $(date +%s) + 75*60 ))
while :; do
alldone=1
while IFS=$'\t' read -r name vid sid; do
[ -z "$vid" ] && continue
grep -q "^$vid\$" heygen/done.txt 2>/dev/null && continue
[ -f "out/heygen-$name.mp4" ] && { declare_done "$vid"; continue; }
resp=$(heygen video get "$vid" </dev/null 2>/dev/null)
st=$(echo "$resp" | python3 -c "import sys,json;print(json.load(sys.stdin)['data'].get('status',''))" 2>/dev/null)
echo "$(date +%H:%M:%S) $name $st" >> "$LOG"
if [ "$st" = "completed" ] || [ "$st" = "success" ]; then
url=$(echo "$resp" | python3 -c "import sys,json;print(json.load(sys.stdin)['data'].get('video_url',''))" 2>/dev/null)
dur=$(echo "$resp" | python3 -c "import sys,json;print(json.load(sys.stdin)['data'].get('duration',''))" 2>/dev/null)
if [ -n "$url" ]; then
curl -fsSL "$url" -o "out/heygen-$name.mp4" && echo "$(date +%H:%M:%S) DOWNLOADED $name (dur=$dur) $(ls -lh out/heygen-$name.mp4 | awk '{print $5}')" >> "$LOG"
declare_done "$vid"
else
alldone=0
fi
elif [ "$st" = "failed" ] || [ "$st" = "error" ]; then
err=$(echo "$resp" | python3 -c "import sys,json;d=json.load(sys.stdin)['data'];print(d.get('error') or d.get('failure') or 'unknown')" 2>/dev/null)
echo "$(date +%H:%M:%S) FAILED $name: $err" >> "$LOG"
declare_done "$vid"
else
alldone=0
fi
done < heygen/jobs.tsv
[ "$alldone" = "1" ] && { echo "ALL DONE" >> "$LOG"; break; }
[ "$(date +%s)" -ge "$deadline" ] && { echo "TIMEOUT" >> "$LOG"; break; }
sleep 60
done
echo "=== poll finished ===" >> "$LOG"
ls -lh out/heygen-*.mp4 2>/dev/null >> "$LOG"