shell/+mpd: refactor bin scripts
This commit is contained in:
parent
7fb8491be7
commit
91dc932353
|
@ -1,34 +1,36 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# I frequently discover some of my music is simply awful. So I file them away in
|
# I frequently discover that there are crappy songs hidden in my music library.
|
||||||
# my 1-star.m3u playlist. This script reads those songs, deletes them, and
|
# I ain't havin' none o' that! So I file them away in my 1-star.m3u playlist.
|
||||||
# cleans up any empty folders it leaves behind.
|
# 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
|
# Usage: cleanup-music
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
shopt -s nullglob
|
shopt -s nullglob
|
||||||
|
|
||||||
musicdir=$(awk '/music_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
|
music_dir=$(awk '/music_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
|
||||||
musicdir="${musicdir/#\~/$HOME}"
|
music_dir="${music_dir/#\~/$HOME}"
|
||||||
playlistdir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
|
playlist_dir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
|
||||||
playlistdir="${playlistdir/#\~/$HOME}"
|
playlist_dir="${playlist_dir/#\~/$HOME}"
|
||||||
|
|
||||||
playlistfile="${playlistdir}/1-star.m3u"
|
playlist_file="${playlist_dir}/1-star.m3u"
|
||||||
|
|
||||||
# Delete designated songs
|
# Delete designated songs
|
||||||
IFS=$'\n'
|
IFS=$'\n'
|
||||||
declare -a to_delete=()
|
declare -a to_delete=()
|
||||||
while read -r song; do
|
while read -r song; do
|
||||||
songpath="${musicdir}/${song}"
|
songpath="${music_dir}/${song}"
|
||||||
[[ -f $songpath ]] && to_delete+=("$songpath")
|
[[ -f $songpath ]] && to_delete+=("$songpath")
|
||||||
done < "$playlistfile"
|
done < "$playlist_file"
|
||||||
|
|
||||||
if (( ${#to_delete[@]} == 0 )); then
|
if (( ${#to_delete[@]} == 0 )); then
|
||||||
echo "Nothing to delete"
|
echo "Nothing to delete"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# List songs to delete and ask for confirmation
|
||||||
printf "Deleting %s songs:\n\n%s\n\nConfirm? (y/n) " \
|
printf "Deleting %s songs:\n\n%s\n\nConfirm? (y/n) " \
|
||||||
"${#to_delete[@]}" \
|
"${#to_delete[@]}" \
|
||||||
"$(IFS=$'\n'; printf "%s" "${to_delete[*]}")"
|
"$(IFS=$'\n'; printf "%s" "${to_delete[*]}")"
|
||||||
|
@ -43,10 +45,10 @@ case $confirm in
|
||||||
n|N) echo "Aborted"; exit ;;
|
n|N) echo "Aborted"; exit ;;
|
||||||
*) echo "Invalid" ;;
|
*) echo "Invalid" ;;
|
||||||
esac
|
esac
|
||||||
:>"$playlistfile"
|
:>"$playlist_file"
|
||||||
|
|
||||||
# Remove duplicates across all playlists
|
# Remove duplicates across playlists
|
||||||
for playlist in ${playlistdir}/*.m3u; do
|
for playlist in ${playlist_dir}/*.m3u; do
|
||||||
awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp"
|
awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp"
|
||||||
if [[ -f "${playlist}.tmp" ]]; then
|
if [[ -f "${playlist}.tmp" ]]; then
|
||||||
rm -f "$playlist"
|
rm -f "$playlist"
|
||||||
|
@ -55,4 +57,4 @@ for playlist in ${playlistdir}/*.m3u; do
|
||||||
done
|
done
|
||||||
|
|
||||||
# Remove empty directories
|
# Remove empty directories
|
||||||
find "$HOME/music" -type d -empty -delete
|
find "$music_dir" -type d -empty -delete
|
||||||
|
|
|
@ -4,43 +4,39 @@
|
||||||
# native notifications on MacOS), announcing the currently playing song. Used by
|
# native notifications on MacOS), announcing the currently playing song. Used by
|
||||||
# ncmpcpp.
|
# ncmpcpp.
|
||||||
|
|
||||||
|
_color() { printf '<span fgcolor="%s">%s</span>' "$1" "$2"; }
|
||||||
|
|
||||||
if ! pgrep mpd >/dev/null; then
|
if ! pgrep mpd >/dev/null; then
|
||||||
>&2 echo "mpd isn't running."
|
>&2 echo "mpd isn't running."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
info=$(mpc -f ";%artist%;%album%;%title%;%file%")
|
info=$(mpc current -f ";%artist%;%album%;%title%;%file%")
|
||||||
if [[ -z $info || ${info:0:1} != ";" ]]; then
|
if [[ ${info:0:1} != ";" ]]; then
|
||||||
>&2 echo "nothing is playing."
|
>&2 echo "nothing is playing."
|
||||||
exit
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
artist=$(awk -F';' '{ print $2 }' <<<"$info")
|
IFS=';' read -r _ artist album title file <<< "$info"
|
||||||
album=$(awk -F';' '{ print $3 }' <<<"$info")
|
|
||||||
title=$(awk -F';' '{ print $4 }' <<<"$info")
|
|
||||||
file=$(awk -F';' '{ print $5 }' <<<"$info")
|
|
||||||
|
|
||||||
artist="${artist:-No Artist}"
|
artist="${artist:-$(_color "#757575" "No Artist")}"
|
||||||
album="${album:-No Album}"
|
album="${album:-$(_color "#757575" "No Album")}"
|
||||||
title="${title:-Untitled}"
|
title="${title:-$(_color "#757575" "Untitled")}"
|
||||||
rating=
|
rating=
|
||||||
|
|
||||||
stars=$(cd ~/music/.db/playlists/ && grep "$file" ./*-star.m3u)
|
case $(grep "$file" ~/music/.db/playlists/*-star.m3u) in
|
||||||
if [[ $stars ]]; then
|
*/1-star.m3u:*) starn=1 ;;
|
||||||
case $stars in
|
*/2-star.m3u:*) starn=2 ;;
|
||||||
./1-star.m3u*) starn=1 ;;
|
*/3-star.m3u:*) starn=3 ;;
|
||||||
./2-star.m3u*) starn=2 ;;
|
*/4-star.m3u:*) starn=4 ;;
|
||||||
./3-star.m3u*) starn=3 ;;
|
*/5-star.m3u:*) starn=5 ;;
|
||||||
./4-star.m3u*) starn=4 ;;
|
|
||||||
./5-star.m3u*) starn=5 ;;
|
|
||||||
esac
|
esac
|
||||||
|
|
||||||
if [[ $starn ]]; then
|
if [[ $starn ]]; then
|
||||||
star_filled_icon="★"
|
star_filled_icon="★"
|
||||||
star_empty_icon="☆"
|
star_empty_icon="☆"
|
||||||
if [[ $OSTYPE == linux* ]]; then
|
if [[ $OSTYPE == linux* ]]; then
|
||||||
star_filled_icon="<span fgcolor='#8fb1cd'>${star_filled_icon}</span>"
|
star_filled_icon=$(_color "#8fb1cd" "$star_filled_icon")
|
||||||
star_empty_icon="<span fgcolor='#666666'>${star_empty_icon}</span>"
|
star_empty_icon=$(_color "#666666" "$star_empty_icon")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for ((i=1;i<=5;i++)); do
|
for ((i=1;i<=5;i++)); do
|
||||||
|
@ -51,7 +47,6 @@ if [[ $stars ]]; then
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
case $OSTYPE in
|
case $OSTYPE in
|
||||||
darwin*)
|
darwin*)
|
||||||
|
|
|
@ -10,57 +10,71 @@
|
||||||
# Else if 1 <= rating <= 5,
|
# Else if 1 <= rating <= 5,
|
||||||
# then clear it from all playlists,
|
# then clear it from all playlists,
|
||||||
# and add it to ${rating}-star.m3u
|
# and add it to ${rating}-star.m3u
|
||||||
# Else, error out
|
# Else
|
||||||
|
# display the current rating of the playing song
|
||||||
#
|
#
|
||||||
# Usage: rate [N]
|
# Usage: rate [N]
|
||||||
|
|
||||||
if (( $# != 1 )); then
|
playlist_dir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
|
||||||
echo "Usage: rate [N]"
|
playlist_dir="${playlist_dir/#\~/$HOME}"
|
||||||
exit 1
|
song_file=$(mpc current -f '%file%')
|
||||||
fi
|
song_title=$(mpc current)
|
||||||
|
if [[ -z "$song_file" ]]; then
|
||||||
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
|
|
||||||
>&2 echo 'Nothing is playing!'
|
>&2 echo 'Nothing is playing!'
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#
|
#
|
||||||
_clearPlaylists() {
|
_clear_playlists() {
|
||||||
# Remove the song from other 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
|
if grep "^$1$" "$playlist" >/dev/null; then
|
||||||
sed -i "\#^$1\$#d" "$playlist"
|
sed -i "\#^$1\$#d" "$playlist"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
_display_stars() {
|
||||||
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
|
# Display new rating
|
||||||
stars=""
|
stars=""
|
||||||
star_filled_icon="★"
|
star_filled_icon="★"
|
||||||
star_empty_icon="[2m[37m☆[0m"
|
star_empty_icon="[2m[37m☆[0m"
|
||||||
for ((i=1;i<=5;i++)); do
|
for ((i=1;i<=5;i++)); do
|
||||||
if (( "$rating" >= "$i" )); then
|
if (( "$1" >= "$i" )); then
|
||||||
stars="${stars}${star_filled_icon}"
|
stars="${stars}${star_filled_icon}"
|
||||||
else
|
else
|
||||||
stars="${stars}${star_empty_icon}"
|
stars="${stars}${star_empty_icon}"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo "($stars) $currentsong"
|
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"
|
||||||
|
|
Loading…
Reference in a new issue