From da3972c31554da1721b9e2c536993a09ab2f4b21 Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 26 Sep 2019 18:55:22 -0500 Subject: [PATCH] Add machine-processible mode so people can adapt to their own players --- README.md | 2 +- ptgdp | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d7e83c5..b3b19a1 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ On the bright side, though, youtube-dl downloads most songs in Opus, which has i **Q**: Why isn't my music player supported? -**A**: Open a bug report/pull request. +**A**: Open a bug report/pull request. Alternatively, use `ptgdp -m` and some glue. ## Bugs diff --git a/ptgdp b/ptgdp index 285883d..84e4d4a 100755 --- a/ptgdp +++ b/ptgdp @@ -27,6 +27,7 @@ declare _optconfigfile="${XDG_CONFIG_HOME:-$HOME/.config}/${_name}.conf" declare -i _optautoplay=0 declare -i _optdlonly declare -i _opthelp +declare -i _optmachinemode declare -i _optverbose # Working variables declare -a _queue @@ -41,15 +42,23 @@ log() { # loglevel 1: Detailed but not quite debugging # loglevel 2: Definitely debugging [ -z "$1" ] && return 1 + local -i outstream=1 + [ -n "$_optmachinemode" ] && outstream=2 if (( _optverbose >= ${2:-0} )); then - printf "%s\\n" "$1" + printf "%s\\n" "$1" >&"$outstream" fi } warn() { # Print a yellow line to the terminal, respecting _optverbose [ -z "$1" ] && return 1 + local -i outstream=1 + [ -n "$_optmachinemode" ] && outstream=2 if (( _optverbose >= ${2:-0} )); then - printf "\\e[33m%s\\e[0m\\n" "$1" + if [ -t "$outstream" ]; then + printf "\\e[33m%s\\e[0m\\n" "$1" >&"$outstream" + else + printf "WARN: %s\\n" "$1" >&"$outstream" + fi fi } error() { @@ -247,6 +256,9 @@ given plaintext files with only search queries -c [FILE] Load the given file in place of the usual config file -d Download songs but don't queue them up -h Print this help text + -m Machine-processible mode. Output all log text to STDERR + and print only the final list of songs to STDOUT, + separated by newlines. -p Play the queue after it's built -v Print more status messages. Stacks @@ -285,14 +297,20 @@ playlist() { continue fi done < "$1" - backend-execqueue + if [ -n "$_optmachinemode" ]; then + for file in "${_queue[@]}"; do + printf "$file\\n" + done + else + backend-execqueue + fi log "Finished: $dlcache cached, $dlsuccess downloaded, $dlerr failed" } # Main main() { # Getopts before anything else - while getopts ":c:dhpv" opt; do + while getopts ":c:dhmpv" opt; do case $opt in c) _optconfigfile="$OPTARG" @@ -303,6 +321,9 @@ main() { h) _opthelp=1 ;; + m) + _optmachinemode=1 + ;; p) _optautoplay=1 ;;