ptgdp: Major refactor, clarify logging
This commit is contained in:
parent
15d25b5ddc
commit
0ee928cae9
171
ptgdp
171
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)"
|
||||
_xdguserdirs="${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs"
|
||||
[ -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
|
||||
log() {
|
||||
@ -52,7 +53,7 @@ checkforsong() {
|
||||
[ -z "$1" ] && return 1
|
||||
# Very rudimentary implementation
|
||||
# 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
|
||||
_return="$file"
|
||||
return 0
|
||||
@ -60,6 +61,68 @@ checkforsong() {
|
||||
done
|
||||
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() {
|
||||
# $@: Dependencies to validate
|
||||
for prog in "$@"; do
|
||||
@ -83,7 +146,7 @@ trapexit() {
|
||||
|
||||
# Critical functions
|
||||
clearcache() {
|
||||
[ -n "$_musicdir" ] && rm "$_musicdir"/* > /dev/null 2>&1
|
||||
[ -n "$_ptgdpmusicdir" ] && rm "$_ptgdpmusicdir"/* > /dev/null 2>&1
|
||||
log "Cache has been emptied"
|
||||
}
|
||||
helptext() {
|
||||
@ -159,7 +222,7 @@ playlist() {
|
||||
normal dialog-error 3000
|
||||
error "Failed to communicate with MPD" 52
|
||||
fi
|
||||
local -a queue
|
||||
[ -z "$_optdryrun" ] && local -a queue # An array of songs to later enqueue into mpd
|
||||
local -i dlexist=0
|
||||
local -i dlsuccess=0
|
||||
local -i dlfailure=0
|
||||
@ -170,78 +233,64 @@ playlist() {
|
||||
continue
|
||||
fi
|
||||
rm "$_tmpfile"* > /dev/null 2>&1
|
||||
sanitize "$line"
|
||||
filename="$_musicdir/$_return"
|
||||
# Do the do
|
||||
cachesong "$line"
|
||||
# What did we do?
|
||||
local errorcode=$?
|
||||
if [ -z "$_optdryrun" ]; then
|
||||
if ! checkforsong "$_return"; 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
|
||||
case $errorcode in
|
||||
0)
|
||||
# Downloaded
|
||||
log "Downloaded song \"$line\""
|
||||
dlsuccess+=1
|
||||
for file in "$_tmpfile"*; do
|
||||
[ -f "$file" ] && local extension="$file"
|
||||
break
|
||||
done
|
||||
extension="${extension##*/}"
|
||||
extension="${extension##*.}"
|
||||
filename="$filename.$extension"
|
||||
mv "$_tmpfile"* "$filename"
|
||||
queue+=("$filename")
|
||||
else
|
||||
;;
|
||||
100)
|
||||
# Cached
|
||||
log "Using cached song \"$line\""
|
||||
dlexist+=1
|
||||
;;
|
||||
*)
|
||||
# Failed
|
||||
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" \
|
||||
"youtube-dl did not download a song for \"$line\"" \
|
||||
normal dialog-error 3000
|
||||
error "Could not download song \"$line\""
|
||||
continue
|
||||
fi
|
||||
else
|
||||
queue+=("$_return")
|
||||
dlexist+=1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
queue+=("$_return")
|
||||
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
|
||||
log "$line - \"$output\""
|
||||
fi
|
||||
dlsuccess+=1
|
||||
case $errorcode in
|
||||
0)
|
||||
# Success
|
||||
if [ -n "$_return" ]; then
|
||||
log "$line - \"$output\""
|
||||
else
|
||||
log "$line parsed, but title could not be extracted"
|
||||
fi
|
||||
dlsuccess+=1
|
||||
;;
|
||||
*)
|
||||
# Failure
|
||||
error "Could not find song \"$line\""
|
||||
dlerror+=1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi)
|
||||
if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ] && [ -z "$_optdryrun" ]; then
|
||||
notify "Failed to enqueue 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" \
|
||||
notify "Failed to download playlist" \
|
||||
"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
|
||||
elif [ -z "$_optdryrun" ]; then
|
||||
if [ -z "$_optdownloadonly" ]; then
|
||||
notify "Finished building queue" \
|
||||
"The playlist queue has been built in mpd"
|
||||
log "Finished building queue: $dlexist cached, $dlsuccess downloaded, $dlfailure failed"
|
||||
notify "Finished downloading playlist" \
|
||||
"The playlist has been downloaded and will be added to mpd shortly"
|
||||
log "Finished downloading: $dlexist cached, $dlsuccess downloaded, $dlfailure failed"
|
||||
mpc update --wait > /dev/null 2>&1
|
||||
local xdgmusicdir="${XDG_MUSIC_DIR:-$HOME/Music}"
|
||||
for file in "${queue[@]}"; do
|
||||
file=${file##$xdgmusicdir/}
|
||||
file=${file##$_musicdir/}
|
||||
mpc add "$file" > /dev/null 2>&1 || error "Could not add file: \"$file\""
|
||||
done
|
||||
if [ -n "$_optautoplay" ]; then
|
||||
@ -263,7 +312,7 @@ main() {
|
||||
# Boostrapping and setup
|
||||
validatedeps youtube-dl basename || error "Critical dependency $_return was not met" 1
|
||||
mkdir -p "$_tmpdir"
|
||||
mkdir -p "$_musicdir"
|
||||
mkdir -p "$_ptgdpmusicdir"
|
||||
trap trapexit EXIT
|
||||
|
||||
# Actual program stuff
|
||||
|
Loading…
Reference in New Issue
Block a user