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

59 lines
1.5 KiB
Plaintext
Raw Normal View History

2016-12-23 00:10:40 -08:00
#!/usr/bin/env bash
2017-04-19 11:20:27 -07:00
# 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.
#
# Usage: cleanup-music
2016-12-23 00:10:40 -08:00
set -e
shopt -s nullglob
2017-04-19 11:20:27 -07:00
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}"
playlistfile="${playlistdir}/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="${musicdir}/${song}"
[[ -f $songpath ]] && to_delete+=("$songpath")
done < "$playlistfile"
if (( ${#to_delete[@]} == 0 )); then
echo "Nothing to delete"
exit
fi
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..."
2017-04-19 11:20:27 -07:00
for file in "${to_delete[@]}"; do
rm -fv "$file"
done
;;
n|N) echo "Aborted"; exit ;;
*) echo "Invalid" ;;
esac
2016-12-23 00:10:40 -08:00
:>"$playlistfile"
2017-04-19 11:20:27 -07:00
# Remove duplicates across all playlists
for playlist in ${playlistdir}/*.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
find "$HOME/music" -type d -empty -delete