Use parametric functions for interactive data querying

This commit is contained in:
Samantaz Fox 2021-10-26 00:55:03 +02:00
parent 0a9b21ce5f
commit c47e949a81

View file

@ -40,6 +40,38 @@ print_endpoints()
} }
query_with_default()
{
prompt="$1"
default="$2"
printf "\n%s [%s]: " "$prompt" "$default" >&2
read data
if [ -z "$data" ]; then
echo "$default"
else
echo "$data"
fi
}
query_with_error()
{
prompt="$1"
error_message="$2"
printf "\n%s []: " "$prompt" >&2
read data
if [ -z "$data" ]; then
echo "Error: $error_message"
return 1
else
echo "$data"
fi
}
is_arg() is_arg()
{ {
case $1 in case $1 in
@ -186,10 +218,7 @@ fi
# #
if [ -z $client_option ]; then if [ -z $client_option ]; then
printf "Enter a client to use [web]: " client_option=$(query_with_default "Enter a client to use" "web")
read client_option
if [ -z $client_option ]; then client_option="web"; fi
fi fi
case $client_option in case $client_option in
@ -256,12 +285,8 @@ case $endpoint_option in
endpoint="youtubei/v1/browse" endpoint="youtubei/v1/browse"
if [ $interactive = true ]; then if [ $interactive = true ]; then
printf "Enter browse ID [UCXuqSBlHAE6Xw-yeJA0Tunw]: " browse_id=$(query_with_default "Enter browse ID" "UCXuqSBlHAE6Xw-yeJA0Tunw")
read browse_id
if [ -z $browse_id ]; then browse_id="UCXuqSBlHAE6Xw-yeJA0Tunw"; fi
partial_data="\"browseId\":\"${browse_id}\"" partial_data="\"browseId\":\"${browse_id}\""
fi fi
;; ;;
@ -269,10 +294,7 @@ case $endpoint_option in
endpoint="youtubei/v1/browse" endpoint="youtubei/v1/browse"
if [ $interactive = true ]; then if [ $interactive = true ]; then
printf "Enter continuation token []: " token=$(query_with_error "Enter continuation token" "token required")
read token
if [ -z $token ]; then echo "Error: token required"; return 1; fi
partial_data="\"continuation\":\"${token}\"" partial_data="\"continuation\":\"${token}\""
fi fi
;; ;;
@ -281,11 +303,9 @@ case $endpoint_option in
endpoint="youtubei/v1/$endpoint_option" endpoint="youtubei/v1/$endpoint_option"
if [ $interactive = true ]; then if [ $interactive = true ]; then
printf "Enter video ID [dQw4w9WgXcQ]: " vid=$(query_with_default "Enter video ID" "dQw4w9WgXcQ")
read vid
if [ -z $vid ]; then vid="dQw4w9WgXcQ"; fi
partial_data="\"videoId\":\"${vid}\"" partial_data="\"videoId\":\"${vid}\""
fi fi
;; ;;
@ -293,10 +313,7 @@ case $endpoint_option in
endpoint="youtubei/v1/next" endpoint="youtubei/v1/next"
if [ $interactive = true ]; then if [ $interactive = true ]; then
printf "Enter continuation token []: " token=$(query_with_error "Enter continuation token" "token required")
read token
if [ -z $token ]; then echo "Error: token required"; return 1; fi
partial_data="\"continuation\":\"${token}\"" partial_data="\"continuation\":\"${token}\""
fi fi
;; ;;
@ -305,10 +322,7 @@ case $endpoint_option in
endpoint="navigation/resolve_url" endpoint="navigation/resolve_url"
if [ $interactive = true ]; then if [ $interactive = true ]; then
printf "Enter URL []: " url=$(query_with_error "Enter URL" "URL required")
read url
if [ -z $url ]; then echo "Error: URL required"; return 1; fi
partial_data="\"url\":\"${url}\"" partial_data="\"url\":\"${url}\""
fi fi
;; ;;
@ -349,14 +363,8 @@ echo
# #
if [ $interactive = true ]; then if [ $interactive = true ]; then
printf "Enter content language (hl) [en]: " hl=$(query_with_default "Enter content language (hl)" "en")
read hl gl=$(query_with_default "Enter content region (gl)" "US")
printf "Enter content region (gl) [US]: "
read gl
if [ -z $hl ]; then hl="en"; fi
if [ -z $gl ]; then gl="US"; fi
client="\"clientName\":\"${client_name}\",\"clientVersion\":\"${client_vers}\",\"hl\":\"${hl}\",\"gl\":\"${gl}\"" client="\"clientName\":\"${client_name}\",\"clientVersion\":\"${client_vers}\",\"hl\":\"${hl}\",\"gl\":\"${gl}\""
fi fi