127 lines
3.2 KiB
Bash
Executable File
127 lines
3.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
function get_time_ms {
|
|
date -u +%s%3N
|
|
}
|
|
|
|
|
|
URL_BASE="https://www.radiorecord.ru/api"
|
|
STATIONS=$(curl -s "$URL_BASE/stations/" | jq --argjson ids '[507,522,523,536,537,42532,42602]' '.result.stations | map(select(.id | IN($ids[]))) | map({id, title, stream_hls, icon_fill_white})')
|
|
|
|
MPV_PID_FILE="/tmp/mpv_radio_pid"
|
|
RADIO_ID_FILE="/tmp/radio_id"
|
|
|
|
get_radio() {
|
|
echo "$STATIONS" | jq -r --argjson sel_id "$1" 'map(select(.id == $sel_id)).[0]'
|
|
}
|
|
|
|
get_song() {
|
|
echo $(curl -s "$URL_BASE/station/history/?id=$1" | jq '.result.history[0] | ({id, artist, song, image600})')
|
|
}
|
|
|
|
|
|
# Function to play audio stream
|
|
start() {
|
|
radio_url=$(get_radio $1 | jq -r ".stream_hls")
|
|
if [ -f "$MPV_PID_FILE" ]; then
|
|
mpv_pid=$(cat "$MPV_PID_FILE")
|
|
if ps -p "$mpv_pid" > /dev/null; then
|
|
kill -SIGTERM "$mpv_pid"
|
|
fi
|
|
fi
|
|
mpv "$radio_url" & echo $! > "$MPV_PID_FILE"
|
|
echo $1 > "$RADIO_ID_FILE"
|
|
}
|
|
|
|
# Function to pause/play the audio stream
|
|
pause() {
|
|
if [ -f "$MPV_PID_FILE" ]; then
|
|
mpv_pid=$(cat "$MPV_PID_FILE")
|
|
if ps -p "$mpv_pid" > /dev/null; then
|
|
kill -9 "$mpv_pid"
|
|
fi
|
|
fi
|
|
rm -f $MPV_PID_FILE
|
|
}
|
|
|
|
play() {
|
|
pause
|
|
radio_id=$(cat "$RADIO_ID_FILE")
|
|
start "$radio_id"
|
|
}
|
|
|
|
toggle() {
|
|
PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 )
|
|
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
|
|
PAUSED=$(( $PID == 0 && $RADIO_ID > 0 ? 1 : 0 ))
|
|
if [ "$PAUSED" = 1 ]; then
|
|
play
|
|
else
|
|
pause
|
|
fi
|
|
}
|
|
|
|
status() {
|
|
PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 )
|
|
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
|
|
PAUSED=$(( $PID == 0 && $RADIO_ID > 0 ? 1 : 0 ))
|
|
INFO=$1
|
|
RES=$(jq -cn --argjson pid "$PID" --argjson stations "$STATIONS" --argjson radio_id "$RADIO_ID" --argjson is_paused "$PAUSED" --argjson info "$INFO" '{"is_paused": $is_paused, "song": $info, "radio": $radio_id, "stations": $stations}')
|
|
echo "$RES"
|
|
}
|
|
|
|
|
|
status_loop() {
|
|
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
|
|
INFO=$(get_song "$RADIO_ID")
|
|
status "$INFO"
|
|
last_time_info=$(get_time_ms)
|
|
last_time=$(get_time_ms)
|
|
while true; do
|
|
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
|
|
current_time=$(get_time_ms)
|
|
delta=$((current_time - last_time_info))
|
|
if [[ $delta -gt 15000 ]]; then
|
|
NEW_INFO=$(get_song $RADIO_ID)
|
|
INFO=$(echo "$INFO" "$NEW_INFO" | jq -s ' reduce .[] as $item ( {}; . + (
|
|
reduce ($item | to_entries[]) as $entry ( {};
|
|
if $entry.value != null then .[$entry.key] = $entry.value else . end
|
|
)) )')
|
|
|
|
last_time_info=$(get_time_ms)
|
|
fi
|
|
current_time=$(get_time_ms)
|
|
delta=$((current_time - last_time))
|
|
if [[ $delta -gt 1000 ]]; then
|
|
status "$INFO"
|
|
last_time=$(get_time_ms)
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Main script
|
|
case "$1" in
|
|
"start")
|
|
start "$2"
|
|
;;
|
|
"toggle")
|
|
toggle
|
|
;;
|
|
"pause")
|
|
pause
|
|
;;
|
|
"play")
|
|
play
|
|
;;
|
|
"status")
|
|
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
|
|
INFO=$(get_song $RADIO_ID)
|
|
status "$INFO"
|
|
;;
|
|
*)
|
|
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
|
|
echo $(get_song $RADIO_ID)
|
|
status_loop
|
|
;;
|
|
esac
|