From 524a013cbb3bf5f290d723215301794499fed0c0 Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 4 Jul 2019 03:59:02 -0500 Subject: [PATCH] ptgdp: Add notification support, make logging more verbose --- ptgdp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/ptgdp b/ptgdp index 5f03f5a..54cad4d 100755 --- a/ptgdp +++ b/ptgdp @@ -21,18 +21,34 @@ _musiclink="${XDG_MUSIC_DIR:-$HOME/Music}/PTGDP Songs" # Helper functions log() { - [ -z "$1" ] && return 1 + [ -z "$1" ] && return 1 # Message body printf "%s: %s\\n" \ "$_name" \ "$1" } error() { - [ -z "$1" ] && return 1 - [ -z "$2" ] && return 2 + [ -z "$1" ] && return 1 # Message body + [ -z "$2" ] && return 2 # Exit code log "$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() { + # $@: Dependencies to validate for prog in "$@"; do if ! command -v "$prog" > /dev/null 2>&1; then _return="$prog" @@ -42,7 +58,7 @@ validatedeps() { return 0 } sanitize() { - [ -z "$1" ] && return 1 + [ -z "$1" ] && return 1 # String to strip special chars from _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 -r 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 Copyright (c) 2019 rehashedsalt@cock.li Licensed under the MIT license EOF + notify "Test" "shit" } rofimenu() { validatedeps rofi || error "$_return is not currently installed" 1 @@ -96,6 +115,9 @@ rofimenu() { playlists+="$filebase" done <<< "$files" else + notify "No playlists found" \ + "No playlists could be found in directory \"$_optrofi\"." \ + normal dialog-error 5000 error "No playlists found" 61 fi choice="$(rofi -dmenu -i -p "$_name" <<< "$playlists")" @@ -116,6 +138,9 @@ playlist() { log "Music can be found at \"$_musicdir\"" fi fi + local -i dlexist=0 + local -i dlsuccess=0 + local -i dlfailure=0 while read line; do [ -z "$line" ] && continue rm "$_tmpfile"* > /dev/null 2>&1 @@ -133,11 +158,18 @@ playlist() { ytsearch:"$line" \ > /dev/null 2>&1 & if wait $!; then + dlsuccess+=1 mv "$_tmpfile"* "$filename" 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 fi + else + dlexist+=1 fi [ -z "$_optdownloadonly" ] && audacious -e "$filename" if [ -n "$_optautoplay" ]; then @@ -145,10 +177,20 @@ playlist() { unset _optautoplay fi done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi) - if [ -z "$_optdownloadonly" ]; then - log "Finished building queue" + if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ]; 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" \ + normal dialog-error 10000 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 }