From d699c8195f06772c0eb8e8d9af03e70cc1a0d749 Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Sun, 28 Jul 2019 17:46:30 -0500
Subject: [PATCH] ptgdp: Add dry run, overhaul erroring syntax

---
 ptgdp | 88 +++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 58 insertions(+), 30 deletions(-)

diff --git a/ptgdp b/ptgdp
index 92f0fd8..ffb22cc 100755
--- a/ptgdp
+++ b/ptgdp
@@ -28,9 +28,11 @@ log() {
 }
 error() {
 	[ -z "$1" ] && return 1 # Message body
-	[ -z "$2" ] && return 2 # Exit code
-	log "$1"
-	exit "${2:-1}"
+	# 2: Exit code
+	printf "%s: \\e[31m%s\\e[0m\\n" \
+		"$_name" \
+		"$1"
+	[ -n "$2" ] && exit "${2:-1}"
 }
 notify() {
 	[ -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)
   -d			Download only; don't queue anything up
 			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.
 			Conflicts with -d
   -s			Shuffle the playlist
@@ -146,35 +151,55 @@ playlist() {
 		rm "$_tmpfile"* > /dev/null 2>&1
 		sanitize "$line"
 		filename="$_musicdir/$_return"
-		if ! [ -f "$filename" ]; then
-			log "Finding a song for \"$line\""
-			youtube-dl \
-				--add-metadata \
-				--audio-format "best" \
-				--geo-bypass \
-				--playlist-items 1 \
-				-x \
-				-o "$_tmpfile.%(ext)s" \
-				ytsearch:"$line" \
-				> /dev/null 2>&1 &
-			if wait $!; then
-				dlsuccess+=1
-				mv "$_tmpfile"* "$filename"
+		if [ -z "$_optdryrun" ]; then
+			if ! [ -f "$filename" ]; then
+				log "Finding a song for \"$line\""
+				youtube-dl \
+					--add-metadata \
+					--audio-format "best" \
+					--geo-bypass \
+					--playlist-items 1 \
+					-x \
+					-o "$_tmpfile.%(ext)s" \
+					ytsearch:"$line" \
+					> /dev/null 2>&1 &
+				if wait $!; then
+					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
-				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
-				log "Could not download song \"$line\""
-				continue
+				dlexist+=1
+			fi
+			[ -z "$_optdownloadonly" ] && audacious -e "$filename"
+			if [ -n "$_optautoplay" ]; then
+				audacious -p
+				unset _optautoplay
 			fi
 		else
-			dlexist+=1
-		fi
-		[ -z "$_optdownloadonly" ] && audacious -e "$filename"
-		if [ -n "$_optautoplay" ]; then
-			audacious -p
-			unset _optautoplay
+			output="$(
+				youtube-dl \
+					--get-title \
+					--geo-bypass \
+					--playlist-items 1 \
+					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
 	done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi)
 	if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; then
@@ -203,7 +228,7 @@ main() {
 	trap trapexit EXIT
 
 	# Actual program stuff
-	while getopts ":cdf:pr:sh" opt; do
+	while getopts ":cdDf:pr:sh" opt; do
 		case $opt in
 			c)
 				clearcache
@@ -212,6 +237,9 @@ main() {
 			d)
 				_optdownloadonly=1
 				;;
+			D)
+				_optdryrun=1
+				;;
 			f)
 				_optfile="$OPTARG"
 				;;