From d45d85391e355cd78b159e7af8e08bc6c33cbc4d Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sat, 30 Mar 2024 22:51:18 +0100 Subject: [PATCH] api-helper: Move client data assembly logic to a separate function --- scripts/yt-api-helper.sh | 118 ++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 56 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 52d830d..d94e39c 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -309,6 +309,57 @@ endpoint_select() } +# +# Function to craft the client data +# + +make_request_data() +{ + client="\"hl\":\"${hl}\",\"gl\":\"${gl}\"" + + client="${client},\"deviceMake\":\"${client_extra_device_make}\"" + client="${client},\"deviceModel\":\"${client_extra_device_model}\"" + + if ! [ -z "$screen" ]; then + client="${client},\"clientScreen\":\"${screen}\"" + fi + + client="${client},\"clientName\":\"${client_name}\"" + client="${client},\"clientVersion\":\"${client_vers}\"" + + if ! [ -z "$client_extra_os_name" ]; then + client="${client},\"osName\":\"${client_extra_os_name}\"" + fi + + if ! [ -z "$client_extra_os_vers" ]; then + client="${client},\"osVersion\":\"${client_extra_os_vers}\"" + fi + + if ! [ -z "$client_extra_platform" ]; then + client="${client},\"platform\":\"${client_extra_platform}\"" + fi + + if ! [ -z "$client_extra_form_factor" ]; then + client="${client},\"clientFormFactor\":\"${client_extra_form_factor}\"" + fi + + + data="{\"context\":{\"client\":{$client}},$endpoint_data}" + + # Basic debug + if [ $debug = true ]; then + if command -v jq >&2 >/dev/null; then + printf "\nSending: %s\n\n" "$data" | jq . >&2 + else + printf "\nSending: %s\n\n" "$data" | sed 's/{/{\n/g; s/}/\n}/g; s/,/,\n/g' >&2 + fi + fi + + # Return data + printf "{\"context\":{\"client\":{%s}},%s}\n" "$client" "$endpoint_data" +} + + # # Parameters init # @@ -323,7 +374,7 @@ data="" # -# Interactive client selection +# Parse arguments # while :; do @@ -449,7 +500,7 @@ fi # -# Client selection +# Required parameters. Ask if not provided # if [ -z "$client_option" ]; then @@ -461,13 +512,6 @@ if [ -z "$client_option" ]; then fi fi -client_select "$client_option" - - -# -# Endpoint selection -# - if [ -z "$endpoint_option" ]; then if [ $interactive = true ]; then print_endpoints @@ -480,63 +524,25 @@ fi # new line echo >&2 -endpoint_select "$endpoint_option" - - -# # Interactive language/region selection -# - if [ $interactive = true ]; then hl=$(query_with_default "Enter content language (hl)" "en") gl=$(query_with_default "Enter content region (gl)" "US") - - client="\"hl\":\"${hl}\",\"gl\":\"${gl}\"" - - client="${client},\"deviceMake\":\"${client_extra_device_make}\"" - client="${client},\"deviceModel\":\"${client_extra_device_model}\"" - - if ! [ -z "$screen" ]; then - client="${client},\"clientScreen\":\"${screen}\"" - fi - - client="${client},\"clientName\":\"${client_name}\"" - client="${client},\"clientVersion\":\"${client_vers}\"" - - if ! [ -z "$client_extra_os_name" ]; then - client="${client},\"osName\":\"${client_extra_os_name}\"" - fi - - if ! [ -z "$client_extra_os_vers" ]; then - client="${client},\"osVersion\":\"${client_extra_os_vers}\"" - fi - - if ! [ -z "$client_extra_platform" ]; then - client="${client},\"platform\":\"${client_extra_platform}\"" - fi - - if ! [ -z "$client_extra_form_factor" ]; then - client="${client},\"clientFormFactor\":\"${client_extra_form_factor}\"" - fi - - - data="{\"context\":{\"client\":{$client}},$endpoint_data}" - - # Basic debug - if [ $debug = true ]; then - if command -v jq >&2 >/dev/null; then - printf "\nSending: %s\n\n" "$data" | jq . >&2 - else - printf "\nSending: %s\n\n" "$data" | sed 's/{/{\n/g; s/}/\n}/g; s/,/,\n/g' >&2 - fi - fi fi # -# Final command +# Run the request # +client_select "$client_option" +endpoint_select "$endpoint_option" + +if [ $interactive = true ]; then + data=$(make_request_data) +fi + + url="https://www.youtube.com/${endpoint}?key=${apikey}" # Headers