helm/shell/+mpd/bin/mpd-rate

81 lines
2 KiB
Plaintext
Raw Normal View History

2017-04-19 12:40:26 -07:00
#!/usr/bin/env bash
# Rate the currently playing song in mpd.
#
# Under the hood, this simply appends the playing song to the
# $playlist_directory/$N-star.m3u playlist.
#
# If rating = 0,
# then clear it from all playlists
# Else if 1 <= rating <= 5,
# then clear it from all playlists,
# and add it to ${rating}-star.m3u
2017-04-26 20:26:01 -07:00
# Else
# display the current rating of the playing song
2017-04-19 12:40:26 -07:00
#
# Usage: rate [N]
2017-04-26 20:26:01 -07:00
playlist_dir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
playlist_dir="${playlist_dir/#\~/$HOME}"
song_file=$(mpc current -f '%file%')
song_title=$(mpc current)
if [[ -z "$song_file" ]]; then
2017-04-19 12:40:26 -07:00
>&2 echo 'Nothing is playing!'
exit 1
fi
#
2017-04-26 20:26:01 -07:00
_clear_playlists() {
2017-04-19 12:40:26 -07:00
# Remove the song from other playlists
2017-04-26 20:26:01 -07:00
for playlist in "${playlist_dir}/"*-star.m3u; do
2017-04-19 12:40:26 -07:00
if grep "^$1$" "$playlist" >/dev/null; then
sed -i "\#^$1\$#d" "$playlist"
fi
done
}
2017-04-26 20:26:01 -07:00
_display_stars() {
# Display new rating
stars=""
2017-10-07 08:33:26 -07:00
star_filled_icon='\033[0;32m*\033[0m'
star_empty_icon='\033[0;30m*\033[0m'
2017-04-26 20:26:01 -07:00
for ((i=1;i<=5;i++)); do
if (( "$1" >= "$i" )); then
stars="${stars}${star_filled_icon}"
else
stars="${stars}${star_empty_icon}"
fi
done
2017-10-07 08:33:26 -07:00
printf "$stars\n"
2017-04-26 20:26:01 -07:00
}
2017-04-19 12:40:26 -07:00
#
2017-04-26 20:26:01 -07:00
if (( $# != 1 )); then
for ((i=1;i<=5;i++)); do
if grep "^${song_file}$" "${playlist_dir}/"${i}-star.m3u >/dev/null; then
echo "($(_display_stars $i)) $song_title"
exit 1
fi
done
echo "($(_display_stars 0)) $song_title"
exit 1
fi
rating="$1"
target="${playlist_dir}/${rating}-star.m3u"
2017-04-19 12:40:26 -07:00
if (( rating == 0 )); then
2017-04-26 20:26:01 -07:00
_clear_playlists "$song_file"
2017-04-19 12:40:26 -07:00
echo "Rating cleared"
elif [[ ! -f "$target" ]]; then
>&2 echo "Not a valid rating, must be between 0-5!"
exit 2
2017-04-26 20:26:01 -07:00
elif ! grep "^${song_file}$" "$target" >/dev/null; then
_clear_playlists "$song_file"
echo "$song_file" >> "$target"
2017-04-19 12:40:26 -07:00
fi
# Display new rating
2017-04-26 20:26:01 -07:00
echo "($(_display_stars $rating)) $song_title"