Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from moopet/mpc-guardian.sh
Last active January 26, 2025 06:23
Show Gist options
  • Save bonelifer/75ff3b7b455ec8eac9ce7e06f6808452 to your computer and use it in GitHub Desktop.
Save bonelifer/75ff3b7b455ec8eac9ce7e06f6808452 to your computer and use it in GitHub Desktop.
With a shared MPD server, sometimes other people will add shit music. Stop the rot. Also watch Christmas genre and limit it to specific date range.
#!/usr/bin/env bash
# Automatically removes songs from your playlist that match a blacklist or the Christmas genre
# unless it is between the day after U.S. Thanksgiving and before January 15.
BLACKLIST=/var/music/blacklist.txt
MPD_HOST=localhost
REJECT_GENRE="Christmas"
# Define the date range
START_DATE=$(date -d "fourth Thursday of November + 1 day" +%Y-%m-%d) # Day after Thanksgiving
END_DATE=$(date -d "January 15" +%Y-%m-%d)
[ -f "$BLACKLIST" ] || touch "$BLACKLIST"
is_outside_date_range() {
local today=$(date +%Y-%m-%d)
[[ "$today" < "$START_DATE" || "$today" > "$END_DATE" ]]
}
while :; do
# Get the currently playing song
CURRENT_SONG=$(mpc current)
# Check against the blacklist
if echo "$CURRENT_SONG" | grep -i -f "$BLACKLIST" > /dev/null; then
echo "Removing blacklisted song: $CURRENT_SONG"
mpc del "$(mpc status -f %position% | head -1)"
sleep 5
continue
fi
# Get genre information for the current song
GENRE=$(mpc --format "%genre%" current)
# Remove the song if it's in the rejected genre and outside the allowed date range
if [[ "$GENRE" =~ $REJECT_GENRE ]] && is_outside_date_range; then
echo "Removing song from rejected genre ($REJECT_GENRE) outside the allowed date range: $CURRENT_SONG"
mpc del "$(mpc status -f %position% | head -1)"
fi
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment