ptgdp: Add dry run, overhaul erroring syntax

This commit is contained in:
Salt 2019-07-28 17:46:30 -05:00
parent 107937bb41
commit d699c8195f

88
ptgdp
View File

@ -28,9 +28,11 @@ log() {
} }
error() { error() {
[ -z "$1" ] && return 1 # Message body [ -z "$1" ] && return 1 # Message body
[ -z "$2" ] && return 2 # Exit code # 2: Exit code
log "$1" printf "%s: \\e[31m%s\\e[0m\\n" \
exit "${2:-1}" "$_name" \
"$1"
[ -n "$2" ] && exit "${2:-1}"
} }
notify() { notify() {
[ -z "$_optrofi" ] && return 0 [ -z "$_optrofi" ] && return 0
@ -85,6 +87,9 @@ files are cached in your Music folder under "JPTGDP Songs" for offline use.
-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
Conflicts with -p Conflicts with -p
-D Dry run; parse out all songs, downloaded or not, and
print out the resolved names. Useful for testing, as
YouTube searches can sometimes be finicky.
-p Play the playlist after it is enqueued. -p Play the playlist after it is enqueued.
Conflicts with -d Conflicts with -d
-s Shuffle the playlist -s Shuffle the playlist
@ -146,35 +151,55 @@ playlist() {
rm "$_tmpfile"* > /dev/null 2>&1 rm "$_tmpfile"* > /dev/null 2>&1
sanitize "$line" sanitize "$line"
filename="$_musicdir/$_return" filename="$_musicdir/$_return"
if ! [ -f "$filename" ]; then if [ -z "$_optdryrun" ]; then
log "Finding a song for \"$line\"" if ! [ -f "$filename" ]; then
youtube-dl \ log "Finding a song for \"$line\""
--add-metadata \ youtube-dl \
--audio-format "best" \ --add-metadata \
--geo-bypass \ --audio-format "best" \
--playlist-items 1 \ --geo-bypass \
-x \ --playlist-items 1 \
-o "$_tmpfile.%(ext)s" \ -x \
ytsearch:"$line" \ -o "$_tmpfile.%(ext)s" \
> /dev/null 2>&1 & ytsearch:"$line" \
if wait $!; then > /dev/null 2>&1 &
dlsuccess+=1 if wait $!; then
mv "$_tmpfile"* "$filename" dlsuccess+=1
mv "$_tmpfile"* "$filename"
else
dlfailure+=1
notify "Could not download song" \
"youtube-dl did not download a song for \"$line\", either because it is out of date or because it could not find a video to rip from" \
normal dialog-error 3000
error "Could not download song \"$line\""
continue
fi
else else
dlfailure+=1 dlexist+=1
notify "Could not download song" \ fi
"youtube-dl did not download a song for \"$line\", either because it is out of date or because it could not find a video to rip from" \ [ -z "$_optdownloadonly" ] && audacious -e "$filename"
normal dialog-error 3000 if [ -n "$_optautoplay" ]; then
log "Could not download song \"$line\"" audacious -p
continue unset _optautoplay
fi fi
else else
dlexist+=1 output="$(
fi youtube-dl \
[ -z "$_optdownloadonly" ] && audacious -e "$filename" --get-title \
if [ -n "$_optautoplay" ]; then --geo-bypass \
audacious -p --playlist-items 1 \
unset _optautoplay ytsearch:"$line" 2>&1
)"
exitcode="$?"
if [ $exitcode -gt 0 ]; then
error "Could not find song \"$line\""
continue
fi
if ! [ "$output" = "${output#*WARNING}" ]; then
log "$line parsed, but title could not be extracted"
else
log "$line - \"$output\""
fi
fi fi
done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi) done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi)
if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; then if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; then
@ -203,7 +228,7 @@ main() {
trap trapexit EXIT trap trapexit EXIT
# Actual program stuff # Actual program stuff
while getopts ":cdf:pr:sh" opt; do while getopts ":cdDf:pr:sh" opt; do
case $opt in case $opt in
c) c)
clearcache clearcache
@ -212,6 +237,9 @@ main() {
d) d)
_optdownloadonly=1 _optdownloadonly=1
;; ;;
D)
_optdryrun=1
;;
f) f)
_optfile="$OPTARG" _optfile="$OPTARG"
;; ;;