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

38
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,6 +151,7 @@ playlist() {
rm "$_tmpfile"* > /dev/null 2>&1 rm "$_tmpfile"* > /dev/null 2>&1
sanitize "$line" sanitize "$line"
filename="$_musicdir/$_return" filename="$_musicdir/$_return"
if [ -z "$_optdryrun" ]; then
if ! [ -f "$filename" ]; then if ! [ -f "$filename" ]; then
log "Finding a song for \"$line\"" log "Finding a song for \"$line\""
youtube-dl \ youtube-dl \
@ -165,7 +171,7 @@ playlist() {
notify "Could not download song" \ 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" \ "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 normal dialog-error 3000
log "Could not download song \"$line\"" error "Could not download song \"$line\""
continue continue
fi fi
else else
@ -176,6 +182,25 @@ playlist() {
audacious -p audacious -p
unset _optautoplay unset _optautoplay
fi fi
else
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) done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi)
if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; then if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; then
notify "Failed to enqueue playlist" \ notify "Failed to enqueue playlist" \
@ -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"
;; ;;