#!/usr/bin/env bash # next-video: make a video from the first topic in ~/moneyprinterturbo/topics.txt # with MoneyPrinterTurbo's API on this VM, wait for it, and email it to the owner. # Settings for every video come from ~/moneyprinterturbo/video.json. # From https://exe.dev/deploy/moneyprinterturbo set -euo pipefail dir="$HOME/moneyprinterturbo" api=http://127.0.0.1:8080/api/v1 topic=$(grep -m1 -v '^[[:space:]]*$' "$dir/topics.txt" 2>/dev/null || true) if [ -z "$topic" ]; then echo "next-video: no topics left in $dir/topics.txt" exit 0 fi # Start the video: video.json plus this topic as the subject. id=$(jq --arg s "$topic" '. + {video_subject: $s}' "$dir/video.json" | curl -fsS "$api/videos" -H 'Content-Type: application/json' --data-binary @- | jq -r .data.task_id) echo "next-video: task $id: $topic" # The API has it now, so take the topic off the list. awk -v t="$topic" '!done && $0 == t { done = 1; next } 1' "$dir/topics.txt" > "$dir/topics.txt.new" mv "$dir/topics.txt.new" "$dir/topics.txt" # Wait up to two hours: 1 is done, -1 failed, anything else is still running. # The API keeps task status in memory: if it restarted, the task is gone. state=4 task=$(jq -n --arg id "$id" '{task_id: $id, state: 4, error: "still running after two hours"}') for _ in $(seq 240); do sleep 30 resp=$(curl -sS "$api/tasks/$id") || continue # the API is restarting task=$(jq -c --arg id "$id" '.data // {task_id: $id, state: -1, error: .message}' <<<"$resp") || continue state=$(jq -r .state <<<"$task") [ "$state" = 1 ] || [ "$state" = -1 ] && break done if [ "$state" = 1 ]; then subject="New video: $topic" body=$(jq -r --arg base "https://$(hostname).exe.xyz:8080" \ '"Your video is ready:\n" + ([.videos[] | $base + .] | join("\n"))' <<<"$task") else subject="Video failed: $topic" body=$(jq -r '"MoneyPrinterTurbo task \(.task_id) ended in state \(.state): \(.error // "no error message")"' <<<"$task") fi echo "next-video: $subject" to=$(curl -fsS https://reflection.int.exe.xyz/email | jq -r '.email // empty') if [ -n "$to" ]; then jq -n --arg to "$to" --arg subject "$subject" --arg body "$body" \ '{to: $to, subject: $subject, body: $body}' | curl -fsS -X POST http://169.254.169.254/gateway/email/send \ -H 'Content-Type: application/json' --data-binary @- >/dev/null fi [ "$state" = 1 ]