Slow bash script

1

I use a bash script to translate words from other languages into Portuguese. It always worked very well, but for a few days it got extremely slow, to the point that I could not use it. Home some people informed me that the problem could be in the "wget". I've done some testing by replacing wget with Axel or aria2c and speed has returned to normal, but using those two commands I can not get the translation.

The original code is this one:

#!/usr/bin/env bash
text="$(xsel -o)"
translate="$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=pt&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2}')"
echo -e "Original text:" "$text"'\n' > /tmp/notitrans
echo "Translation:" "$translate" >> /tmp/notitrans
zenity --text-info --title="Translation" --filename=/tmp/notitrans

Line 3 of this code, replacing "wget" with "Axel" is:

translate="$(axel -n 4 "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=pt&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2}')"

And replacing "wget" with "aria2c" is:

translate="$(aria2c "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=pt&dt=t&q=$(echo $text | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2}')"

Possibly I'm not sure how to set the options for Axel and Aria2c. For me either does either, I need the code to work again. Would anyone guide me in this direction?

    
asked by anonymous 16.09.2017 / 02:49

1 answer

1

It seems unlikely that the delay is due to wget. (but this is not the issue)

Probably the problem with the examples tested has to do with the lack of definition of User-agent (the -U "Mozilla / 5.0" of wget) - which causes googleapis not to accept the request.

Suggestion to use curl .

$ gapi="http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=pt&dt=t"
$ curl -A "Mozilla/5.0" $gapi -d 'q=le fromage est trés bon'
[[["o queijo é muito bom","le fromage est trés bon",null,null,3.....["fr"]]]

Processing the web-service response ( respostaJSON[0][0][0] ):

$ curl -A "Mozilla/5.0" $gapi -d 'q=le fromage est trés bon'| jq .[0][0][0]
"o queijo é muito bom"
    
27.09.2017 / 13:19