helm/shell/mpd+ncmpcpp/bin/mpd-cleanup

61 lines
1.6 KiB
Plaintext
Raw Normal View History

2016-12-23 00:10:40 -08:00
#!/usr/bin/env bash
2017-04-26 20:26:01 -07:00
# 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.
2017-04-19 11:20:27 -07:00
#
# Usage: cleanup-music
2016-12-23 00:10:40 -08:00
set -e
shopt -s nullglob
2017-04-26 20:26:01 -07:00
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}"
2017-04-19 11:20:27 -07:00
2017-04-26 20:26:01 -07:00
playlist_file="${playlist_dir}/1-star.m3u"
2016-12-23 00:10:40 -08:00
# Delete designated songs
2017-04-19 11:20:27 -07:00
IFS=$'\n'
declare -a to_delete=()
2017-04-19 11:20:27 -07:00
while read -r song; do
songpath="${music_dir}/${song}"
[[ -f $songpath ]] && to_delete+=("$songpath")
2017-04-26 20:26:01 -07:00
done < "$playlist_file"
if (( ${#to_delete[@]} == 0 )); then
echo "Nothing to delete"
exit
fi
2017-04-26 20:26:01 -07:00
# 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[*]}")"
read -r confirm
case $confirm in
y|Y)
echo "Deleting..."
for file in "${to_delete[@]}"; do
rm -fv "$file"
done
;;
n|N) echo "Aborted"; exit ;;
*) echo "Invalid" ;;
esac
2017-04-26 20:26:01 -07:00
:>"$playlist_file"
2016-12-23 00:10:40 -08:00
2017-04-26 20:26:01 -07:00
# 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"
mv "${playlist}.tmp" "$playlist"
fi
done
# Remove empty directories
2017-04-26 20:26:01 -07:00
find "$music_dir" -type d -empty -delete