-
-
Save schinken/16af477869ad9f4221ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -eu | |
MQTT_HOST=mqtt | |
VOTE_TOPIC=soundboard/vote | |
SOUNDFILE_DIR=/mnt/nfs/soundboard/files | |
VOTES_FILE=/mnt/nfs/soundboard/votes.json | |
THRESHOLD=-10 | |
if [ ! -d "$SOUNDFILE_DIR" ]; then | |
echo "$SOUNDFILE_DIR is not a directory" >&2 | |
exit 1 | |
fi | |
if [ ! -f "$VOTES_FILE" ]; then | |
echo '{}' > $VOTES_FILE | |
fi | |
mosquitto_sub -h "$MQTT_HOST" -t "$VOTE_TOPIC/+" -v | while read -r event; do | |
topic="${event% [+-]}" | |
[ -z "$topic" -o "$topic" = "$event" ] && continue | |
sound="$(basename -- "$topic")" | |
vote="${event##* }" | |
case "$vote" in | |
-) diff=-1;; | |
+) diff=1;; | |
*) continue;; | |
esac | |
jq ".\"$sound\" += $diff" <"$VOTES_FILE" >"${VOTES_FILE}.new" | |
mv "${VOTES_FILE}.new" "$VOTES_FILE" | |
newval=$(jq ".\"$sound\"" <"$VOTES_FILE") | |
[ "$newval" -le "$THRESHOLD" ] && rm -f "$SOUNDFILE_DIR/$sound" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment