ptgdp: Major refactor, clarify logging
This commit is contained in:
163
ptgdp
163
ptgdp
@@ -15,7 +15,8 @@ _tmpdir="${XDG_CACHE_HOME:-$HOME/.cache}/$_name"
|
|||||||
_tmpfile="$_tmpdir/tmpfile-$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 12)"
|
_tmpfile="$_tmpdir/tmpfile-$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 12)"
|
||||||
_xdguserdirs="${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
|
_xdguserdirs="${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
|
||||||
[ -f "$_xdguserdirs" ] && source "$_xdguserdirs"
|
[ -f "$_xdguserdirs" ] && source "$_xdguserdirs"
|
||||||
_musicdir="${XDG_MUSIC_DIR:-$HOME/Music}/${PTGDP_MUSIC_DIR:-PTGDP Songs}"
|
_musicdir="${XDG_MUSIC_DIR:-$HOME/Music}"
|
||||||
|
_ptgdpmusicdir="$_musicdir/${PTGDP_MUSIC_DIR:-PTGDP Songs}"
|
||||||
|
|
||||||
# Helper functions
|
# Helper functions
|
||||||
log() {
|
log() {
|
||||||
@@ -52,7 +53,7 @@ checkforsong() {
|
|||||||
[ -z "$1" ] && return 1
|
[ -z "$1" ] && return 1
|
||||||
# Very rudimentary implementation
|
# Very rudimentary implementation
|
||||||
# TODO: Make this a lot more thorough, maybe ensure MIME is good
|
# TODO: Make this a lot more thorough, maybe ensure MIME is good
|
||||||
for file in "$_musicdir/$1"*; do
|
for file in "$_ptgdpmusicdir/$1"*; do
|
||||||
if [ -f "$file" ]; then
|
if [ -f "$file" ]; then
|
||||||
_return="$file"
|
_return="$file"
|
||||||
return 0
|
return 0
|
||||||
@@ -60,6 +61,68 @@ checkforsong() {
|
|||||||
done
|
done
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
cachesong() {
|
||||||
|
# $1: A song query to download
|
||||||
|
# $_return: The relative file path to song from XDG_MUSIC_DIR
|
||||||
|
# If in a dry run, $_return is the title of the video, if extracted
|
||||||
|
# Note that this will return 100 if the file is cached, so do not assume only 0 is success
|
||||||
|
[ -z "$1" ] && return 1
|
||||||
|
# Clean up tmpfile
|
||||||
|
rm "$_tmpfile"* > /dev/null 2>&1
|
||||||
|
sanitize "$1"
|
||||||
|
filename="$_ptgdpmusicdir/$_return"
|
||||||
|
if [ -z "$_optdryrun" ]; then
|
||||||
|
if ! checkforsong "$_return"; then
|
||||||
|
# We need to download the song
|
||||||
|
youtube-dl \
|
||||||
|
--add-metadata \
|
||||||
|
--audio-format "best" \
|
||||||
|
--geo-bypass \
|
||||||
|
--playlist-items 1 \
|
||||||
|
-x \
|
||||||
|
-o "$_tmpfile.%(ext)s" \
|
||||||
|
ytsearch:"$1" \
|
||||||
|
> /dev/null 2>&1 &
|
||||||
|
if wait $!; then
|
||||||
|
for file in "$_tmpfile"*; do
|
||||||
|
[ -f "$file" ] && local extension="$file"
|
||||||
|
break
|
||||||
|
done
|
||||||
|
extension="${extension##*/}"
|
||||||
|
extension="${extension##*.}"
|
||||||
|
filename="$filename.$extension"
|
||||||
|
mv "$_tmpfile"* "$filename"
|
||||||
|
_return="$filename"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
#_return is already an existing cached file, so just die
|
||||||
|
return 100
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
output="$(
|
||||||
|
youtube-dl \
|
||||||
|
--get-title \
|
||||||
|
--geo-bypass \
|
||||||
|
--playlist-items 1 \
|
||||||
|
ytsearch:"$1" 2>&1
|
||||||
|
)"
|
||||||
|
exitcode="$?"
|
||||||
|
if [ $exitcode -gt 0 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
if ! [ "$output" = "${output#*WARNING}" ]; then
|
||||||
|
# Title could not be extracted
|
||||||
|
unset _return
|
||||||
|
else
|
||||||
|
# Title was found
|
||||||
|
_return="$output"
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
}
|
||||||
validatedeps() {
|
validatedeps() {
|
||||||
# $@: Dependencies to validate
|
# $@: Dependencies to validate
|
||||||
for prog in "$@"; do
|
for prog in "$@"; do
|
||||||
@@ -83,7 +146,7 @@ trapexit() {
|
|||||||
|
|
||||||
# Critical functions
|
# Critical functions
|
||||||
clearcache() {
|
clearcache() {
|
||||||
[ -n "$_musicdir" ] && rm "$_musicdir"/* > /dev/null 2>&1
|
[ -n "$_ptgdpmusicdir" ] && rm "$_ptgdpmusicdir"/* > /dev/null 2>&1
|
||||||
log "Cache has been emptied"
|
log "Cache has been emptied"
|
||||||
}
|
}
|
||||||
helptext() {
|
helptext() {
|
||||||
@@ -159,7 +222,7 @@ playlist() {
|
|||||||
normal dialog-error 3000
|
normal dialog-error 3000
|
||||||
error "Failed to communicate with MPD" 52
|
error "Failed to communicate with MPD" 52
|
||||||
fi
|
fi
|
||||||
local -a queue
|
[ -z "$_optdryrun" ] && local -a queue # An array of songs to later enqueue into mpd
|
||||||
local -i dlexist=0
|
local -i dlexist=0
|
||||||
local -i dlsuccess=0
|
local -i dlsuccess=0
|
||||||
local -i dlfailure=0
|
local -i dlfailure=0
|
||||||
@@ -170,78 +233,64 @@ playlist() {
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
rm "$_tmpfile"* > /dev/null 2>&1
|
rm "$_tmpfile"* > /dev/null 2>&1
|
||||||
sanitize "$line"
|
# Do the do
|
||||||
filename="$_musicdir/$_return"
|
cachesong "$line"
|
||||||
|
# What did we do?
|
||||||
|
local errorcode=$?
|
||||||
if [ -z "$_optdryrun" ]; then
|
if [ -z "$_optdryrun" ]; then
|
||||||
if ! checkforsong "$_return"; then
|
case $errorcode in
|
||||||
log "Finding a song for \"$line\""
|
0)
|
||||||
youtube-dl \
|
# Downloaded
|
||||||
--add-metadata \
|
log "Downloaded song \"$line\""
|
||||||
--audio-format "best" \
|
|
||||||
--geo-bypass \
|
|
||||||
--playlist-items 1 \
|
|
||||||
-x \
|
|
||||||
-o "$_tmpfile.%(ext)s" \
|
|
||||||
ytsearch:"$line" \
|
|
||||||
> /dev/null 2>&1 &
|
|
||||||
if wait $!; then
|
|
||||||
dlsuccess+=1
|
dlsuccess+=1
|
||||||
for file in "$_tmpfile"*; do
|
;;
|
||||||
[ -f "$file" ] && local extension="$file"
|
100)
|
||||||
break
|
# Cached
|
||||||
done
|
log "Using cached song \"$line\""
|
||||||
extension="${extension##*/}"
|
dlexist+=1
|
||||||
extension="${extension##*.}"
|
;;
|
||||||
filename="$filename.$extension"
|
*)
|
||||||
mv "$_tmpfile"* "$filename"
|
# Failed
|
||||||
queue+=("$filename")
|
|
||||||
else
|
|
||||||
dlfailure+=1
|
dlfailure+=1
|
||||||
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\"" \
|
||||||
normal dialog-error 3000
|
normal dialog-error 3000
|
||||||
error "Could not download song \"$line\""
|
error "Could not download song \"$line\""
|
||||||
continue
|
continue
|
||||||
fi
|
;;
|
||||||
else
|
esac
|
||||||
queue+=("$_return")
|
queue+=("$_return")
|
||||||
dlexist+=1
|
|
||||||
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\""
|
|
||||||
dlerror+=1
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
if ! [ "$output" = "${output#*WARNING}" ]; then
|
|
||||||
log "$line parsed, but title could not be extracted"
|
|
||||||
else
|
else
|
||||||
|
case $errorcode in
|
||||||
|
0)
|
||||||
|
# Success
|
||||||
|
if [ -n "$_return" ]; then
|
||||||
log "$line - \"$output\""
|
log "$line - \"$output\""
|
||||||
|
else
|
||||||
|
log "$line parsed, but title could not be extracted"
|
||||||
fi
|
fi
|
||||||
dlsuccess+=1
|
dlsuccess+=1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# Failure
|
||||||
|
error "Could not find song \"$line\""
|
||||||
|
dlerror+=1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
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" ] && [ -z "$_optdryrun" ]; then
|
if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ] && [ -z "$_optdryrun" ]; then
|
||||||
notify "Failed to enqueue playlist" \
|
notify "Failed to download playlist" \
|
||||||
"The playlist could not be enqueued. Ensure that youtube-dl is up to date, you have a valid internet connection, and your search queries pull up results" \
|
"The playlist in its entirety could not be downloaded. Ensure that youtube-dl is up to date, you have a valid internet connection, and your search queries pull up results" \
|
||||||
normal dialog-error 10000
|
normal dialog-error 10000
|
||||||
elif [ -z "$_optdryrun" ]; then
|
elif [ -z "$_optdryrun" ]; then
|
||||||
if [ -z "$_optdownloadonly" ]; then
|
if [ -z "$_optdownloadonly" ]; then
|
||||||
notify "Finished building queue" \
|
notify "Finished downloading playlist" \
|
||||||
"The playlist queue has been built in mpd"
|
"The playlist has been downloaded and will be added to mpd shortly"
|
||||||
log "Finished building queue: $dlexist cached, $dlsuccess downloaded, $dlfailure failed"
|
log "Finished downloading: $dlexist cached, $dlsuccess downloaded, $dlfailure failed"
|
||||||
mpc update --wait > /dev/null 2>&1
|
mpc update --wait > /dev/null 2>&1
|
||||||
local xdgmusicdir="${XDG_MUSIC_DIR:-$HOME/Music}"
|
|
||||||
for file in "${queue[@]}"; do
|
for file in "${queue[@]}"; do
|
||||||
file=${file##$xdgmusicdir/}
|
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
|
||||||
if [ -n "$_optautoplay" ]; then
|
if [ -n "$_optautoplay" ]; then
|
||||||
@@ -263,7 +312,7 @@ main() {
|
|||||||
# Boostrapping and setup
|
# Boostrapping and setup
|
||||||
validatedeps youtube-dl basename || error "Critical dependency $_return was not met" 1
|
validatedeps youtube-dl basename || error "Critical dependency $_return was not met" 1
|
||||||
mkdir -p "$_tmpdir"
|
mkdir -p "$_tmpdir"
|
||||||
mkdir -p "$_musicdir"
|
mkdir -p "$_ptgdpmusicdir"
|
||||||
trap trapexit EXIT
|
trap trapexit EXIT
|
||||||
|
|
||||||
# Actual program stuff
|
# Actual program stuff
|
||||||
|
Reference in New Issue
Block a user