From d24f7a9c59b707c9c95a63fb587512063f782a9c Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 14 Feb 2022 17:11:53 +0100 Subject: [PATCH 1/7] Add --debug option & don't display request by default --- scripts/yt-api-helper.sh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 25ed099..8f1663b 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -17,6 +17,8 @@ print_help() echo " -i,--interactive Run in interactive mode" echo " -o,--output Print output to file instead of stdout" echo "" + echo " --debug Show what is sent to the API" + echo "" } print_clients() @@ -47,7 +49,7 @@ query_with_default() prompt="$1" default="$2" - printf "\n%s [%s]: " "$prompt" "$default" >&2 + printf "%s [%s]: " "$prompt" "$default" >&2 read -r data if [ -z "$data" ]; then @@ -62,7 +64,7 @@ query_with_error() prompt="$1" error_message="$2" - printf "\n%s []: " "$prompt" >&2 + printf "%s []: " "$prompt" >&2 read -r data if [ -z "$data" ]; then @@ -83,6 +85,7 @@ is_arg() -h|--help) true;; -i|--interactive) true;; -o|--output) true;; + --debug) true;; *) false;; esac } @@ -93,6 +96,7 @@ is_arg() # interactive=false +debug=false client_option="" endpoint_option="" @@ -162,6 +166,10 @@ while :; do output="$1" ;; + --debug) + debug=true + ;; + *) echo "Error: unknown argument '$1'" exit 2 @@ -392,8 +400,12 @@ if [ $interactive = true ]; then data="{\"context\":{\"client\":{$client}},$partial_data}" # Basic debug - echo "sending:" - echo "$data" | sed 's/{/{\n/g; s/}/\n}/g; s/,/,\n/g' + if [ $debug = true ]; then + echo + echo "sending:" + echo "$data" | sed 's/{/{\n/g; s/}/\n}/g; s/,/,\n/g' + echo + fi fi From 9c02b2d8124f28d2586a79ed2d1b0ac201e9ddb2 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 14 Feb 2022 17:12:57 +0100 Subject: [PATCH 2/7] Fix 'resolve' endpoint URL --- scripts/yt-api-helper.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 8f1663b..9f07240 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -341,7 +341,7 @@ case $endpoint_option in ;; resolve) - endpoint="navigation/resolve_url" + endpoint="/youtubei/v1/navigation/resolve_url" if [ $interactive = true ]; then url=$(query_with_error "Enter URL" "URL required") From a9c62a712155ebc1a75c4e9da83de6944c49baba Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 14 Feb 2022 17:15:01 +0100 Subject: [PATCH 3/7] Add iOS client --- scripts/yt-api-helper.sh | 41 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 9f07240..c02407b 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -29,6 +29,7 @@ print_clients() echo "web-mobile" echo "android" echo "android-embed" + echo "apple-ios" } print_endpoints() @@ -101,6 +102,11 @@ debug=false client_option="" endpoint_option="" +client_extra_device_make="" +client_extra_device_model="" +client_extra_os_name="" +client_extra_os_vers="" + data="" @@ -267,6 +273,18 @@ case $client_option in client_vers="16.20" ;; + apple-ios) + apikey="AIzaSyB-63vPrdThhKuerbB2N_l7Kwwcxj6yUAc" + client_name="IOS" + client_vers="16.46" + + client_extra_device_make="Apple" + client_extra_device_model="iPhone11,8" + client_extra_os_vers="15.2.0" + + user_agent="com.google.ios.youtube/16.46 (iPhone11,8; U; CPU iOS 15_2 like Mac OS X; en_GB)" + ;; + *) echo "Error: Unknown client '$client_option'" echo "" @@ -388,7 +406,21 @@ if [ $interactive = true ]; then hl=$(query_with_default "Enter content language (hl)" "en") gl=$(query_with_default "Enter content region (gl)" "US") - client="\"clientName\":\"${client_name}\",\"clientVersion\":\"${client_vers}\",\"hl\":\"${hl}\",\"gl\":\"${gl}\"" + client="\"hl\":\"${hl}\",\"gl\":\"${gl}\"" + + client="${client},\"deviceMake\":\"${client_extra_device_make}\"" + client="${client},\"deviceModel\":\"${client_extra_device_model}\"" + + 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 fi @@ -413,7 +445,12 @@ url="https://www.youtube.com/${endpoint}?key=${apikey}" # Headers hdr_ct='Content-Type: application/json; charset=utf-8' -hdr_ua='User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0' + +if [ -z "$user_agent" ]; then + user_agent="Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0" +fi + +hdr_ua="User-Agent: ${user_agent}" # Default to STDOUT if no output file was given if [ -z "$output" ]; then output='-'; fi From 653a1611c2bebe9b9348dc73e761fc96468dbc56 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 14 Feb 2022 17:25:45 +0100 Subject: [PATCH 4/7] Fix: -c/--client is required even with -d/--data (required to get the proper API key) --- scripts/yt-api-helper.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index c02407b..9a65279 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -5,14 +5,16 @@ print_help() { echo "Usage: yt-api-helper -i [-c ] [-e ]" - echo "Usage: yt-api-helper -e -d " + echo "Usage: yt-api-helper -c -e -d " echo "" echo "Options:" echo " -c,--client Client to use. Pass 'help' to this option to get" - echo " the list of supported clients" + echo " the list of supported clients. Mandatory in" + echo " non-interactive mode." echo " -d,--data Raw data to send to the API" echo " -e,--endpoint Youtube endpoint to request. Pass 'help' to this" - echo " option to get the list of supported endpoints" + echo " option to get the list of supported endpoints." + echo " Mandatory in non-interactive mode" echo " -h,--help Show this help" echo " -i,--interactive Run in interactive mode" echo " -o,--output Print output to file instead of stdout" @@ -197,9 +199,10 @@ if [ ! -z "$data" ]; then exit 2 fi - # Can't pass client in non-interactive mode (must be part of data) - if [ ! -z "$client_option" ]; then - echo "Error: -c/--client can't be used with -d/--data" + # In non-interactive mode, we still need to pass a client + # so the right API key is passed as a URL parameter + if [ -z "$client_option" ]; then + echo "Error: -c/--client is required to select an API key" exit 2 fi From 20e14ee43344029f862f4a635088387c087330a1 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Wed, 23 Mar 2022 23:33:55 +0100 Subject: [PATCH 5/7] Add gitignore (ignore JSON files) --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a6c57f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.json From ecfa789948a0a7e487023111801094f3334efa8b Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Wed, 23 Mar 2022 23:51:36 +0100 Subject: [PATCH 6/7] Mute shellcheck error SC2237 --- scripts/yt-api-helper.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 9a65279..dbf1299 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -1,5 +1,8 @@ #!/bin/sh -# shellcheck disable=SC2236 +# shellcheck disable=SC2236,SC2237 +# +# ^ Allow the use of `! -z` and `! [ -z]` as those are +# more intuitive than `-n` print_help() From a0fb0e3c07ec08097195a4d4567c4330045f70e2 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Wed, 23 Mar 2022 23:54:27 +0100 Subject: [PATCH 7/7] Add code to support 'platform' and 'clientFormFactor' query options --- scripts/yt-api-helper.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index dbf1299..3761cb4 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -111,6 +111,8 @@ client_extra_device_make="" client_extra_device_model="" client_extra_os_name="" client_extra_os_vers="" +client_extra_platform="" +client_extra_form_factor="" data="" @@ -427,6 +429,14 @@ if [ $interactive = true ]; then 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 fi