Slow bash script

Asked

Viewed 147 times

1

I use a bash script to translate words from other languages into Portuguese. It always worked very well, but from a few days to here became extremely slow, to the point that I could not use it.
some people informed me that the problem could be in the "wget". I did some tests replacing "wget" with "Axel" or "aria2c" and the speed returned to normal, but using these two commands I cannot 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}')"

Maybe I don’t know how to set the options for Axel and Aria2c. For me it doesn’t matter if any of them, I need the code to work again. Would anyone know to steer me in that direction?

  • There is already a tool that already does this, from a look: https://github.com/soimort/translate-shell

  • Gfleck, this tool is for terminal use only. The script I use is more free, I can use it in any environment and therefore it is more practical. As I read long books, it is faster to have a script that recognizes highlighted words and shows the definition automatically; this greatly speeds up reading.

1 answer

1

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

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

Suggestion to use the 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"
  • 1

    Jjoão, it seems that the problem was the Ipv6 port, somehow she was not letting wget make its connection with Google. I set wget to use the Ipv4 port, adding the "-4" right after it, (wget -4 "Mozilla/5.0" ...) and the script went back to normal.

  • @Ricardo, sometimes, some of these webservices have some maximum limits of queries, after which they cut or delay the answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.