Update modules/home/wayland/apps/eww/bar/scripts/radio

This commit is contained in:
sora 2024-01-07 10:43:51 +01:00
parent dce503e035
commit d67ef5edae

View File

@ -1,108 +1,108 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function get_time_ms { get_time_ms() {
date -u +%s%3N date -u +%s%3N
} }
URL_BASE="https://www.radiorecord.ru/api" URL_BASE="https://www.radiorecord.ru/api"
MPV_PID_FILE="/tmp/mpv_radio_pid" MPV_PID_FILE="/tmp/mpv_radio_pid"
RADIO_ID_FILE="/tmp/radio_id" RADIO_ID_FILE="/tmp/radio_id"
STATIONS="[]" STATIONS="[]"
PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 ) PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 )
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 ) RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
RADIO_URL="" RADIO_URL=""
INFO='{"id":null,"artist":null,"song":null,"image600":null}' INFO='{"id":null,"artist":null,"song":null,"image600":null}'
PAUSED=$(( $PID == 0 || $RADIO_ID == 0 ? 1 : 0 )) PAUSED=$(( $PID == 0 || $RADIO_ID == 0 ? 1 : 0 ))
STATUS="{}" STATUS="{}"
get_radio() { get_radio() {
echo "$STATIONS" | jq -r --argjson sel_id "$RADIO_ID" 'map(select(.id == $sel_id)).[0]' echo "$STATIONS" | jq -r --argjson sel_id "$RADIO_ID" 'map(select(.id == $sel_id)).[0]'
} }
get_song() { get_song() {
echo $(curl -s "$URL_BASE/station/history/?id=$RADIO_ID" | jq '.result.history[0] | ({id, artist, song, image600})') echo $(curl -s "$URL_BASE/station/history/?id=$RADIO_ID" | jq '.result.history[0] | ({id, artist, song, image600})')
} }
update() { update() {
PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 ) PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 )
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 ) RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
PAUSED=$(( $PID == 0 && $RADIO_ID > 0 ? 1 : 0 )) PAUSED=$(( $PID == 0 && $RADIO_ID > 0 ? 1 : 0 ))
} }
start() { start() {
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})') 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})')
RADIO_URL=$(get_radio | jq -r '.stream_hls') RADIO_URL=$(get_radio | jq -r '.stream_hls')
if [ "$PAUSED" = 0 ]; then if [ "$PAUSED" = 0 ]; then
kill -9 $PID kill -9 $PID
fi fi
mpv "$RADIO_URL" & echo $! > "$MPV_PID_FILE" mpv "$RADIO_URL" & echo $! > "$MPV_PID_FILE"
echo $RADIO_ID > "$RADIO_ID_FILE" echo $RADIO_ID > "$RADIO_ID_FILE"
} }
toggle() { toggle() {
if [ "$PAUSED" = 1 ]; then if [ "$PAUSED" = 1 ]; then
echo $RADIO_ID $PID echo $RADIO_ID $PID
start start
else else
kill -9 "$PID" kill -9 "$PID"
rm -f $MPV_PID_FILE rm -f $MPV_PID_FILE
fi fi
} }
status() { status() {
echo $(jq -cnr --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 $(jq -cnr --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}')
} }
status_loop() { status_loop() {
STATUS=$(status) STATUS=$(status)
echo $STATUS echo $STATUS
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})') 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})')
last_time_info=0 last_time_info=0
last_time=0 last_time=0
while true; do while true; do
current_time=$(get_time_ms) current_time=$(get_time_ms)
delta=$((current_time - last_time)) delta=$((current_time - last_time))
delta_i=$((current_time - last_time_info)) delta_i=$((current_time - last_time_info))
if [[ $delta -gt 1000 ]]; then if [[ $delta -gt 1000 ]]; then
PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 ) PID=$( [ -e "$MPV_PID_FILE" ] && cat "$MPV_PID_FILE" || echo 0 )
RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 ) RADIO_ID=$( [ -e "$RADIO_ID_FILE" ] && cat "$RADIO_ID_FILE" || echo 0 )
PAUSED=$(( $PID == 0 && $RADIO_ID > 0 ? 1 : 0 )) PAUSED=$(( $PID == 0 && $RADIO_ID > 0 ? 1 : 0 ))
NEW_STATUS=$(status) NEW_STATUS=$(status)
if [[ "$NEW_STATUS" != "$STATUS" ]]; then if [[ "$NEW_STATUS" != "$STATUS" ]]; then
STATUS=$NEW_STATUS STATUS=$NEW_STATUS
echo $STATUS echo $STATUS
fi fi
last_time=$current_time last_time=$current_time
fi fi
if [[ $delta_i -gt 15000 ]]; then if [[ $delta_i -gt 15000 ]]; then
if [ "$PAUSED" = 1 ]; then if [ "$PAUSED" = 1 ]; then
INFO="{\"id\":null,\"artist\":null,\"song\":null,\"image600\": \"$(get_radio | jq -r '.icon_fill_white')\"}" INFO="{\"id\":null,\"artist\":null,\"song\":null,\"image600\": \"$(get_radio | jq -r '.icon_fill_white')\"}"
else else
INFO=$(echo "$INFO" "$(get_song)" | jq -s ' reduce .[] as $item ( {}; . + ( INFO=$(echo "$INFO" "$(get_song)" | jq -s ' reduce .[] as $item ( {}; . + (
reduce ($item | to_entries[]) as $entry ( {}; if $entry.value != null then .[$entry.key] = $entry.value else . end reduce ($item | to_entries[]) as $entry ( {}; if $entry.value != null then .[$entry.key] = $entry.value else . end
)) )') )) )')
fi fi
last_time_info=$current_time last_time_info=$current_time
fi fi
done done
} }
# Main script # Main script
case "$1" in case "$1" in
"start") "start")
RADIO_ID=$2 RADIO_ID=$2
start start
;; ;;
"toggle") "toggle")
toggle toggle
;; ;;
*) *)
status_loop status_loop
;; ;;
esac esac