helm/shell/+mpd/bin/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=""
star_filled_icon="★"
star_empty_icon="☆"
for ((i=1;i<=5;i++)); do
if (( "$1" >= "$i" )); then
stars="${stars}${star_filled_icon}"
else
stars="${stars}${star_empty_icon}"
fi
done
echo "$stars"
}
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"