From b51609a2dfda88eb2b236607c927d45400a64754 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 25 Oct 2021 20:02:13 +0200 Subject: [PATCH 01/12] Add a basic YT API utility script (made in bash) This utility script is intended for research on the innertube API so the developper can query the API without having to compile invidious nor need to copy-paste the same commands over and over. --- scripts/yt-api-helper.sh | 337 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100755 scripts/yt-api-helper.sh diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh new file mode 100755 index 0000000..38d6157 --- /dev/null +++ b/scripts/yt-api-helper.sh @@ -0,0 +1,337 @@ +#!/bin/zsh + + +print_help() +{ + echo "Usage: yt-api-helper -i [-c ] [-e ]" + echo "Usage: yt-api-helper -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 " -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 " -h,--help Show this help" + echo " -i,--interactive Run in interactive mode" + echo " -o,--output Print output to file instead of stdout" + echo "" +} + +print_clients() +{ + echo "Available clients:" + echo "web" +} + +print_endpoints() +{ + echo "Available endpoints:" + echo "browse" + echo "browse-continuation" + echo "next" + echo "next-continuation" + echo "player" + echo "resolve" +} + + +is_arg() +{ + case $1 in + -c|--client) true;; + -d|--data) true;; + -e|--endpoint) true;; + -h|--help) true;; + -i|--interactive) true;; + -o|--output) true;; + *) false;; + esac +} + + +# +# Parameters init +# + +interactive=false + +client_option="" +endpoint_option="" + +data="" + + +# +# Interactive client selection +# + +while :; do + # Exit if no more arguments to parse + if [ $# -eq 0 ]; then break; fi + + case $1 in + -c|--client) + shift + + if [ $# -eq 0 ] || $(is_arg "$1"); then + echo "Error: missing argument after -c/--client" + return 2 + fi + + client_option=$1 + ;; + + -d|--data) + shift + + if [ $# -eq 0 ] || $(is_arg "$1"); then + echo "Error: missing argument after -d/--data" + return 2 + fi + + data=$1 + ;; + + -e|--endpoint) + shift + + if [ $# -eq 0 ] || $(is_arg "$1"); then + echo "Error: missing argument after -e/--endpoint" + return 2 + fi + + endpoint_option=$1 + ;; + + -h|--help) + print_help + return 0 + ;; + + -i|--interactive) + interactive=true + ;; + + -o|--output) + shift + + if [ $# -eq 0 ] || $(is_arg "$1"); then + echo "Error: missing argument after -o/--output" + return 2 + fi + + output="$1" + ;; + + *) + echo "Error: unknown argument '$1'" + return 2 + ;; + esac + + shift +done + + +# +# Input validation +# + +if [ ! -z "$data" ]; then + # Can't pass data in interactive mode + if [ $interactive = true ]; then + echo "Error: -d/--data can't be used with -i/--interactive" + return 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" + return 2 + fi + + # Endpoint must be given if non-interactive mode + if [ -z $endpoint_option ]; then + echo "Error: In non-interactive mode, an endpoint must be passed with -e/--endpoint" + return 2 + fi +fi + +if [ -z "$data" ] && [ $interactive = false ]; then + # Data must be given if non-interactive mode + echo "Error: In non-interactive mode, data must be passed with -d/--data" + return 2 +fi + +if [ -z "$output" ] && [ $interactive = true ]; then + printf "\nIt is recommended to use --output in interactive mode.\nContinue? [y/N]: " + read confirm + + if [ -z $confirm ]; then confirm="n"; fi + + case $confirm in + [Yy]|[Yy][Ee][Ss]) ;; + *) return 0;; + esac +fi + + +# +# Client selection +# + +if [ -z $client_option ]; then + printf "Enter a client to use [web]: " + read client_option + + if [ -z $client_option ]; then client_option="web"; fi +fi + +case $client_option in + help) + print_clients + return 0 + ;; + + web) + apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" + client_name="WEB" + client_vers="2.20210330.08.00" + ;; + + *) + echo "Error: Unknown client '$client_option'" + echo "" + print_clients + return 1 + ;; +esac + + +# +# Endpoint selection +# + +if [ -z $endpoint_option ]; then + printf "Enter an endpoint to request []: " + read endpoint_option +fi + +case $endpoint_option in + help) + print_endpoints + return 0 + ;; + + browse) + endpoint="youtubei/v1/browse" + + if [ $interactive = true ]; then + printf "Enter browse ID [UCXuqSBlHAE6Xw-yeJA0Tunw]: " + read browse_id + + if [ -z $browse_id ]; then browse_id="UCXuqSBlHAE6Xw-yeJA0Tunw"; fi + partial_data="\"browseId\":\"${browse_id}\"" + fi + ;; + + browse-cont*|browse-tok*) + endpoint="youtubei/v1/browse" + + if [ $interactive = true ]; then + printf "Enter continuation token []: " + read token + + if [ -z $toekn ]; then echo "Error: token required"; return 1; fi + partial_data="\"continuation\":\"${token}\"" + fi + ;; + + player|next) + endpoint="youtubei/v1/$endpoint_option" + + if [ $interactive = true ]; then + printf "Enter video ID [dQw4w9WgXcQ]: " + read vid + + if [ -z $vid ]; then vid="dQw4w9WgXcQ"; fi + partial_data="\"videoId\":\"${vid}\"" + fi + ;; + + next-cont*|next-tok*) + endpoint="youtubei/v1/next" + + if [ $interactive = true ]; then + printf "Enter continuation token []: " + read token + + if [ -z $toekn ]; then echo "Error: token required"; return 1; fi + partial_data="\"continuation\":\"${token}\"" + fi + ;; + + resolve) + endpoint="navigation/resolve_url" + + if [ $interactive = true ]; then + printf "Enter URL []: " + read url + + if [ -z $url ]; then echo "Error: URL required"; return 1; fi + partial_data="\"url\":\"${url}\"" + fi + ;; + + *) + echo "Error: Unknown endpoint '$endpoint_option'" + echo "" + print_clients + return 1 + ;; +esac + + +# +# Interactive language/region selection +# + +if [ $interactive = true ]; then + printf "Enter content language (hl) [en]: " + read hl + + 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}\"" +fi + + +# +# Final command +# + +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' +fi + + +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' + +# Default to STDOUT if no output file was given +if [ -z "output" ]; then output='-'; fi + +# Run! +curl --compressed -o "$output" -H "$hdr_ct" -H "$hdr_ua" --data "$data" "$url" From 2e11ec2e4aba9b3605a4870172639448157f7b37 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 25 Oct 2021 20:10:15 +0200 Subject: [PATCH 02/12] Add more clients to YT API utility script --- scripts/yt-api-helper.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 38d6157..c193c63 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -22,6 +22,10 @@ print_clients() { echo "Available clients:" echo "web" + echo "web-embed" + echo "web-mobile" + echo "android" + echo "android-embed" } print_endpoints() @@ -197,7 +201,31 @@ case $client_option in web) apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" client_name="WEB" - client_vers="2.20210330.08.00" + client_vers="2.20210721.00.00" + ;; + + web-embed) + apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" + client_name="WEB_EMBEDDED_PLAYER" + client_vers="1.20210721.1.0" + ;; + + web-mobile) + apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" + client_name="MWEB" + client_vers="2.20210726.08.00" + ;; + + android) + apikey= "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w" + client_name="ANDROID" + client_vers="16.20" + ;; + + android-embed) + apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" + client_name="ANDROID_EMBEDDED_PLAYER" + client_vers="16.20" ;; *) From fce94103281ea419e906801a1e403ecc8ac3919b Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 25 Oct 2021 20:19:07 +0200 Subject: [PATCH 03/12] Fix typo in variable name: s/toekn/token/ --- scripts/yt-api-helper.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index c193c63..253cb66 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -271,7 +271,7 @@ case $endpoint_option in printf "Enter continuation token []: " read token - if [ -z $toekn ]; then echo "Error: token required"; return 1; fi + if [ -z $token ]; then echo "Error: token required"; return 1; fi partial_data="\"continuation\":\"${token}\"" fi ;; @@ -295,7 +295,7 @@ case $endpoint_option in printf "Enter continuation token []: " read token - if [ -z $toekn ]; then echo "Error: token required"; return 1; fi + if [ -z $token ]; then echo "Error: token required"; return 1; fi partial_data="\"continuation\":\"${token}\"" fi ;; From c7eb46c7bb89fbea86fb9fc763e65e02e18e8ec9 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Mon, 25 Oct 2021 20:22:49 +0200 Subject: [PATCH 04/12] Request optional base64 parameters for browse/player --- scripts/yt-api-helper.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 253cb66..479e6a2 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -261,6 +261,13 @@ case $endpoint_option in if [ -z $browse_id ]; then browse_id="UCXuqSBlHAE6Xw-yeJA0Tunw"; fi partial_data="\"browseId\":\"${browse_id}\"" + + printf "Enter optional parameters (base64-encoded protobuf) []: " + read params + + if [ ! -z $params ]; then + partial_data="${partial_data},\"params\":\"${params}\"" + fi fi ;; @@ -285,6 +292,16 @@ case $endpoint_option in if [ -z $vid ]; then vid="dQw4w9WgXcQ"; fi partial_data="\"videoId\":\"${vid}\"" + + + if [ "$endpoint_option" = "player" ]; then + printf "Enter optional parameters (base64-encoded protobuf) []: " + read params + + if [ ! -z $params ]; then + partial_data="${partial_data},\"params\":\"${params}\"" + fi + fi fi ;; From 0a9b21ce5f9e86f5cc5379d4541cd6d122a23656 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 26 Oct 2021 00:51:16 +0200 Subject: [PATCH 05/12] Avoid redundant code for interactive parameters query --- scripts/yt-api-helper.sh | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 479e6a2..c660f1b 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -262,12 +262,6 @@ case $endpoint_option in if [ -z $browse_id ]; then browse_id="UCXuqSBlHAE6Xw-yeJA0Tunw"; fi partial_data="\"browseId\":\"${browse_id}\"" - printf "Enter optional parameters (base64-encoded protobuf) []: " - read params - - if [ ! -z $params ]; then - partial_data="${partial_data},\"params\":\"${params}\"" - fi fi ;; @@ -292,16 +286,6 @@ case $endpoint_option in if [ -z $vid ]; then vid="dQw4w9WgXcQ"; fi partial_data="\"videoId\":\"${vid}\"" - - - if [ "$endpoint_option" = "player" ]; then - printf "Enter optional parameters (base64-encoded protobuf) []: " - read params - - if [ ! -z $params ]; then - partial_data="${partial_data},\"params\":\"${params}\"" - fi - fi fi ;; @@ -338,6 +322,28 @@ case $endpoint_option in esac +# +# Interactively request additional parameters for the supported endpoints +# + +if [ $interactive = true ] +then + case $endpoint_option in + + browse|player) + params=$(query_with_default "Enter optional parameters (base64-encoded protobuf)" "") + + if [ ! -z $params ]; then + partial_data="${partial_data},\"params\":\"${params}\"" + fi + ;; + esac +fi + +# new line +echo + + # # Interactive language/region selection # From c47e949a81e5af259b905e338a142709477656a5 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 26 Oct 2021 00:55:03 +0200 Subject: [PATCH 06/12] Use parametric functions for interactive data querying --- scripts/yt-api-helper.sh | 74 ++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index c660f1b..2a85922 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -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() { case $1 in @@ -186,10 +218,7 @@ fi # if [ -z $client_option ]; then - printf "Enter a client to use [web]: " - read client_option - - if [ -z $client_option ]; then client_option="web"; fi + client_option=$(query_with_default "Enter a client to use" "web") fi case $client_option in @@ -256,12 +285,8 @@ case $endpoint_option in endpoint="youtubei/v1/browse" if [ $interactive = true ]; then - printf "Enter browse ID [UCXuqSBlHAE6Xw-yeJA0Tunw]: " - read browse_id - - if [ -z $browse_id ]; then browse_id="UCXuqSBlHAE6Xw-yeJA0Tunw"; fi + browse_id=$(query_with_default "Enter browse ID" "UCXuqSBlHAE6Xw-yeJA0Tunw") partial_data="\"browseId\":\"${browse_id}\"" - fi ;; @@ -269,10 +294,7 @@ case $endpoint_option in endpoint="youtubei/v1/browse" if [ $interactive = true ]; then - printf "Enter continuation token []: " - read token - - if [ -z $token ]; then echo "Error: token required"; return 1; fi + token=$(query_with_error "Enter continuation token" "token required") partial_data="\"continuation\":\"${token}\"" fi ;; @@ -281,11 +303,9 @@ case $endpoint_option in endpoint="youtubei/v1/$endpoint_option" if [ $interactive = true ]; then - printf "Enter video ID [dQw4w9WgXcQ]: " - read vid - - if [ -z $vid ]; then vid="dQw4w9WgXcQ"; fi + vid=$(query_with_default "Enter video ID" "dQw4w9WgXcQ") partial_data="\"videoId\":\"${vid}\"" + fi ;; @@ -293,10 +313,7 @@ case $endpoint_option in endpoint="youtubei/v1/next" if [ $interactive = true ]; then - printf "Enter continuation token []: " - read token - - if [ -z $token ]; then echo "Error: token required"; return 1; fi + token=$(query_with_error "Enter continuation token" "token required") partial_data="\"continuation\":\"${token}\"" fi ;; @@ -305,10 +322,7 @@ case $endpoint_option in endpoint="navigation/resolve_url" if [ $interactive = true ]; then - printf "Enter URL []: " - read url - - if [ -z $url ]; then echo "Error: URL required"; return 1; fi + url=$(query_with_error "Enter URL" "URL required") partial_data="\"url\":\"${url}\"" fi ;; @@ -349,14 +363,8 @@ echo # if [ $interactive = true ]; then - printf "Enter content language (hl) [en]: " - read hl - - printf "Enter content region (gl) [US]: " - read gl - - if [ -z $hl ]; then hl="en"; fi - if [ -z $gl ]; then gl="US"; fi + 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}\"" fi From 1f643d78f852c9a7db8c73fa2cdaf04f7faf7eaf Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Tue, 26 Oct 2021 01:28:48 +0200 Subject: [PATCH 07/12] Add search endpoint to YT API utility script --- scripts/yt-api-helper.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 2a85922..3149f86 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -36,6 +36,7 @@ print_endpoints() echo "next" echo "next-continuation" echo "player" + echo "search" echo "resolve" } @@ -318,6 +319,19 @@ case $endpoint_option in fi ;; + search) + endpoint="youtubei/v1/search" + + if [ $interactive = true ]; then + # Get search query, and escape backslashes and double quotes + query=$( + query_with_error "Enter your search query" "search term required" | + sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' + ) + partial_data="\"query\":\"${query}\"" + fi + ;; + resolve) endpoint="navigation/resolve_url" @@ -344,7 +358,7 @@ if [ $interactive = true ] then case $endpoint_option in - browse|player) + browse|player|search) params=$(query_with_default "Enter optional parameters (base64-encoded protobuf)" "") if [ ! -z $params ]; then From dadee4a14d5394330e26f73cdb058719e20fcd91 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Thu, 28 Oct 2021 15:12:46 +0200 Subject: [PATCH 08/12] Use '/bin/sh' instead of '/bin/zsh' in shebang --- 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 3149f86..ce4b7de 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -1,4 +1,4 @@ -#!/bin/zsh +#!/bin/sh print_help() From 07e6aeecb907b277da0a40b8e13c670133fcc6db Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 12:48:42 +0200 Subject: [PATCH 09/12] Fix typo: missing $ before 'output' vaiable Co-authored-by: Perflyst --- 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 ce4b7de..4dd6811 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -404,7 +404,7 @@ 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' # Default to STDOUT if no output file was given -if [ -z "output" ]; then output='-'; fi +if [ -z "$output" ]; then output='-'; fi # Run! curl --compressed -o "$output" -H "$hdr_ct" -H "$hdr_ua" --data "$data" "$url" From fd739cc5ea339b1e19d0c1eeb7d240c6063b737b Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 18:03:47 +0200 Subject: [PATCH 10/12] Fix issues reported by shellcheck --- scripts/yt-api-helper.sh | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index 4dd6811..a876543 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -1,4 +1,5 @@ #!/bin/sh +# shellcheck disable=SC2236 print_help() @@ -47,7 +48,7 @@ query_with_default() default="$2" printf "\n%s [%s]: " "$prompt" "$default" >&2 - read data + read -r data if [ -z "$data" ]; then echo "$default" @@ -62,7 +63,7 @@ query_with_error() error_message="$2" printf "\n%s []: " "$prompt" >&2 - read data + read -r data if [ -z "$data" ]; then echo "Error: $error_message" @@ -111,7 +112,7 @@ while :; do -c|--client) shift - if [ $# -eq 0 ] || $(is_arg "$1"); then + if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -c/--client" return 2 fi @@ -122,7 +123,7 @@ while :; do -d|--data) shift - if [ $# -eq 0 ] || $(is_arg "$1"); then + if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -d/--data" return 2 fi @@ -133,7 +134,7 @@ while :; do -e|--endpoint) shift - if [ $# -eq 0 ] || $(is_arg "$1"); then + if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -e/--endpoint" return 2 fi @@ -153,7 +154,7 @@ while :; do -o|--output) shift - if [ $# -eq 0 ] || $(is_arg "$1"); then + if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -o/--output" return 2 fi @@ -183,13 +184,13 @@ if [ ! -z "$data" ]; then fi # Can't pass client in non-interactive mode (must be part of data) - if [ ! -z $client_option ]; then + if [ ! -z "$client_option" ]; then echo "Error: -c/--client can't be used with -d/--data" return 2 fi # Endpoint must be given if non-interactive mode - if [ -z $endpoint_option ]; then + if [ -z "$endpoint_option" ]; then echo "Error: In non-interactive mode, an endpoint must be passed with -e/--endpoint" return 2 fi @@ -203,9 +204,9 @@ fi if [ -z "$output" ] && [ $interactive = true ]; then printf "\nIt is recommended to use --output in interactive mode.\nContinue? [y/N]: " - read confirm + read -r confirm - if [ -z $confirm ]; then confirm="n"; fi + if [ -z "$confirm" ]; then confirm="n"; fi case $confirm in [Yy]|[Yy][Ee][Ss]) ;; @@ -218,7 +219,7 @@ fi # Client selection # -if [ -z $client_option ]; then +if [ -z "$client_option" ]; then client_option=$(query_with_default "Enter a client to use" "web") fi @@ -247,7 +248,7 @@ case $client_option in ;; android) - apikey= "AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w" + apikey="AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w" client_name="ANDROID" client_vers="16.20" ;; @@ -271,9 +272,8 @@ esac # Endpoint selection # -if [ -z $endpoint_option ]; then - printf "Enter an endpoint to request []: " - read endpoint_option +if [ -z "$endpoint_option" ]; then + endpoint_option=$(query_with_default "Enter an endpoint to request" "") fi case $endpoint_option in @@ -361,7 +361,7 @@ then browse|player|search) params=$(query_with_default "Enter optional parameters (base64-encoded protobuf)" "") - if [ ! -z $params ]; then + if [ ! -z "$params" ]; then partial_data="${partial_data},\"params\":\"${params}\"" fi ;; From 22ecea01287d2422e32a2d503bc88efc183a8590 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 18:09:45 +0200 Subject: [PATCH 11/12] Add basic github action that runs shellcheck --- .github/workflows/shellcheck.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/shellcheck.yml diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml new file mode 100644 index 0000000..6c3256a --- /dev/null +++ b/.github/workflows/shellcheck.yml @@ -0,0 +1,14 @@ +name: shellcheck-validation + +on: push + +jobs: + shellcheck: + runs-on: ubuntu-latest + + steps: + - name: Clone repository + uses: actions/checkout@v2 + + - name: Execute ShellCheck + run: shellcheck scripts/*.sh From ffdf371763a414f82fbf141e59c2d106c912023b Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Fri, 29 Oct 2021 19:07:55 +0200 Subject: [PATCH 12/12] Use 'exit' instead of 'return' to end script execution --- scripts/yt-api-helper.sh | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index a876543..25ed099 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -67,7 +67,7 @@ query_with_error() if [ -z "$data" ]; then echo "Error: $error_message" - return 1 + exit 1 else echo "$data" fi @@ -114,7 +114,7 @@ while :; do if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -c/--client" - return 2 + exit 2 fi client_option=$1 @@ -125,7 +125,7 @@ while :; do if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -d/--data" - return 2 + exit 2 fi data=$1 @@ -136,7 +136,7 @@ while :; do if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -e/--endpoint" - return 2 + exit 2 fi endpoint_option=$1 @@ -144,7 +144,7 @@ while :; do -h|--help) print_help - return 0 + exit 0 ;; -i|--interactive) @@ -156,7 +156,7 @@ while :; do if [ $# -eq 0 ] || is_arg "$1"; then echo "Error: missing argument after -o/--output" - return 2 + exit 2 fi output="$1" @@ -164,7 +164,7 @@ while :; do *) echo "Error: unknown argument '$1'" - return 2 + exit 2 ;; esac @@ -180,26 +180,26 @@ if [ ! -z "$data" ]; then # Can't pass data in interactive mode if [ $interactive = true ]; then echo "Error: -d/--data can't be used with -i/--interactive" - return 2 + 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" - return 2 + exit 2 fi # Endpoint must be given if non-interactive mode if [ -z "$endpoint_option" ]; then echo "Error: In non-interactive mode, an endpoint must be passed with -e/--endpoint" - return 2 + exit 2 fi fi if [ -z "$data" ] && [ $interactive = false ]; then # Data must be given if non-interactive mode echo "Error: In non-interactive mode, data must be passed with -d/--data" - return 2 + exit 2 fi if [ -z "$output" ] && [ $interactive = true ]; then @@ -210,7 +210,7 @@ if [ -z "$output" ] && [ $interactive = true ]; then case $confirm in [Yy]|[Yy][Ee][Ss]) ;; - *) return 0;; + *) exit 0;; esac fi @@ -226,7 +226,7 @@ fi case $client_option in help) print_clients - return 0 + exit 0 ;; web) @@ -263,7 +263,7 @@ case $client_option in echo "Error: Unknown client '$client_option'" echo "" print_clients - return 1 + exit 1 ;; esac @@ -279,7 +279,7 @@ fi case $endpoint_option in help) print_endpoints - return 0 + exit 0 ;; browse) @@ -345,7 +345,7 @@ case $endpoint_option in echo "Error: Unknown endpoint '$endpoint_option'" echo "" print_clients - return 1 + exit 1 ;; esac