From 2e5f6f3f81a28936e4bfb1d03d3b47d8431d280b Mon Sep 17 00:00:00 2001 From: cobweb Date: Sun, 5 Oct 2025 19:04:45 +0000 Subject: [PATCH] Add autocue.sh --- autocue.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 autocue.sh diff --git a/autocue.sh b/autocue.sh new file mode 100644 index 0000000..164c988 --- /dev/null +++ b/autocue.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "--- AutoQueue started ---" + +while true; do + inotifywait -e close_write -m "${WATCH_DIR}" | while read -r path action file; do + TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') + echo "[$TIMESTAMP] Detected new file: $file" | tee -a "${LOG_FILE}" + + ATTEMPTS=0 + MEDIA_ID="" + + while [ -z "$MEDIA_ID" ] && [ $ATTEMPTS -lt 6 ]; do + ATTEMPTS=$((ATTEMPTS + 1)) + sleep 5 + MEDIA_ID=$(curl -s -H "Authorization: Bearer ${AZURACAST_API_KEY}" "${AZURACAST_API_URL}/files" | + jq -r --arg NAME "${SUBDIR}/$file" '.[] | select(.path | endswith($NAME)) | .id') + + if [ -z "$MEDIA_ID" ]; then + echo "[$TIMESTAMP] Attempt $ATTEMPTS: File $file not yet indexed" | tee -a "${LOG_FILE}" + fi + done + + if [ -n "$MEDIA_ID" ]; then + echo "[$TIMESTAMP] Queuing $file (ID $MEDIA_ID) as next..." | tee -a "${LOG_FILE}" + if curl -s -X POST -H "Content-Type: application/json" \ + -H "Authorization: Bearer ${AZURACAST_API_KEY}" \ + -d "{\"track_id\": \"$MEDIA_ID\", \"play_next\": true}" \ + "${AZURACAST_API_URL}/requests"; then + echo "[$TIMESTAMP] Successfully queued $file" | tee -a "${LOG_FILE}" + else + echo "[$TIMESTAMP] Failed to queue $file" | tee -a "${LOG_FILE}" + fi + else + echo "[$TIMESTAMP] Gave up after $ATTEMPTS attempts: $file not indexed" | tee -a "${LOG_FILE}" + fi + done +done