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
Translating with Deepl API
Re: Translating with Deepl API
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
Russ
Re: Translating with Deepl API
Thanks, I didn't found anything useful.
I try to create this script, based on Airtable http request. Is It correct?
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
Re: Translating with Deepl API
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
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
Re: Translating with Deepl API
Why? Last my post in this topic with link to deepl hack should work.Thanks, I didn't found anything useful.
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/
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
This is because You dont generate needed timestamps
Also here is written about this issue.
https://github.com/ptrstn/deepl-translate/issues/16
Need add some sleep between requests.
Code: Select all
generate_timestamp(sentences) {
Return A_TickCount ; not implemented yet
}
https://github.com/ptrstn/deepl-translate/issues/16
Need add some sleep between requests.
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
For me an error rises after the first request. Tryed also using VPN.
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
What do you mean? generate_id() ? For me no luck.
Re: Translating with Deepl API
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)
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
Timestamp is used after split_into_sentences() call.
Re: Translating with Deepl API
Python code
I do not see where You generate valid timestamps.
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)
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
split_into_sentences() doesn't use a timestamp, as I said.
Re: Translating with Deepl API
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
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
Clear, cookies, cache, change ip.
I see
1) https://www.deepl.com/PHP/backend/privacy.php?request_type=jsonrpc&il=EN&method=enableDataUse
2) https://w.deepl.com/web?request_type=jsonrpc&il=EN&method=getClientState
3) https://www2.deepl.com/jsonrpc?method=LMT_split_text
4) https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs ; with generated timestamp
May be deepl developers have changed "LMT_split_into_sentences" to "LMT_split_text" method.
I see
1) https://www.deepl.com/PHP/backend/privacy.php?request_type=jsonrpc&il=EN&method=enableDataUse
2) https://w.deepl.com/web?request_type=jsonrpc&il=EN&method=getClientState
3) https://www2.deepl.com/jsonrpc?method=LMT_split_text
4) https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs ; with generated timestamp
May be deepl developers have changed "LMT_split_into_sentences" to "LMT_split_text" method.
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
Did it, still see https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs as the first request.
-
- Posts: 4400
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Translating with Deepl API
I meant requests after inserting text into the field, but if i reload the page, requests look like you showed.
Re: Translating with Deepl API
Should work something like this.
https://www.sobyte.net/post/2022-04/deepl-api/
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
}
Who is online
Users browsing this forum: Bing [Bot], CoffeeChaton, JensHaglof, madensuyu1, mikeyww, newbieforever and 112 guests