Переводит и произносит, с английского на русский и с других языков на английский.
Язык определяет по первому символу запроса.
требует:
1)curl
2)Mazilla js shell
3)аккаунт на forvo.com
#!/bin/bash
# access translate.google.com from terminal
help='translate <text>
if text is english, DEFAULT_TARGET_LANG is russian
otherwise, DEFAULT_TARGET_LANG is english'
jsout="var arr = eval(myJSONObject);
if(typeof arr[1] !== 'undefined' && arr[1] !== null){
for (var i = 0; i < arr[1].length; i++){
if(typeof arr[1][i][0] !== 'undefined' && arr[1][i][0] !== null){
print(\"\033[1;34m\"+arr[1][i][0]+\"\033[0m\");
for (var e = 0; e < arr[1][i][2].length; e++){
print(arr[1][i][2][e]);
}
}
}
}"
# adjust to taste
DEFAULT_TARGET_LANG=en
if [[ $1 = -h || $1 = --help ]]
then
echo "$help"
exit
fi
source=auto
target="$DEFAULT_TARGET_LANG"
tchar=${1:0:1}
tcharnum=$(printf "%d" "'${tchar}")
if [[ $tcharnum -ge 65 && $tcharnum -le 122 ]]; then
target=ru
fi
request=$*
if [[ ${#request} -gt 2000 ]]; then
result=$(curl -s -i --user-agent "" -d "sl=$source" -d "tl=$target" --data-urlencode "text=$*" http://translate.google.com)
encoding=$(awk '/Content-Type: .* charset=/ {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' <<<"$result")
#iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="<div"};/<span[^>]* id=["'\'']?result_box["'\'']?/ {sub(/^.*id=["'\'']?result_box["'\'']?(>| [^>]*>)([ \n\t]*<[^>]*>)*/,"");sub(/<.*$/,"");print}' | html2text -utf8
# iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8
echo -e "\033[32;1m"$(iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8)"\033[0m"
else
result2=$(curl -s -i --user-agent "" --data-urlencode "text=$*" "http://translate.google.com/translate_a/t?client=t&hl=ru&sl=$source&tl=$target&ie=UTF-8&oe=UTF-8&multires=1&ssel=0&tsel=0&sc=1")
res=$(echo $result2 | sed 's/.*\[\[\[\"\([^\"]*\)".*/\1/')
echo $(tput bold)$(tput setaf 2)$res$(tput sgr0)
fl=$(echo $result2 | grep -o '"[^"]*/i[^"]*"' | sed 's/\\u003cb\\u003e\\u003ci\\u003e/'$(tput bold)$(tput setaf 3)'/g' | sed 's/\\u003c\/i\\u003e\\u003c\/b\\u003e/'$(tput sgr0)'/g' | sed 's/\\u0026#39;.//')
echo $fl
# echo $result2 | grep -o '"[^"]/i[^"]"'
echo -n "var myJSONObject = " > tmpjsobj
echo -n $result2 | grep -o '\[.*\]' >> tmpjsobj
echo ";">> tmpjsobj
echo -n "$jsout" >> tmpjsobj
js tmpjsobj
rm tmpjsobj
if [[ $target == ru && $(echo $* |wc -w) -eq 1 && ! $fl && $(echo $* | tr '[:upper:]' '[:lower:]') != $(echo $res | tr '[:upper:]' '[:lower:]') ]]; then
echo -e "\tgetting sound..."
flogin=your forvo.com account!!!!!!!
fpass=your pass!!!!!!!!!!!
word=$*
curl -s -c tmpcookie --user-agent "" -d "login=$flogin&password=$fpass" http://ru.forvo.com/login/
aa=$(curl -s -j tmpcookie http://ru.forvo.com/word/"$word"/ | grep -o '[^"]*/download/mp3/'"$word"'/[^"]*' |head -n 1 )
curl -s -b tmpcookie --user-agent "" -o s.mp3 http://ru.forvo.com$aa
playsound s.mp3
playsound s.mp3
# rm s.mp3
fi
fi
exit