Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 92 lines (79 sloc) 1.44 KB
#!/usr/bin/env bash
#
# Play and stop radio automagically.
# Don't play if it's a holiday.
#
# Rafael Cavalcanti
#
MPC="mpc -q"
CURL="curl -sS"
PACTL="pactl"
HOLIDAYS="/opt/rc/data/holidays.txt"
PA_SINK="alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo"
SILENT_TRACK="http://files.lan/pi/silence.ogg"
RADIO_API="http://radio.lan/api"
usage() {
cat <<-END >&2
Usage:
$0 --start volume stream backup_stream
$0 --stop volume
END
exit 1
}
check_holiday() {
if grep -q "$(date +%Y-%m-%d)" "$HOLIDAYS"; then
printf "Today is a holiday. Not doing anything.\n" >&2
exit 0
fi
}
# Set volume even if MPC doesn't have a source on PulseAudio
# Volume as the argument
set_volume() {
# Make sure PA sink isn't mute and it's at 100%.
$PACTL set-sink-mute $PA_SINK false
$PACTL set-sink-volume $PA_SINK 100%
$MPC clear
$MPC add $SILENT_TRACK
$MPC play
sleep 10
$MPC volume $1
$MPC clear
}
start_playback() {
$CURL \
--data-urlencode "cmd=play_stream" \
--data-urlencode "type=name" \
--data-urlencode "value=$1" \
--data-urlencode "queue=$2" \
$RADIO_API
}
stop_playback() {
$MPC stop
}
start() {
check_holiday
set_volume "$1"
start_playback "$2" "$3"
}
stop() {
check_holiday
stop_playback
set_volume "$1"
}
# Check arguments
[[ $# -eq 0 ]] && usage
command="$1"
shift
case "$command" in
--start)
[[ $# != 3 ]] && usage
start "$1" "$2" "$3"
;;
--stop)
[[ $# != 1 ]] && usage
stop "$1"
;;
*)
usage
;;
esac