From 8df7834c36b77917b374f80c52a6a4f6d5dc7fc1 Mon Sep 17 00:00:00 2001 From: Samantaz Fox Date: Sat, 30 Mar 2024 22:54:20 +0100 Subject: [PATCH] api-helper: Move request logic to a separate function --- scripts/yt-api-helper.sh | 58 ++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/scripts/yt-api-helper.sh b/scripts/yt-api-helper.sh index d94e39c..0bcb0ea 100755 --- a/scripts/yt-api-helper.sh +++ b/scripts/yt-api-helper.sh @@ -360,6 +360,40 @@ make_request_data() } +# +# Final command - send request to Youtube +# + +send_request() +{ + request_data="$1" + output_file="$2" + + url="https://www.youtube.com/${endpoint}?key=${apikey}" + + # Headers + hdr_ct='Content-Type: application/json; charset=utf-8' + hdr_ua="User-Agent: ${user_agent}" + + # Run! + temp_dl=_curl_$(date '+%s') + + curl --compressed -H "$hdr_ct" -H "$hdr_ua" --data "$request_data" "$url" | \ + sed -E ' + /^\s+"(clickT|t)rackingParams.+,$/d + s/,?\n\s+"(clickT|t)rackingParams.+$// + ' > "$temp_dl" + + # Print to STDOUT if no output file was given + if [ -z "$output_file" ]; then + cat "$temp_dl" + rm "$temp_dl" + else + mv -- "$temp_dl" "$output_file" + fi +} + + # # Parameters init # @@ -542,26 +576,4 @@ if [ $interactive = true ]; then data=$(make_request_data) fi - -url="https://www.youtube.com/${endpoint}?key=${apikey}" - -# Headers -hdr_ct='Content-Type: application/json; charset=utf-8' -hdr_ua="User-Agent: ${user_agent}" - -# Run! -temp_dl=_curl_$(date '+%s') - -curl --compressed -H "$hdr_ct" -H "$hdr_ua" --data "$data" "$url" | \ -sed -E ' - /^\s+"(clickT|t)rackingParams.+,$/d - s/,?\n\s+"(clickT|t)rackingParams.+$// -' > "$temp_dl" - -# Print to STDOUT if no output file was given -if [ -z "$output" ]; then - cat "$temp_dl" - rm "$temp_dl" -else - mv -- "$temp_dl" "$output" -fi +send_request "$data" "$output"