ptgdp: Add some preprocessor shenanigans

This commit is contained in:
Salt 2019-09-13 22:00:34 -05:00
parent 06712e971b
commit d099c224b3

56
ptgdp
View File

@ -20,6 +20,10 @@ _ptgdpmusicdir="$_musicdir/${PTGDP_MUSIC_DIR:-PTGDP Songs}"
declare -a _queue declare -a _queue
declare -a _playlists declare -a _playlists
# Preprocessor-accessible vars
declare p_ytdlargs="--geo-bypass"
declare p_search="ytsearch:"
# Helper functions # Helper functions
log() { log() {
[ -z "$1" ] && return 1 # Message body [ -z "$1" ] && return 1 # Message body
@ -54,7 +58,29 @@ execpreprocessor() {
# Execute a preprocessor line # Execute a preprocessor line
# $1: The line, either stripped or not of the leading #: # $1: The line, either stripped or not of the leading #:
line="${1#\#:}" line="${1#\#:}"
log "pp: $line" read -r -a args <<< "$line"
case ${args[0]} in
ytdlargs)
local old="$p_ytdlargs"
p_ytdlargs="${args[@]:1}"
if ! [ "$p_ytdlargs" = "$old" ]; then
log "Setting youtube-dl arguments \"$p_ytdlargs\""
fi
;;
search)
local old="$p_search"
p_search="${args[@]:1}"
if [ "$p_search" = "${p_search%:}" ]; then
p_search="${p_search}:"
fi
if ! [ "$p_search" = "$old" ]; then
log "Setting default search method \"$p_search\""
fi
;;
*)
error "Preprocessor command \"${args[0]}\" not implemented"
;;
esac
} }
checkforsong() { checkforsong() {
# $1: A song name to validate # $1: A song name to validate
@ -87,9 +113,9 @@ cachesong() {
youtube-dl \ youtube-dl \
--add-metadata \ --add-metadata \
--audio-format "best" \ --audio-format "best" \
--default-search "ytsearch:" \ --default-search "$p_search" \
--geo-bypass \
--playlist-items 1 \ --playlist-items 1 \
"$p_ytdlargs" \
-x \ -x \
-o "$_tmpfile.%(ext)s" \ -o "$_tmpfile.%(ext)s" \
"$1" \ "$1" \
@ -274,10 +300,19 @@ given plaintext FILEs with only search queries.
be a file in this mode be a file in this mode
-h Print this help text -h Print this help text
Preprocessor Commands
Any line prefixed with #: is interpreted as a preprocessor command. Commands
are IFS-separated with everything after the first word interpreted as arguments.
search Set the default search prefix, with or without a
trailing colon.
ytdlargs A set of arguments to pass to youtube-dl
Environment Variables Environment Variables
In addition to XDG-spec variables (XDG_CACHE_HOME, user-dirs.dirs, etc.), ptgdp In addition to XDG-spec variables (XDG_CACHE_HOME, user-dirs.dirs, etc.),
also respects an additional variable: $_name also respects an additional variable:
PTGDP_MUSIC_DIR The subdirectory in XDG_MUSIC_DIR to save music to PTGDP_MUSIC_DIR The subdirectory in XDG_MUSIC_DIR to save music to
@ -365,6 +400,12 @@ playlist() {
maxlines+=1 maxlines+=1
done < "$1" done < "$1"
log "Parsed playlist \"$1\" with $maxlines songs" log "Parsed playlist \"$1\" with $maxlines songs"
# Store old preprocessor settings
declare -a oldsettings
while read var; do
oldsettings+=("$(declare -p "$var")")
done < <(compgen -v "p_")
# Parse out the playlist
while read line; do while read line; do
validateline "$line" validateline "$line"
case $? in case $? in
@ -394,6 +435,11 @@ playlist() {
;; ;;
esac esac
done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi) done < <(if [ -n "$_optshuffle" ]; then shuf "$1"; else cat "$1"; fi)
# Return to normality
for var in "${oldsettings[@]}"; do
eval "$var"
done
# Dump some stats
if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ] && [ -z "$_optdryrun" ]; then if [ "$dlexist" = "0" ] && [ "$dlsuccess" = "0" ] && [ -z "$_optdryrun" ]; then
notify "Failed to download playlist" \ 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" \ "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" \