Jump to content

Recommended Posts

Posted

Hey I searched code on autoit forum and modify it according to my needs and try to translate text from Russian to English in return I'm getting error such as "Error 411 (Length Required)!!1"

Both my autoit codes and error I got are given below, please help me to solve this issue, Thanks

Autoit codes to translate text form Russian to English;

#include <urlencode.au3>
$File1 = @ScriptDir & "\russian_text.txt"
$txt = FileRead($File1)
; Try to convert line breaks with .....so final URL looks simpler
$txt = StringReplace($txt, @CRLF, '...........')
$txt = StringReplace($txt, @LF, '.............')
$txt = StringReplace($txt, @CR, '.............')
FileWrite (@scriptdir & '\russian_text2.txt', $txt)
$openfile = @ScriptDir & "\russian_text2.txt"
$mytext = FileRead ($openfile)
$encoding = urlencode ($mytext)
FileWrite (@scriptdir & '\enooding.txt', $encoding)
$from = "ru"
$to = "en"
$url = "https://translate.googleapis.com/translate_a/single?client=gtx"
$url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $encoding
$oHTTP = ObjCreate("Microsoft.XMLHTTP")
$oHTTP.Open("POST", $url, False)
$oHTTP.Send()
$sData = $oHTTP.ResponseText
$sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @crlf)
FileWrite (@scriptdir & '\errorcode.txt', $sData)
Msgbox(0,"", $sData)

In response of above codes, I'm getting below error;

 

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 411 (Length Required)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>411.</b> <ins>That’s an error.</ins>
  <p>POST requests require a <code>Content-length</code> header.  <ins>That’s all we know.</ins>

 

Posted

Interesting.

Try this:

$oHttp.SetRequestHeader('Content-Length', $nValue)

 

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 12/12/2016 at 12:09 PM, mLipok said:

Interesting.

Try this:

$oHttp.SetRequestHeader('Content-Length', $nValue)

 

 

Expand  

Thanks for guidance but where to put it even i put it after below code

$oHTTP = ObjCreate("Microsoft.XMLHTTP")

it give me error $nValue not declared like you can say that variable used without defining it

If i put $nValue = "0" then i got following error;

Here is Error

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 413 (Request Entity Too Large)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>413.</b> <ins>That’s an error.</ins>
  <p>Your client issued a request that was too large.
 <script>
  (function() { var c=function(a,d,b){a=a+"=deleted"+("; path="+d);null!=b&&(a+="; domain="+b);document.cookie=a+"; expires=Thu, 01 Jan 1970 00:00:00 GMT"};var g=function(a){var d=e,b=location.hostname;c(d,a,null);c(d,a,b);for(var f=0;;){f=b.indexOf(".",f+1);if(0>f)break;c(d,a,b.substring(f+1))}};var h;if(4E3<unescape(encodeURI(document.cookie)).length){for(var k=document.cookie.split(";"),l=[],m=0;m<k.length;m++){var n=k[m].match(/^\s*([^=]+)/);n&&l.push(n[1])}for(var p=0;p<l.length;p++){var e=l[p];g("/");for(var q=location.pathname,r=0;;){r=q.indexOf("/",r+1);if(0>r)break;var t=q.substring(0,r);g(t);g(t+"/")}"/"!=q.charAt(q.length-1)&&(g(q),g(q+"/"))}h=!0}else h=!1;h&&setTimeout(function(){location.reload(!0)},1E3); })();
</script>
 <ins>That’s all we know.</ins>

Here is my new autoit code;

#include <urlencode.au3>
$File1 = @ScriptDir & "\russian_text.txt"
$txt = FileRead($File1)
; Try to convert line breaks with .....so final URL looks simpler
$txt = StringReplace($txt, @CRLF, '...........')
$txt = StringReplace($txt, @LF, '.............')
$txt = StringReplace($txt, @CR, '.............')
FileWrite (@scriptdir & '\russian_text2.txt', $txt)
$openfile = @ScriptDir & "\russian_text2.txt"
$mytext = FileRead ($openfile)
$encoding = urlencode ($mytext)
FileWrite (@scriptdir & '\enooding.txt', $encoding)
$from = "ru"
$to = "en"
$url = "https://translate.googleapis.com/translate_a/single?client=gtx"
$url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $encoding
$nValue = "0"
$oHTTP = ObjCreate("Microsoft.XMLHTTP")
$oHttp.SetRequestHeader('Content-Length', $nValue)
$oHTTP.Open("POST", $url, False)
$oHTTP.Send()
$sData = $oHTTP.ResponseText
$sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @crlf)
FileWrite (@scriptdir & '\errorcode.txt', $sData)
Msgbox(0,"", $sData)

 

Posted (edited)

The script work on my side:

$sString = "This is a test phrase. Translate me. I'll write al very long phrase for see if there is an error based on the text lenght but it doesn't on my side, i don't know. Well good luck, see you soon on Autoit thread!"
$sFrom = "en"
$sTo = "ru"
$sURL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & $sFrom & "&tl=" & $sTo & "&dt=t&q=" & $sString

$oHTTP = ObjCreate("Microsoft.XMLHTTP")
$oHTTP.Open("GET", $sURL, False)
$oHTTP.Send()
$sData = $oHTTP.ResponseText
$sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @CRLF)
MsgBox(0, 0, $sData)

29zeptv.png

Probably there is some mistake when you read and decode the file?

EDIT: Replace POST with GET, better the second one.

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Posted
  On 12/12/2016 at 1:51 PM, jonson1986 said:

