shell/+mpd: refactor bin scripts

This commit is contained in:
Henrik Lissner 2017-04-26 23:26:01 -04:00
parent 7fb8491be7
commit 91dc932353
3 changed files with 89 additions and 78 deletions

View file

@ -1,34 +1,36 @@
#!/usr/bin/env bash
# I frequently discover some of my music is simply awful. So I file them away in
# my 1-star.m3u playlist. This script reads those songs, deletes them, and
# cleans up any empty folders it leaves behind.
# I frequently discover that there are crappy songs hidden in my music library.
# I ain't havin' none o' that! So I file them away in my 1-star.m3u playlist.
# This script reads that playlist, delete those songs, and cleans up any empty
# folders it leaves behind. It will also remove duplicates from all playlists.
#
# Usage: cleanup-music
set -e
shopt -s nullglob
musicdir=$(awk '/music_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
musicdir="${musicdir/#\~/$HOME}"
playlistdir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
playlistdir="${playlistdir/#\~/$HOME}"
music_dir=$(awk '/music_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
music_dir="${music_dir/#\~/$HOME}"
playlist_dir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
playlist_dir="${playlist_dir/#\~/$HOME}"
playlistfile="${playlistdir}/1-star.m3u"
playlist_file="${playlist_dir}/1-star.m3u"
# Delete designated songs
IFS=$'\n'
declare -a to_delete=()
while read -r song; do
songpath="${musicdir}/${song}"
songpath="${music_dir}/${song}"
[[ -f $songpath ]] && to_delete+=("$songpath")
done < "$playlistfile"
done < "$playlist_file"
if (( ${#to_delete[@]} == 0 )); then
echo "Nothing to delete"
exit
fi
# List songs to delete and ask for confirmation
printf "Deleting %s songs:\n\n%s\n\nConfirm? (y/n) " \
"${#to_delete[@]}" \
"$(IFS=$'\n'; printf "%s" "${to_delete[*]}")"
@ -43,10 +45,10 @@ case $confirm in
n|N) echo "Aborted"; exit ;;
*) echo "Invalid" ;;
esac
:>"$playlistfile"
:>"$playlist_file"
# Remove duplicates across all playlists
for playlist in ${playlistdir}/*.m3u; do
# Remove duplicates across playlists
for playlist in ${playlist_dir}/*.m3u; do
awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp"
if [[ -f "${playlist}.tmp" ]]; then
rm -f "$playlist"
@ -55,4 +57,4 @@ for playlist in ${playlistdir}/*.m3u; do
done
# Remove empty directories
find "$HOME/music" -type d -empty -delete
find "$music_dir" -type d -empty -delete

View file

@ -4,43 +4,39 @@
# native notifications on MacOS), announcing the currently playing song. Used by
# ncmpcpp.
_color() { printf '<span fgcolor="%s">%s</span>' "$1" "$2"; }
if ! pgrep mpd >/dev/null; then
>&2 echo "mpd isn't running."
exit 1
fi
info=$(mpc -f ";%artist%;%album%;%title%;%file%")
if [[ -z $info || ${info:0:1} != ";" ]]; then
info=$(mpc current -f ";%artist%;%album%;%title%;%file%")
if [[ ${info:0:1} != ";" ]]; then
>&2 echo "nothing is playing."
exit
exit 1
fi
artist=$(awk -F';' '{ print $2 }' <<<"$info")
album=$(awk -F';' '{ print $3 }' <<<"$info")
title=$(awk -F';' '{ print $4 }' <<<"$info")
file=$(awk -F';' '{ print $5 }' <<<"$info")
IFS=';' read -r _ artist album title file <<< "$info"
artist="${artist:-No Artist}"
album="${album:-No Album}"
title="${title:-Untitled}"
artist="${artist:-$(_color "#757575" "No Artist")}"
album="${album:-$(_color "#757575" "No Album")}"
title="${title:-$(_color "#757575" "Untitled")}"
rating=
stars=$(cd ~/music/.db/playlists/ && grep "$file" ./*-star.m3u)
if [[ $stars ]]; then
case $stars in
./1-star.m3u*) starn=1 ;;
./2-star.m3u*) starn=2 ;;
./3-star.m3u*) starn=3 ;;
./4-star.m3u*) starn=4 ;;
./5-star.m3u*) starn=5 ;;
esac
if [[ $starn ]]; then
case $(grep "$file" ~/music/.db/playlists/*-star.m3u) in
*/1-star.m3u:*) starn=1 ;;
*/2-star.m3u:*) starn=2 ;;
*/3-star.m3u:*) starn=3 ;;
*/4-star.m3u:*) starn=4 ;;
*/5-star.m3u:*) starn=5 ;;
esac
if [[ $starn ]]; then
star_filled_icon="★"
star_empty_icon="☆"
if [[ $OSTYPE == linux* ]]; then
star_filled_icon="<span fgcolor='#8fb1cd'>${star_filled_icon}</span>"
star_empty_icon="<span fgcolor='#666666'>${star_empty_icon}</span>"
star_filled_icon=$(_color "#8fb1cd" "$star_filled_icon")
star_empty_icon=$(_color "#666666" "$star_empty_icon")
fi
for ((i=1;i<=5;i++)); do
@ -50,7 +46,6 @@ if [[ $stars ]]; then
rating="${rating}${star_empty_icon}"
fi
done
fi
fi
case $OSTYPE in

View file

@ -10,57 +10,71 @@
# Else if 1 <= rating <= 5,
# then clear it from all playlists,
# and add it to ${rating}-star.m3u
# Else, error out
# Else
# display the current rating of the playing song
#
# Usage: rate [N]
if (( $# != 1 )); then
echo "Usage: rate [N]"
exit 1
fi
rating="$1"
playlistdir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
playlistdir="${playlistdir/#\~/$HOME}"
currentsong=$(mpc current -f '%file%')
if [[ -z "$currentsong" ]]; then
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
>&2 echo 'Nothing is playing!'
exit 1
fi
#
_clearPlaylists() {
_clear_playlists() {
# Remove the song from other playlists
for playlist in "${playlistdir}/"*-star.m3u; do
for playlist in "${playlist_dir}/"*-star.m3u; do
if grep "^$1$" "$playlist" >/dev/null; then
sed -i "\#^$1\$#d" "$playlist"
fi
done
}
#
target="${playlistdir}/${rating}-star.m3u"
if (( rating == 0 )); then
_clearPlaylists "$currentsong"
echo "Rating cleared"
elif [[ ! -f "$target" ]]; then
>&2 echo "Not a valid rating, must be between 0-5!"
exit 2
elif ! grep "^${currentsong}$" "$target" >/dev/null; then
_clearPlaylists "$currentsong"
echo "$currentsong" >> "$target"
fi
# Display new rating
stars=""
star_filled_icon="★"
star_empty_icon="☆"
for ((i=1;i<=5;i++)); do
if (( "$rating" >= "$i" )); then
_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) $currentsong"
done
echo "$stars"
}
#
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"
if (( rating == 0 )); then
_clear_playlists "$song_file"
echo "Rating cleared"
elif [[ ! -f "$target" ]]; then
>&2 echo "Not a valid rating, must be between 0-5!"
exit 2
elif ! grep "^${song_file}$" "$target" >/dev/null; then
_clear_playlists "$song_file"
echo "$song_file" >> "$target"
fi
# Display new rating
echo "($(_display_stars $rating)) $song_title"