ptgdp: Add notification support, make logging more verbose

This commit is contained in:
Salt 2019-07-04 03:59:02 -05:00
parent 4ff300b376
commit 524a013cbb

60
ptgdp
View File

@ -21,18 +21,34 @@ _musiclink="${XDG_MUSIC_DIR:-$HOME/Music}/PTGDP Songs"
# Helper functions # Helper functions
log() { log() {
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1 # Message body
printf "%s: %s\\n" \ printf "%s: %s\\n" \
"$_name" \ "$_name" \
"$1" "$1"
} }
error() { error() {
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1 # Message body
[ -z "$2" ] && return 2 [ -z "$2" ] && return 2 # Exit code
log "$1" log "$1"
exit "${2:-1}" exit "${2:-1}"
} }
notify() {
[ -z "$_optrofi" ] && return 0
[ -z "$1" ] && return 1 # Title
[ -z "$2" ] && return 2 # Body
local urg="${3:-normal}" # Urgency
local icon="${4:-dialog-information}" # Icon
local timeout="${5:-3000}" # Timeout in milliseconds
notify-send \
-a "$_name" \
-i "$icon" \
-u "$urg" \
-t "$timeout" \
"$1" \
"$2" > /dev/null 2>&1
}
validatedeps() { validatedeps() {
# $@: Dependencies to validate
for prog in "$@"; do for prog in "$@"; do
if ! command -v "$prog" > /dev/null 2>&1; then if ! command -v "$prog" > /dev/null 2>&1; then
_return="$prog" _return="$prog"
@ -42,7 +58,7 @@ validatedeps() {
return 0 return 0
} }
sanitize() { sanitize() {
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1 # String to strip special chars from
_return="${1//[^ a-zA-Z0-9\[\]|()_-]/}" _return="${1//[^ a-zA-Z0-9\[\]|()_-]/}"
} }
@ -74,13 +90,16 @@ files are cached in your Music folder under "JPTGDP Songs" for offline use.
-s Shuffle the playlist -s Shuffle the playlist
-r <directory> Start up rofi, if installed, and present a listing of -r <directory> Start up rofi, if installed, and present a listing of
all .gdp files in the given directory all .gdp files in the given directory. If notify-send
is installed, this will also send notifications
pertaining to playlist status.
-h Print this help text -h Print this help text
Copyright (c) 2019 rehashedsalt@cock.li Copyright (c) 2019 rehashedsalt@cock.li
Licensed under the MIT license Licensed under the MIT license
EOF EOF
notify "Test" "shit"
} }
rofimenu() { rofimenu() {
validatedeps rofi || error "$_return is not currently installed" 1 validatedeps rofi || error "$_return is not currently installed" 1
@ -96,6 +115,9 @@ rofimenu() {
playlists+="$filebase" playlists+="$filebase"
done <<< "$files" done <<< "$files"
else else
notify "No playlists found" \
"No playlists could be found in directory \"$_optrofi\"." \
normal dialog-error 5000
error "No playlists found" 61 error "No playlists found" 61
fi fi
choice="$(rofi -dmenu -i -p "$_name" <<< "$playlists")" choice="$(rofi -dmenu -i -p "$_name" <<< "$playlists")"
@ -116,6 +138,9 @@ playlist() {
log "Music can be found at \"$_musicdir\"" log "Music can be found at \"$_musicdir\""
fi fi
fi fi
local -i dlexist=0
local -i dlsuccess=0
local -i dlfailure=0
while read line; do while read line; do
[ -z "$line" ] && continue [ -z "$line" ] && continue
rm "$_tmpfile"* > /dev/null 2>&1 rm "$_tmpfile"* > /dev/null 2>&1
@ -133,11 +158,18 @@ playlist() {
ytsearch:"$line" \ ytsearch:"$line" \
> /dev/null 2>&1 & > /dev/null 2>&1 &
if wait $!; then if wait $!; then
dlsuccess+=1
mv "$_tmpfile"* "$filename" mv "$_tmpfile"* "$filename"
else else
log "No results found for \"$line\"" 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 continue
fi fi
else
dlexist+=1
fi fi
[ -z "$_optdownloadonly" ] && audacious -e "$filename" [ -z "$_optdownloadonly" ] && audacious -e "$filename"
if [ -n "$_optautoplay" ]; then if [ -n "$_optautoplay" ]; then
@ -145,10 +177,20 @@ playlist() {
unset _optautoplay unset _optautoplay
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 [ -z "$_optdownloadonly" ]; then if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; then
log "Finished building queue" 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" \
normal dialog-error 10000
else else
log "Finished downloading" if [ -z "$_optdownloadonly" ]; then
notify "Finished building queue" \
"The playlist queue has been built in Audacious"
log "Finished building queue: $dlexist cached, $dlsuccess downloaded, $dlfailure failed"
else
notify "Finished precaching" \
"Your songs have been cached and are ready for offline playback"
log "Finished downloading: $dlexist cached, $dlsuccess downloaded, $dlfailure failed"
fi
fi fi
} }