dotfiles/.local/bin/scripts/pl

40 lines
2.0 KiB
Plaintext
Raw Normal View History

2021-09-29 16:36:17 +00:00
#!/bin/sh
2021-10-14 18:26:08 +00:00
# Playlist management
2021-09-29 16:36:17 +00:00
# optdepends: highlight python
# env: MUSIC
test $# -gt 0 && command=$1 && shift
2021-10-14 18:26:08 +00:00
test "$1" = '-v' && verbose='true' && shift || verbose='false'
! test -d "$MUSIC" && echo "No music directory!" && exit 1
2021-09-29 16:36:17 +00:00
case $command in
(update)
for pl; do
2022-07-06 06:59:22 +00:00
highlight $pl >&2
bak="/tmp/$pl"
cat $pl | tee "$bak" | rev | cut -d'/' -f-2 | rev | while read f; do
2021-10-14 18:26:08 +00:00
$verbose && printf "\e[31;1mSearching for '$f'\e[0m\n" >&2
2021-09-29 16:36:17 +00:00
test -n "$f" || continue
2021-10-12 11:55:22 +00:00
newpath="$(find $MUSIC -path "*${f#*../}" -prune | grep . || find $MUSIC -path "*${f##* - }" -o -path "*${f##*/}" | grep -m 1 .)"
2021-10-14 18:26:08 +00:00
$verbose && printf "\e[31;1mFound '$newpath'\e[0m\n" >&2
2021-10-12 11:55:22 +00:00
test -n "$newpath" && realpath --relative-to $(dirname "$pl") "$newpath" || echo "$f"
2021-09-29 16:36:17 +00:00
done | tee $pl
2022-07-06 06:59:22 +00:00
echo "Backup of $pl saved to $bak" >&2
2021-09-29 16:36:17 +00:00
done;;
(edit) ${EDITOR:-nano} $(find $MUSIC/Playlists -iname "$1\.*" | grep . || find $MUSIC/Playlists -iname "$1*");;
(make)
if test $1 = d
then shift && i=1; while test $i -le ${1:-9}; do $0 make "${2:-Ω}$i" "empty$i"; ((i++)); done
else echo ${2:-empty} >$1.m3u8
fi;;
(fix) sed -i 's/.*\/\(musi[kc]\|janek\)/../g' "$@";;
(cd) dir="${2:-cd}"
mkdir "$dir"
cat "$1" | python -c "import sys, urllib as ul; [sys.stdout.write(ul.unquote_plus(l)) for l in sys.stdin]" | sed '=' | sed -r 's/^[0-9]$/0\0/' | sed -r "N; s/(.*)\n(.*\/[0-9 ]*(.*))/'\2'\n'$dir\/\1 \3'/" | xargs -L 2 cp;;
2021-09-29 16:36:17 +00:00
(*) echo "Unknown subcommand '$1' - Possible subcommands:
update - update file paths for moved songs in a playlist
edit - open playlist in default editor
make - create an empty playlist with given name in current directory (optional second arg provides content)
> d - creates up to 9 dummy files
fix - replace absolute by relative paths for all given files (assumes library root at musi[kc] and playlist file one level below)
cd - copy all songs of a playlist into a folder with indexed names, so they can easily be burned to a CD"
esac