File size: 2,783 Bytes
9459cd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# render_engine.sh — OpenClaw wrapper for the video-render-engine HF Space.
# Config values (base_url, poll interval/timeout) come from config.yaml so
# the YAML and this script stay in sync as required by convention.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/config.yaml"

_yaml() {
  # tiny dependency-free YAML value getter for flat "key: value" lines
  grep -E "^\s*${1}:" "$CONFIG_FILE" | head -1 | sed -E "s/^[^:]+:\s*\"?([^\"]*)\"?\s*$/\1/"
}

BASE_URL=$(_yaml "base_url")
POLL_INTERVAL=$(_yaml "poll_interval_seconds")
POLL_TIMEOUT=$(_yaml "poll_timeout_seconds")
POLL_INTERVAL=${POLL_INTERVAL:-10}
POLL_TIMEOUT=${POLL_TIMEOUT:-3600}

usage() {
  echo "Usage:"
  echo "  $0 render <request.json>"
  echo "  $0 status <job_id>"
  echo "  $0 download <job_id> <local_output_dir>"
  exit 1
}

cmd_render() {
  local req_file="$1"
  [ -f "$req_file" ] || { echo "request file not found: $req_file" >&2; exit 1; }

  local resp
  resp=$(curl -sS -X POST "${BASE_URL}/render" \
    -H "Content-Type: application/json" \
    --data-binary "@${req_file}")

  local job_id
  job_id=$(echo "$resp" | grep -o '"job_id":"[^"]*"' | head -1 | cut -d'"' -f4)

  if [ -z "$job_id" ]; then
    echo "render request failed, response: $resp" >&2
    exit 1
  fi

  echo "$job_id"
}

cmd_status() {
  local job_id="$1"
  local elapsed=0

  while [ "$elapsed" -lt "$POLL_TIMEOUT" ]; do
    local resp
    resp=$(curl -sS "${BASE_URL}/status/${job_id}")
    local status
    status=$(echo "$resp" | grep -o '"status":"[^"]*"' | head -1 | cut -d'"' -f4)

    echo "$resp"

    case "$status" in
      completed|failed)
        return 0
        ;;
    esac

    sleep "$POLL_INTERVAL"
    elapsed=$((elapsed + POLL_INTERVAL))
  done

  echo "polling timed out after ${POLL_TIMEOUT}s" >&2
  exit 1
}

cmd_download() {
  local job_id="$1"
  local out_dir="$2"
  mkdir -p "$out_dir"

  local resp
  resp=$(curl -sS "${BASE_URL}/status/${job_id}")
  local video thumbnail
  video=$(echo "$resp" | grep -o '"video":"[^"]*"' | head -1 | cut -d'"' -f4)
  thumbnail=$(echo "$resp" | grep -o '"thumbnail":"[^"]*"' | head -1 | cut -d'"' -f4)

  if [ -n "$video" ] && [ "$video" != "null" ]; then
    curl -sS "${BASE_URL}/outputs/${video}" -o "${out_dir}/${video}"
    echo "saved ${out_dir}/${video}"
  fi
  if [ -n "$thumbnail" ] && [ "$thumbnail" != "null" ]; then
    curl -sS "${BASE_URL}/outputs/${thumbnail}" -o "${out_dir}/${thumbnail}"
    echo "saved ${out_dir}/${thumbnail}"
  fi
}

[ $# -ge 1 ] || usage
case "$1" in
  render)   [ $# -eq 2 ] || usage; cmd_render "$2" ;;
  status)   [ $# -eq 2 ] || usage; cmd_status "$2" ;;
  download) [ $# -eq 3 ] || usage; cmd_download "$2" "$3" ;;
  *)        usage ;;
esac