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
|
|
|
|
2016-12-28 23:39:21 -08:00
|
|
|
set -e
|
2017-02-28 16:04:35 -08:00
|
|
|
shopt -s nullglob
|
2016-12-28 23:39:21 -08:00
|
|
|
|
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'
|
2017-02-28 16:04:35 -08:00
|
|
|
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"
|
2017-02-28 16:04:35 -08:00
|
|
|
|
|
|
|
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
|
2017-02-28 16:04:35 -08:00
|
|
|
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
|
2017-02-28 16:04:35 -08:00
|
|
|
awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp"
|
2017-01-05 22:30:30 -08:00
|
|
|
if [[ -f "${playlist}.tmp" ]]; then
|
|
|
|
rm -f "$playlist"
|
|
|
|
mv "${playlist}.tmp" "$playlist"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2016-12-28 23:39:21 -08:00
|
|
|
# Remove empty directories
|
|
|
|
find "$HOME/music" -type d -empty -delete
|