p>Your client issued a request that was too large.

Expand  

$nValue should not be = 0
You must to set proper value.

 


What is the problem with reverse translation ?

_Example()
Func _Example()
    Local $sString = "This is a test phrase. Translate me. I'll write al very long phrase for see if there is an error based on the text lenght but it doesn't on my side, i don't know. Well good luck, see you soon on Autoit thread!"
    Local $sFrom = "en"
    Local $sTo = "ru"

    Local $sData = _Google_Translate($sFrom, $sTo, $sString)
    $sData = _Google_Translate($sTo, $sFrom, $sData)
    MsgBox(0, 0, $sData)
EndFunc   ;==>_Example


Func _Google_Translate($sFrom, $sTo, $sString)
    Local $sURL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & $sFrom & "&tl=" & $sTo & "&dt=t&q=" & $sString

    Local $oHTTP = ObjCreate("Microsoft.XMLHTTP")
    $oHTTP.Open("POST", $sURL, False)
    $oHTTP.Send()
    Local $sData = $oHTTP.ResponseText
    $sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @CRLF)
    Return $sData

EndFunc   ;==>_Google_Translate

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Thanks for kind responses,

I tried and found it working from English to any other language fine but when i tried to translate any other language back to English, in this condition I need to encode other language into UTF-8 google friend URL parameters and when i sent request it give me error for what i wrote above, please help me to reverse translation from Russian to English, it'll be really helpful for me.

Thanks

Posted

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Thanks mLipok,

sorry to tell you, I got an idea about queries but I'm not able to make my code working even using the guidance available on stackoverflow.

 

Posted (edited)
_Example()
Func _Example()
    Local $sString = _
            "This is a test phrase. " & _
            "Translate me. " & _
            "I'll write a very long phrase, to see if there is an error based on the text lenght but it doesn't on my side. " & _
            "I don't know why. " & _
            "Well good luck, see you soon on Autoit thread!"
    Local $sFrom = "en"
    Local $sTo = "ru"

    Local $sData = _Google_Translate($sFrom, $sTo, $sString)
    MsgBox(0, $sFrom & '>>' & $sTo, $sData)

    $sData = _Google_Translate($sTo, $sFrom, _URIEncode(StringRegExpReplace($sData, '\R', ' ')))
    MsgBox(0, $sTo & '>>' & $sFrom, $sData)
EndFunc   ;==>_Example


Func _Google_Translate($sFrom, $sTo, $sString)
    ; http://stackoverflow.com/questions/26714426/what-is-the-meaning-of-google-translate-query-params
    ; '&ie=UTF-8&oe=UTF-8'

    Local $sURL = _
            "https://translate.googleapis.com/translate_a/single?client=gtx" & _
            "&sl=" & $sFrom & "&tl=" & $sTo & "&dt=t&q=" & $sString

    Local $oHTTP = ObjCreate("Microsoft.XMLHTTP")
    $oHTTP.Open("POST", $sURL, False)
    $oHTTP.Send()
    Local $sData = $oHTTP.ResponseText
    $sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @CRLF)
    Return $sData

EndFunc   ;==>_Google_Translate

Func _URIEncode($sData)
    ; Prog@ndy
    ; https://www.autoitscript.com/forum/topic/95850-url-encoding/?do=findComment&comment=689060
    Local $aData = StringSplit(BinaryToString(StringToBinary($sData, 4), 1), "")
    Local $nChar
    $sData = ""
    For $i = 1 To $aData[0]
        ; ConsoleWrite($aData[$i] & @CRLF)
        $nChar = Asc($aData[$i])
        Switch $nChar
            Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
                $sData &= $aData[$i]
            Case 32
                $sData &= "+"
            Case Else
                $sData &= "%" & Hex($nChar, 2)
        EndSwitch
    Next
    Return $sData
EndFunc   ;==>_URIEncode

Func _URIDecode($sData)
    ; Prog@ndy
    ; https://www.autoitscript.com/forum/topic/95850-url-encoding/?do=findComment&comment=689060
    Local $aData = StringSplit(StringReplace($sData, "+", " ", 0, 1), "%")
    $sData = ""
    For $i = 2 To $aData[0]
        $aData[1] &= Chr(Dec(StringLeft($aData[$i], 2))) & StringTrimLeft($aData[$i], 2)
    Next
    Return BinaryToString(StringToBinary($aData[1], 1), 4)
EndFunc   ;==>_URIDecode

 

;)

 

EDIT:
You can also look here:

http://www.w3schools.com/Tags/ref_urlencode.asp

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • 1 year later...
Posted (edited)

Sorry, wrong forum topic.

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Posted
  On 12/12/2016 at 2:11 PM, Terenz said:

The script work on my side:

$sString = "This is a test phrase. Translate me. I'll write al very long phrase for see if there is an error based on the text lenght but it doesn't on my side, i don't know. Well good luck, see you soon on Autoit thread!"
$sFrom = "en"
$sTo = "ru"
$sURL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" & $sFrom & "&tl=" & $sTo & "&dt=t&q=" & $sString

$oHTTP = ObjCreate("Microsoft.XMLHTTP")
$oHTTP.Open("GET", $sURL, False)
$oHTTP.Send()
$sData = $oHTTP.ResponseText
$sData = StringRegExpReplace($sData, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @CRLF)
MsgBox(0, 0, $sData)

29zeptv.png

Probably there is some mistake when you read and decode the file?

EDIT: Replace POST with GET, better the second one.

Expand  

Very cool

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...