Translating with Deepl API

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
enrz
Posts: 2
Joined: 18 Apr 2022, 11:19

Translating with Deepl API

Post by enrz » 18 Apr 2022, 11:26

Hi everyone!
I'm trying to create a script that translate with deepl.com a text I insert in an InputBox.
I want to use the Deepl API (free) and recive the translation in a msgbox,
The Http Request is this:

EXAMPLE REQUEST
POST /v2/translate?auth_key=[KEY]> HTTP/1.0
Host: api.deepl.com
User-Agent: YourApp
Accept: */*
Content-Length: [length]
Content-Type: application/x-www-form-urlencoded
auth_key=[KEY]&text=Hello, world&target_lang=DE

EXAMPLE RESPONSE
{
"translations": [{
"detected_source_language":"EN",
"text":"Hallo, Welt!"
}]
}


How Can I implement this in AHK? I think with WinHttp but it doesn't work.
Thanks

RussF
Posts: 1269
Joined: 05 Aug 2021, 06:36

Re: Translating with Deepl API

Post by RussF » 18 Apr 2022, 13:00

Much has already been written on this topic, one of which is this one. I suggest you do a forum search for "deepl" or "deepl.com" and see what is there rather than reinvent the wheel.

Russ

enrz
Posts: 2
Joined: 18 Apr 2022, 11:19

Re: Translating with Deepl API

Post by enrz » 19 Apr 2022, 11:28

Thanks, I didn't found anything useful.
I try to create this script, based on Airtable http request. Is It correct?

Code: Select all

!r::
InputBox, txt, Text to translate

Data := {"Host":"api-free.deepl.com", "User-Agent":"YourApp","Accept":" */*","Content-Length":"5","Content-Type":"application/x-www-form-urlencoded"}

WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttp.Open("POST", "https://api-free.deepl.com/v2/translate?auth_key=KEY" HTTP/1.0)
WinHttp.SetRequestHeader("Content-Type", "application/json")
WinHttp.SetRequestHeader("Authorization", "Bearer" "KEY&text="%txt%, "world&target_lang=EN")
WinHttp.Send(Data)
MsgBox % WinHttp.ResponseText

return

RussF
Posts: 1269
Joined: 05 Aug 2021, 06:36

Re: Translating with Deepl API

Post by RussF » 19 Apr 2022, 13:35

I'm sorry, I can't speak to your code. I adapted this script from @teadrinker (thank you, @teadrinker!) to make a Google-based translation utility for my wife who is a kindergarten teacher. She uses it to translate a weekly newsletter to the parents of her students, a few who are non-English-speaking.

I've heard that Google has a limit on the number of calls to this utility, but we have never run up against it. Deepl has published limits on its free version. I do remember a thread dealing with deepl banning users trying to hit it too much for free.

When I search the AHK forum with just the keyword "deepl", I get 110 hits. Have you investigated all of these?

Russ

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 19 Apr 2022, 17:39

Thanks, I didn't found anything useful.
Why? Last my post in this topic with link to deepl hack should work.
viewtopic.php?p=435542#p435542
Just need to translate it from python or use python.
Also if You are interested in reverse-engineering then You can read this article.
https://zu1k.com/posts/thinking/deception-tactics-in-deepl-api-design/

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Translating with Deepl API

Post by teadrinker » 19 Apr 2022, 18:47

malcev wrote: Last my post in this topic with link to deepl hack should work.
Tried, doesn't work. Error «Too many requests» after split_into_sentences(text, kwargs).
Spoiler

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 19 Apr 2022, 19:14

This is because You dont generate needed timestamps

Code: Select all

generate_timestamp(sentences) {
   Return A_TickCount ; not implemented yet
}
Also here is written about this issue.
https://github.com/ptrstn/deepl-translate/issues/16
Need add some sleep between requests.

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Translating with Deepl API

Post by teadrinker » 19 Apr 2022, 19:20

