ptgdp: Switch from a getopts arg to parsing out program arguments for playlists

This is a breaking change
It's also actually the right way to do things and it comes with multiple playlist support
This commit is contained in:
Salt 2019-09-01 23:33:04 -05:00
parent c4023a1d8d
commit 4e5de57f02

45
ptgdp
View File

@ -18,6 +18,7 @@ _xdguserdirs="${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
_musicdir="${XDG_MUSIC_DIR:-$HOME/Music}" _musicdir="${XDG_MUSIC_DIR:-$HOME/Music}"
_ptgdpmusicdir="$_musicdir/${PTGDP_MUSIC_DIR:-PTGDP Songs}" _ptgdpmusicdir="$_musicdir/${PTGDP_MUSIC_DIR:-PTGDP Songs}"
declare -a _queue declare -a _queue
declare -a _playlists
# Helper functions # Helper functions
log() { log() {
@ -177,8 +178,15 @@ execqueue() {
file=${file##$_musicdir/} file=${file##$_musicdir/}
mpc add "$file" > /dev/null 2>&1 || error "Could not add file: \"$file\"" mpc add "$file" > /dev/null 2>&1 || error "Could not add file: \"$file\""
done done
}
execplaylists() {
# Plays all playlists in _playlists
for list in "${_playlists[@]}"; do
playlist "$list" || error "Failed to play playlist: \"$list\""
done
if [ -n "$_optautoplay" ]; then if [ -n "$_optautoplay" ]; then
mpc play > /dev/null 2>&1 mpc play > /dev/null 2>&1
log "Started playback"
unset _optautoplay unset _optautoplay
fi fi
} }
@ -232,13 +240,9 @@ clearcache() {
} }
helptext() { helptext() {
cat << EOF cat << EOF
Usage: $_name [OPTION] Usage: $_name [OPTION]... [FILE]...
Use youtube-dl and mpc to queue up a playlist given a file of only search Use youtube-dl and a music player to queue up or download a number of songs
queries. The first result found is the one that will be downloaded. Downloaded given plaintext FILEs with only search queries.
files are cached in your Music folder under "PTGDP Songs" for offline use.
-f <file> The playlist file to load. The file should be plaintext
containing a YouTube search query on each line.
Comments are supported and prepended with #
-c Clears the cache (which can become quite large) -c Clears the cache (which can become quite large)
-d Download only; don't queue anything up -d Download only; don't queue anything up
@ -303,7 +307,8 @@ rofimenu() {
choice="$(rofi -dmenu -i -p "$prompt" <<< "$playlists" 2>/dev/null)" choice="$(rofi -dmenu -i -p "$prompt" <<< "$playlists" 2>/dev/null)"
[ -z "$choice" ] && error "User aborted at selection" 62 [ -z "$choice" ] && error "User aborted at selection" 62
if [ -z "$_optrofisong" ]; then if [ -z "$_optrofisong" ]; then
playlist "$_optrofi"/"$choice".gdp _playlists+=("$_optrofi"/"$choice".gdp)
execplaylists
else else
rofiplaysong "$_optrofi"/"$choice".gdp rofiplaysong "$_optrofi"/"$choice".gdp
fi fi
@ -401,7 +406,7 @@ main() {
mkdir -p "$_ptgdpmusicdir" mkdir -p "$_ptgdpmusicdir"
trap trapexit EXIT trap trapexit EXIT
# Actual program stuff # Get options
while getopts ":cdDf:pr:R:sh" opt; do while getopts ":cdDf:pr:R:sh" opt; do
case $opt in case $opt in
c) c)
@ -414,9 +419,6 @@ main() {
D) D)
_optdryrun=1 _optdryrun=1
;; ;;
f)
_optfile="$OPTARG"
;;
p) p)
_optautoplay=1 _optautoplay=1
;; ;;
@ -442,12 +444,25 @@ main() {
;; ;;
esac esac
done done
# Get program arguments (playlists)
shift $((OPTIND - 1))
for list in "$@"; do
if ! [ -f "$list" ]; then
error "Failed to find playlist: $list"
continue
fi
_playlists+=("$list")
done
[ -z "$_optdownloadonly" ] && [ -z "$_optdryrun" ] && ! validatedeps mpc && error "$_return is required outside of dry- and download-only runs" [ -z "$_optdownloadonly" ] && [ -z "$_optdryrun" ] && ! validatedeps mpc && error "$_return is required outside of dry- and download-only runs"
[ -n "$_optfile" ] && [ -n "$_optrofi" ] && error "Flags -f and -r conflict" 2 [ -n "$_playlists" ] && [ -n "$_optrofi" ] && error "Flag -r cannot be used with playlist arguments" 2
[ -n "$_optdownloadonly" ] && [ -n "$_optautoplay" ] && error "Flags -d and -p conflict" 2 [ -n "$_optdownloadonly" ] && [ -n "$_optautoplay" ] && error "Flags -d and -p conflict" 2
if [ -n "$_optrofi" ]; then rofimenu; exit $?; fi if [ -n "$_optrofi" ]; then rofimenu; exit $?; fi
if [ -n "$_optfile" ]; then playlist "$_optfile"; exit $?; fi if [ -n "$_playlists" ]; then
error "Nothing to do" 0 execplaylists
exit $?
else
error "Nothing to do" 1
fi
} }
main "$@" main "$@"