Add machine-processible mode so people can adapt to their own players

This commit is contained in:
Salt 2019-09-26 18:55:22 -05:00
parent f9d199704f
commit da3972c315
2 changed files with 26 additions and 5 deletions

View File

@ -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? **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 ## Bugs

29
ptgdp
View File

@ -27,6 +27,7 @@ declare _optconfigfile="${XDG_CONFIG_HOME:-$HOME/.config}/${_name}.conf"
declare -i _optautoplay=0 declare -i _optautoplay=0
declare -i _optdlonly declare -i _optdlonly
declare -i _opthelp declare -i _opthelp
declare -i _optmachinemode
declare -i _optverbose declare -i _optverbose
# Working variables # Working variables
declare -a _queue declare -a _queue
@ -41,15 +42,23 @@ log() {
# loglevel 1: Detailed but not quite debugging # loglevel 1: Detailed but not quite debugging
# loglevel 2: Definitely debugging # loglevel 2: Definitely debugging
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1
local -i outstream=1
[ -n "$_optmachinemode" ] && outstream=2
if (( _optverbose >= ${2:-0} )); then if (( _optverbose >= ${2:-0} )); then
printf "%s\\n" "$1" printf "%s\\n" "$1" >&"$outstream"
fi fi
} }
warn() { warn() {
# Print a yellow line to the terminal, respecting _optverbose # Print a yellow line to the terminal, respecting _optverbose
[ -z "$1" ] && return 1 [ -z "$1" ] && return 1
local -i outstream=1
[ -n "$_optmachinemode" ] && outstream=2
if (( _optverbose >= ${2:-0} )); then 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 fi
} }
error() { 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 -c [FILE] Load the given file in place of the usual config file
-d Download songs but don't queue them up -d Download songs but don't queue them up
-h Print this help text -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 -p Play the queue after it's built
-v Print more status messages. Stacks -v Print more status messages. Stacks
@ -285,14 +297,20 @@ playlist() {
continue continue
fi fi
done < "$1" 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" log "Finished: $dlcache cached, $dlsuccess downloaded, $dlerr failed"
} }
# Main # Main
main() { main() {
# Getopts before anything else # Getopts before anything else
while getopts ":c:dhpv" opt; do while getopts ":c:dhmpv" opt; do
case $opt in case $opt in
c) c)
_optconfigfile="$OPTARG" _optconfigfile="$OPTARG"
@ -303,6 +321,9 @@ main() {
h) h)
_opthelp=1 _opthelp=1
;; ;;
m)
_optmachinemode=1
;;
p) p)
_optautoplay=1 _optautoplay=1
;; ;;