anoncheg

Command line language translater google, abby,forvo

Jul 27th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.99 KB | None | 0 0
  1. #!/bin/bash
  2. # access translate.google.com from terminal 2
  3.  
  4. help='translate [-s] <text>
  5. if text is english, "target" language is russian
  6. otherwise, "target" language is DEFAULT_TARGET_LANG
  7. -s, --sound Enable sound for one word'
  8.  
  9. jsout="var arr = eval(myJSONObject);
  10. if(typeof arr[1] !== 'undefined' && arr[1] !== null){
  11.     for (var i = 0; i < arr[1].length; i++){
  12.         if(typeof arr[1][i][0] !== 'undefined' && arr[1][i][0] !== null){
  13.             print(\"\033[1;34m\"+arr[1][i][0]+\"\033[0m\");
  14.             for (var e = 0; e < arr[1][i][2].length; e++){
  15.                 print(arr[1][i][2][e]);
  16.             }
  17.         }
  18.     }
  19. }"
  20.  
  21. # adjust to taste
  22. DEFAULT_TARGET_LANG=en
  23. flogin=121212
  24. fpass=121212
  25.  
  26. request=($*)
  27. sound=0
  28. if [[ $1 = -h || $1 = --help ]]; then
  29.     echo "$help"
  30.     exit
  31. fi
  32. if [[ $1 == -s || $1 == --sound ]];then
  33.     sound=1
  34.     request[0]=""
  35. fi
  36. request=${request[*]}
  37.  
  38. source=auto
  39. target="$DEFAULT_TARGET_LANG"
  40. tchar=${request:0:1}
  41. tcharnum=$(printf "%d" "'${tchar}")
  42. if [[ $tcharnum -ge 65 && $tcharnum -le 122 ]]; then #if request is english
  43.     source=en
  44.     target=ru #if text is english, "target" language is russian
  45. fi
  46.  
  47. if [[ ${#request} -gt 300 || "$request" =~ \..+ ]]; then
  48.     result=$(curl -s -i --user-agent "" -d "sl=$source" -d "tl=$target" --data-urlencode "text=$request" http://translate.google.com)
  49.     encoding=$(awk '/Content-Type: .* charset=/ {sub(/^.*charset=["'\'']?/,""); sub(/[ "'\''].*$/,""); print}' <<<"$result")
  50.     #iconv -f $encoding <<<"$result" | awk 'BEGIN {RS="<div"};/<span[^>]* id=["'\'']?result_box["'\'']?/ {sub(/^.*id=["'\'']?result_box["'\'']?(>| [^>]*>)([ \n\t]*<[^>]*>)*/,"");sub(/<.*$/,"");print}' | html2text -utf8
  51. #   iconv -f $encoding <<<"$result" |  awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8
  52.     echo -e "\033[32;1m"$(iconv -f $encoding <<<"$result" |  awk 'BEGIN {RS="</div>"};/<span[^>]* id=["'\'']?result_box["'\'']?/' | html2text -utf8)"\033[0m"
  53.  
  54. else
  55.     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")
  56.     res=$(echo "$result2" | grep -o '\[.*\]' | sed 's/.*\[\[\[\"\([^\"]*\)".*/\1/')
  57.     echo $(tput bold)$(tput setaf 2)$res$(tput sgr0)
  58.     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')
  59.     echo $fl
  60. #   echo $result2 | grep -o '"[^"]*/i[^"]*"'
  61.  
  62.     echo -n "var myJSONObject = " > tmpjsobj
  63.     echo -n "$result2" | grep -o '\[.*\]' >> tmpjsobj
  64.     echo ";">> tmpjsobj
  65.     echo -n "$jsout" >> tmpjsobj
  66.     js tmpjsobj
  67.     rm tmpjsobj
  68.  
  69.     if [[ $(echo "$request"  |wc -w) -eq 1 && $source == en && ! $fl && $(echo "$request" | tr '[:upper:]' '[:lower:]') !=  $(echo "$res" | tr '[:upper:]' '[:lower:]') ]]; then
  70.         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'))
  71.  
  72.         if [[ $trans ]] ; then
  73.             echo [$trans]
  74.  
  75.             #saving words
  76.             if [[ ! $(grep "$request" ~/translated_words 2>/dev/null) ]]; then
  77.                 #echo -e "$request \t\t\t\t\t\t\t\t\t\t [$trans]" >> ~/translated_words
  78.                 echo -e "$request" >> ~/translated_words
  79.             fi
  80.  
  81.             if [[ $sound == 1 ]]; then
  82.                 if [[ ! -e  ~/tmps_"$request".mp3 ]]; then
  83.                     echo -n -e "\tgetting sound from forvo.com..."
  84.                     curl -s -c tmpcookie --user-agent "" -d "login=$flogin&password=$fpass" http://ru.forvo.com/login/ -o/dev/null
  85.                     slink=$(curl -s -b tmpcookie http://ru.forvo.com/word/"$request"/ | grep -o '[^"]*/download/mp3/'"$request"'/en/[^"]*' |head -n 1 )
  86.                     [[ $(ls ~/tmps_*.mp3 | wc -l) -gt 20 ]] && rm ~/tmps_*.mp3 > /dev/null # small error message on the first run
  87.                     curl -s -b tmpcookie --user-agent "" -o ~/tmps_"$request".mp3 http://ru.forvo.com"$slink"
  88.                     rm tmpcookie
  89.                 fi
  90.                 playsound --loop 1 ~/tmps_"$request".mp3 2>/dev/null
  91.             fi
  92.         fi
  93.     fi
  94. fi
  95. exit
Add Comment
Please, Sign In to add comment