Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # access translate.google.com from terminal 2
- help='translate [-s] <text>
- if text is english, "target" language is russian
- otherwise, "target" language is DEFAULT_TARGET_LANG
- -s, --sound Enable sound for one word'
- 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
- flogin=121212
- fpass=121212
- request=($*)
- sound=0
- if [[ $1 = -h || $1 = --help ]]; then
- echo "$help"
- exit
- fi
- if [[ $1 == -s || $1 == --sound ]];then
- sound=1
- request[0]=""
- fi
- request=${request[*]}
- source=auto
- target="$DEFAULT_TARGET_LANG"
- tchar=${request:0:1}
- tcharnum=$(printf "%d" "'${tchar}")
- if [[ $tcharnum -ge 65 && $tcharnum -le 122 ]]; then #if request is english
- source=en
- target=ru #if text is english, "target" language is russian
- fi
- if [[ ${#request} -gt 300 || "$request" =~ \..+ ]]; then
- result=$(curl -s -i --user-agent "" -d "sl=$source" -d "tl=$target" --data-urlencode "text=$request" 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=$request" "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" | grep -o '\[.*\]' | 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;.//g')
- 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 [[ $(echo "$request" |wc -w) -eq 1 && $source == en && ! $fl && $(echo "$request" | tr '[:upper:]' '[:lower:]') != $(echo "$res" | tr '[:upper:]' '[:lower:]') ]]; then
- trans=$(curl -s --user-agent "" http://lingvopro.abbyyonline.com/en/Translate/en-ru/"$request" | grep -o '"[^"]*Handlers/TranscriptionHandler\.ashx[^"]*"' | sed 's/.*=\(.*\)"/\1/'| echo -n -e $(sed 's/+/ /g; s/%/\\x/g'))
- if [[ $trans ]] ; then
- echo [$trans]
- #saving words
- if [[ ! $(grep "$request" ~/translated_words 2>/dev/null) ]]; then
- #echo -e "$request \t\t\t\t\t\t\t\t\t\t [$trans]" >> ~/translated_words
- echo -e "$request" >> ~/translated_words
- fi
- if [[ $sound == 1 ]]; then
- if [[ ! -e ~/tmps_"$request".mp3 ]]; then
- echo -n -e "\tgetting sound from forvo.com..."
- curl -s -c tmpcookie --user-agent "" -d "login=$flogin&password=$fpass" http://ru.forvo.com/login/ -o/dev/null
- slink=$(curl -s -b tmpcookie http://ru.forvo.com/word/"$request"/ | grep -o '[^"]*/download/mp3/'"$request"'/en/[^"]*' |head -n 1 )
- [[ $(ls ~/tmps_*.mp3 | wc -l) -gt 20 ]] && rm ~/tmps_*.mp3 > /dev/null # small error message on the first run
- curl -s -b tmpcookie --user-agent "" -o ~/tmps_"$request".mp3 http://ru.forvo.com"$slink"
- rm tmpcookie
- fi
- playsound --loop 1 ~/tmps_"$request".mp3 2>/dev/null
- fi
- fi
- fi
- fi
- exit
Add Comment
Please, Sign In to add comment