#!/bin/bash ## ## Radio scanner transcripts: downloads and transcribes calls found by ## broadcastifyassist. Audio and transcripts can be shared across machines ## via S3. Settings live in config/scannerassist/default.config. ## CMD=$(basename $0) TMP=$HOME/.tmp/$CMD ; mkdir -p $TMP TENTEN_DIR=$(dirname "$(dirname "$(realpath "$0")")") . $TENTEN_DIR/config/$CMD/default.config usage() { echo "Usage: $CMD -a transcript -g group [-g group ...] -s start -e end [-F] # time|group|duration|src|file|transcript" echo "Usage: $CMD -a file [-f file] # absolute path; reads files from stdin without -f" echo "" echo " -g group ID or broadcastifyassist alias; repeatable" echo " -s/-e local time ('2026-11-09 21:30', zone set by broadcastifyassist's config) or Unix seconds" echo " -F allow windows over 2 hours" echo " -f a file as transcript prints it (5000-1/1794278359.mp3)" exit 1 } unset ACTION START END FORCE FILE GROUP_ARGS=() while getopts ":a:g:s:e:f:Fh" o; do case "$o" in a) ACTION=$OPTARG ;; g) GROUP_ARGS+=(-g "$OPTARG") ;; s) START=$OPTARG ;; e) END=$OPTARG ;; f) FILE=$OPTARG ;; F) FORCE=-F ;; *|h) usage ;; esac done ## Copies one group's cache folder (args: from, to). Does nothing without S3_CACHE. sync_group() { [ -z "$S3_CACHE" ] && return aws --profile=$AWS_PROFILE_NAME s3 sync --quiet "$1" "$2" || echo "Warning: couldn't sync $1 to $2" >&2 } ## Downloads one call's audio and transcribes it, unless already cached. ## Args: file (relative to $TMP), audio url. fetch_call() { local audio=$TMP/$1 url=$2 local text=${audio%.*}.txt mkdir -p "$(dirname "$audio")" if [ ! -s "$audio" ]; then curl -sf -o "$audio.part" "$url" && mv "$audio.part" "$audio" || { echo "Couldn't download $url" >&2 ; return; } fi if [ ! -e "$text" ]; then chatgptassist -a transcribe -f "$audio" -m $TRANSCRIBE_MODEL -p "$TRANSCRIBE_CONTEXT" < /dev/null > "$text.part" && mv "$text.part" "$text" || echo "Couldn't transcribe $1" >&2 fi } export -f fetch_call export TMP TRANSCRIBE_MODEL TRANSCRIBE_CONTEXT ## Prints the file|url of each call (group|file|url|... lines on stdin) ## whose audio or transcript isn't on this machine. uncached_calls() { local group file url rest while IFS='|' read -r group file url rest; do [ -s "$TMP/$file" ] && [ -e "$TMP/${file%.*}.txt" ] || echo "$file|$url" done } ## Prints the absolute path of a cached file, pulling it from S3 if it isn't here. local_path() { local file=$1 if [ ! -e "$TMP/$file" ] && [ -n "$S3_CACHE" ]; then aws --profile=$AWS_PROFILE_NAME s3 cp --quiet "$S3_CACHE/$file" "$TMP/$file" 2> /dev/null fi if [ -e "$TMP/$file" ]; then echo "$TMP/$file" else echo "Not cached here or in S3: $file" >&2 return 1 fi } case "$ACTION" in transcript) [ ${#GROUP_ARGS[@]} -eq 0 ] || [ -z "$START" ] || [ -z "$END" ] && usage calls=$(set -o pipefail; broadcastifyassist -a group-archives "${GROUP_ARGS[@]}" -s "$START" -e "$END" $FORCE -v | jq -r '.[] | [ .groupId, "\(.groupId)/\(.ts).\(.url | split(".") | last)", .url, .start, .label, .duration, (.src // "") ] | map(tostring) | join("|")') || exit 1 [ -z "$calls" ] && exit 0 missing=$(echo "$calls" | uncached_calls) if [ -n "$missing" ]; then groups=$(echo "$missing" | cut -d/ -f1 | sort -u) for g in $groups; do sync_group $S3_CACHE/$g $TMP/$g done missing=$(echo "$calls" | uncached_calls) if [ -n "$missing" ]; then echo "$missing" | tr '|' '\n' | xargs -n 2 -P $PARALLEL bash -c 'fetch_call "$0" "$1"' for g in $groups; do sync_group $TMP/$g $S3_CACHE/$g done fi fi echo "$calls" | while IFS='|' read -r group file url time label duration src; do text=$(cat "$TMP/${file%.*}.txt" 2> /dev/null | tr '|\n\r' ' ' | sed 's/^ *//; s/ *$//') echo "$time|$label|$duration|$src|$file|$text" done ;; file) if [ -n "$FILE" ]; then local_path "$FILE" || exit 1 else missing=0 while read -r f; do [ -n "$f" ] && { local_path "$f" || missing=1; } done exit $missing fi ;; *) usage ;; esac