For me an error rises after the first request. Tryed also using VPN.


teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Translating with Deepl API

Post by teadrinker » 19 Apr 2022, 19:27

What do you mean? generate_id() ? For me no luck.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 19 Apr 2022, 19:32

I mean

Code: Select all

def calculate_valid_timestamp(timestamp, i_count):
    try:
        return timestamp + (i_count - timestamp % i_count)
    except ZeroDivisionError:
        return timestamp


def generate_timestamp(sentences):
    now = int(time.time() * 1000)
    i_count = 1
    for sentence in sentences:
        i_count += sentence.count("i")

    return calculate_valid_timestamp(now, i_count)

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Translating with Deepl API

Post by teadrinker » 19 Apr 2022, 19:34

Timestamp is used after split_into_sentences() call.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 19 Apr 2022, 19:40

Python code

Code: Select all

def generate_translation_request_data(
    source_language,
    target_language,
    sentences,
    identifier=MAGIC_NUMBER,
    alternatives=1,
    formality_tone=None,
):
    return {
        "jsonrpc": "2.0",
        "method": "LMT_handle_jobs",
        "params": {
            "jobs": generate_jobs(sentences, beams=alternatives),
            "lang": {
                "user_preferred_langs": [target_language, source_language],
                "source_lang_computed": source_language,
                "target_lang": target_language,
            },
            "priority": 1,
            "commonJobParams": generate_common_job_params(formality_tone),
            "timestamp": generate_timestamp(sentences),
        },
        "id": identifier,
    }
    
def calculate_valid_timestamp(timestamp, i_count):
    try:
        return timestamp + (i_count - timestamp % i_count)
    except ZeroDivisionError:
        return timestamp


def generate_timestamp(sentences):
    now = int(time.time() * 1000)
    i_count = 1
    for sentence in sentences:
        i_count += sentence.count("i")

    return calculate_valid_timestamp(now, i_count)
I do not see where You generate valid timestamps.

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Translating with Deepl API

Post by teadrinker » 19 Apr 2022, 19:46

split_into_sentences() doesn't use a timestamp, as I said.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 19 Apr 2022, 21:19

If You run deepl from browser, then the very first post request will be https://w.deepl.com/web?request_type=jsonrpc&il=EN&method=getClientState


malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 21 Apr 2022, 03:46



teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Translating with Deepl API

Post by teadrinker » 21 Apr 2022, 07:57

I meant requests after inserting text into the field, but if i reload the page, requests look like you showed.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Translating with Deepl API

Post by malcev » 21 Apr 2022, 19:25

Should work something like this.

Code: Select all

StringCaseSense, On
text := "Hello, World! "
id := 12340000
WinHTTP := ComObjCreate("WinHttp.WinHttpRequest.5.1")
loop
{
   WinHTTP.Open("POST", "https://www2.deepl.com/jsonrpc", true)
   WinHTTP.SetRequestHeader("content-type", "application/json")
   WinHTTP.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.75")
   id++
   if (Mod(id+3, 13) = 0) or (Mod(id+5, 29) = 0)
      method = "method" :
   else
      method = "method":
   text .= "i"
   StrReplace(text, "i",, i_count)
   i_count++
   timestamp := A_NowUTC
   timestamp -= 19700101000000, S
   timestamp .= A_MSec
   timestamp := timestamp + i_count - Mod(timestamp, i_count)
   json = {"jsonrpc": "2.0", %method% "LMT_handle_texts", "id": %id%, "params": {"texts": [{"text": "%text%", "requestAlternatives": 3}], "splitting": "newlines", "lang": {"target_lang": "DE"}, "timestamp": %timestamp%, "commonJobParams": {"wasSpoken": false}}}
   WinHTTP.Send(json)
   WinHTTP.WaitForResponse()
   msgbox % WinHTTP.responsetext
}
https://www.sobyte.net/post/2022-04/deepl-api/

Post Reply

Return to “Ask for Help (v1)”