mirror of
				https://gitea.invidious.io/iv-org/youtube-utils.git
				synced 2024-08-15 00:53:16 +00:00 
			
		
		
		
	api-helper: Move request logic to a separate function
This commit is contained in:
		
							parent
							
								
									d45d85391e
								
							
						
					
					
						commit
						8df7834c36
					
				
					 1 changed files with 35 additions and 23 deletions
				
			
		|  | @ -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" | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue