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 _playlists
# Preprocessor-accessible vars
declare p_ytdlargs="--geo-bypass"
declare p_search="ytsearch:"
# Helper functions
log() {
[ -z "$1" ] && return 1 # Message body
@ -54,7 +58,29 @@ execpreprocessor() {
# Execute a preprocessor line
# $1: The line, either stripped or not of the leading #:
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() {
# $1: A song name to validate
@ -87,9 +113,9 @@ cachesong() {
youtube-dl \
--add-metadata \
--audio-format "best" \
--default-search "ytsearch:" \
--geo-bypass \
--default-search "$p_search" \
--playlist-items 1 \
"$p_ytdlargs" \
-x \
-o "$_tmpfile.%(ext)s" \
"$1" \
@ -274,10 +300,19 @@ given plaintext FILEs with only search queries.
be a file in this mode
-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
In addition to XDG-spec variables (XDG_CACHE_HOME, user-dirs.dirs, etc.), ptgdp
also respects an additional variable:
In addition to XDG-spec variables (XDG_CACHE_HOME, user-dirs.dirs, etc.),
$_name also respects an additional variable:
PTGDP_MUSIC_DIR The subdirectory in XDG_MUSIC_DIR to save music to
@ -365,6 +400,12 @@ playlist() {
maxlines+=1
done < "$1"
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
validateline "$line"
case $? in
@ -394,6 +435,11 @@ playlist() {
;;
esac
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
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" \