api-helper: Improve help and error messages

This commit is contained in:
Samantaz Fox 2023-02-15 23:40:00 +01:00
parent 5b4e1b24ab
commit 5f20877542
No known key found for this signature in database
GPG key ID: F42821059186176E

View file

@ -5,18 +5,20 @@
# more intuitive than `-n` # more intuitive than `-n`
print_help() print_usage()
{ {
echo "Usage: yt-api-helper -i [-c <client>] [-e <endpoint>]" echo "Usage: yt-api-helper -i [-c <client>] [-e <endpoint>]"
echo "Usage: yt-api-helper -c <client> -e <endpoint> -d <data>" echo "Usage: yt-api-helper -c <client> -e <endpoint> -d <data>"
}
print_help()
{
print_usage
echo "" echo ""
echo "Options:" echo "Options:"
echo " -c,--client Client to use. Pass 'help' to this option to get" echo " -c,--client Client to use. Mandatory in non-interactive mode."
echo " the list of supported clients. Mandatory in"
echo " non-interactive mode."
echo " -d,--data Raw data to send to the API" echo " -d,--data Raw data to send to the API"
echo " -e,--endpoint Youtube endpoint to request. Pass 'help' to this" echo " -e,--endpoint Youtube endpoint to request."
echo " option to get the list of supported endpoints."
echo " Mandatory in non-interactive mode" echo " Mandatory in non-interactive mode"
echo " -h,--help Show this help" echo " -h,--help Show this help"
echo " -i,--interactive Run in interactive mode" echo " -i,--interactive Run in interactive mode"
@ -24,29 +26,33 @@ print_help()
echo "" echo ""
echo " --debug Show what is sent to the API" echo " --debug Show what is sent to the API"
echo "" echo ""
print_clients
print_endpoints
} }
print_clients() print_clients()
{ {
echo ""
echo "Available clients:" echo "Available clients:"
echo "web" echo " - web"
echo "web-embed" echo " - web-embed"
echo "web-mobile" echo " - web-mobile"
echo "android" echo " - android"
echo "android-embed" echo " - android-embed"
echo "apple-ios" echo " - apple-ios"
} }
print_endpoints() print_endpoints()
{ {
echo ""
echo "Available endpoints:" echo "Available endpoints:"
echo "browse" echo " - browse"
echo "browse-continuation" echo " - browse-continuation"
echo "next" echo " - next"
echo "next-continuation" echo " - next-continuation"
echo "player" echo " - player"
echo "search" echo " - search"
echo "resolve" echo " - resolve"
} }
@ -131,6 +137,7 @@ while :; do
if [ $# -eq 0 ] || is_arg "$1"; then if [ $# -eq 0 ] || is_arg "$1"; then
echo "Error: missing argument after -c/--client" echo "Error: missing argument after -c/--client"
print_usage
exit 2 exit 2
fi fi
@ -142,6 +149,7 @@ while :; do
if [ $# -eq 0 ] || is_arg "$1"; then if [ $# -eq 0 ] || is_arg "$1"; then
echo "Error: missing argument after -d/--data" echo "Error: missing argument after -d/--data"
print_usage
exit 2 exit 2
fi fi
@ -153,6 +161,7 @@ while :; do
if [ $# -eq 0 ] || is_arg "$1"; then if [ $# -eq 0 ] || is_arg "$1"; then
echo "Error: missing argument after -e/--endpoint" echo "Error: missing argument after -e/--endpoint"
print_usage
exit 2 exit 2
fi fi
@ -173,6 +182,7 @@ while :; do
if [ $# -eq 0 ] || is_arg "$1"; then if [ $# -eq 0 ] || is_arg "$1"; then
echo "Error: missing argument after -o/--output" echo "Error: missing argument after -o/--output"
print_usage
exit 2 exit 2
fi fi
@ -185,6 +195,7 @@ while :; do
*) *)
echo "Error: unknown argument '$1'" echo "Error: unknown argument '$1'"
print_usage
exit 2 exit 2
;; ;;
esac esac
@ -201,6 +212,7 @@ if [ ! -z "$data" ]; then
# Can't pass data in interactive mode # Can't pass data in interactive mode
if [ $interactive = true ]; then if [ $interactive = true ]; then
echo "Error: -d/--data can't be used with -i/--interactive" echo "Error: -d/--data can't be used with -i/--interactive"
print_usage
exit 2 exit 2
fi fi
@ -208,12 +220,14 @@ if [ ! -z "$data" ]; then
# so the right API key is passed as a URL parameter # so the right API key is passed as a URL parameter
if [ -z "$client_option" ]; then if [ -z "$client_option" ]; then
echo "Error: -c/--client is required to select an API key" echo "Error: -c/--client is required to select an API key"
print_usage
exit 2 exit 2
fi fi
# Endpoint must be given if non-interactive mode # 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" echo "Error: In non-interactive mode, an endpoint must be passed with -e/--endpoint"
print_usage
exit 2 exit 2
fi fi
fi fi
@ -221,6 +235,7 @@ fi
if [ -z "$data" ] && [ $interactive = false ]; then if [ -z "$data" ] && [ $interactive = false ]; then
# Data must be given if non-interactive mode # Data must be given if non-interactive mode
echo "Error: In non-interactive mode, data must be passed with -d/--data" echo "Error: In non-interactive mode, data must be passed with -d/--data"
print_usage
exit 2 exit 2
fi fi
@ -242,15 +257,16 @@ fi
# #
if [ -z "$client_option" ]; then if [ -z "$client_option" ]; then
if [ $interactive = true ]; then
print_clients
echo ""
client_option=$(query_with_default "Enter a client to use" "web") client_option=$(query_with_default "Enter a client to use" "web")
else
exit 2
fi
fi fi
case $client_option in case $client_option in
help)
print_clients
exit 0
;;
web) web)
apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8" apikey="AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"
client_name="WEB" client_name="WEB"
@ -295,7 +311,6 @@ case $client_option in
*) *)
echo "Error: Unknown client '$client_option'" echo "Error: Unknown client '$client_option'"
echo ""
print_clients print_clients
exit 1 exit 1
;; ;;
@ -307,15 +322,16 @@ esac
# #
if [ -z "$endpoint_option" ]; then if [ -z "$endpoint_option" ]; then
if [ $interactive = true ]; then
print_endpoints
echo ""
endpoint_option=$(query_with_default "Enter an endpoint to request" "") endpoint_option=$(query_with_default "Enter an endpoint to request" "")
else
exit 2
fi
fi fi
case $endpoint_option in case $endpoint_option in
help)
print_endpoints
exit 0
;;
browse) browse)
endpoint="youtubei/v1/browse" endpoint="youtubei/v1/browse"
@ -377,8 +393,7 @@ case $endpoint_option in
*) *)
echo "Error: Unknown endpoint '$endpoint_option'" echo "Error: Unknown endpoint '$endpoint_option'"
echo "" print_endpoints
print_clients
exit 1 exit 1
;; ;;
esac esac