DeepL Using Translation from Python (Part 2)

This article is the second in a series of articles on how to use the DeepL translation API and file translation. (The first article can be found here. )
*Rudimentary knowledge of Python and Linux is assumed.

Translating text files

Now that we have warmed up, let's try file translation.
This one requires a combination of three API calls.
Prepare a text file test.txt containing appropriate Japanese.
We will consider a mechanism to translate it into English (U.S.) and save it as test.trans.txt.

Step 1: Upload source language file

When you submit a translation request to the server at DeepL, you specify the source language file.

$ curl https://api.deepl.com/v2/document \
$ -F file=@test.txt \
$ -F auth_key=${auth_key} \
$ -F target_lang=en-us

 

The document_id and document_key of the file for which the translation request was accepted will be returned from the server at
still in json format.
The process up to the submission can be written in Python as follows:

url = 'https://api.deepl.com/v2/document'
files = dict()
files['file'] = open(fn, 'rb')
files['auth_key'] = (None, get_key())
files['target_lang '] = (None, 'en-us')
res = requests.post(url, files=files)

 

The auth_key and target_lang specifications are quite tricky.
There are several ways to specify items in files. In the case of a binary tuple,
one item is a file name and two items are objects.
If you want to specify a string, you can write it this way.
Another way is to store auth_key and target_lang in the data (dict format) as in
first string translation, and pass both data and files to
requests.post().

Step 2: Obtain translation process status

Requests the translation status of the file being translated:

$ document_id=[documentID]
$ document_key=[documentKey]
$ curl https://api.deepl.com/v2/document/${document_id} \
$ -d auth_key=${auth_key}
$ -d document_key=${document_key}

 

There are four statuses: queued (accepted), translating,
error (translation error occurred), and done (translation finished).
In the case of translating, an estimate of the remaining processing time is also returned.
Assuming that the document_id and document_key variables have been assigned, the process up to the
transmission can be written in Python as follows:

url = f'https://api.deepl.com/v2/document/{document_id}'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key
res = requests.post(url, data=data)

 

Step 3: Download target language file

When the translation status becomes " done ," the file is downloaded:

$ curl https://api.deepl.com/v2/document/${document_id}/result \
$ -d auth_key=${auth_key} \
$ -d document_key=${document_key} > test.trans .txt

 

Download the file only once.
The translation result is redirected (saved) to test.trans.txt.
The process up to the transmission can be written in Python as follows:

url = f'https://api.deepl.com/v2/document/{document_id}/result'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_ key
res = requests.post(url, data=data)

 

File translation process

Finally, the above processes are connected.
Since step 2 only returns the progress of the translation,
we need to wait for the result. Basically:
・When translating , sleep for
seconds of remaining time seconds_remaining and check the progress again.
If it is done or an error occurs, the loop is exited.
The above process can be written in Python as follows:

import requests
import json
from time import sleep

def get_key():
return open('key.txt').read().rstrip()

def upload_src(fn):
"'
file translation step 1: upload source language files
"'
url = 'https://api.deepl.com/v2/document'
files = dict()
files['file'] = open(fn, 'rb')
files['auth_key'] = (None, get_key())
files['target_lang&# 8217;] = (None, 'en-us')

res = requests.post(url, files=files)
res_status = res.status_code # 200 if successful (not used now)
res_text = res.text
res_data = json.loads(res_text)
document _id = res_data['document_id']
document_key = res_data['document_key']
return document_id, document_key

def get_trans_status(document_id, document_key):
"'
sub for step 2: Get translation processing status
"'
url = f'https://api.deepl.com/v2/ document/{document_id}'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key

res = requests.post(url, data=data)
res_status = res.status_code # 200 if successful (not used now)
res_text = res.text
res_data = json.loads(res_text)
return res_ data

def check_proceeding(document_id, document_key):
"'
file translation step 2: waiting for translation process
"'
while True:
res = get_trans_status( document_id, document_key)
status = res['status']
print(f'status: {status}', flush=True)
seconds_remaining = 0
if status == 'done' or status == 'error':
break
elif status == 'translating':
if 'seconds_remaining&# 8217; in res:
seconds_remaining = int(res['seconds_remaining'])
# avoid errors
if seconds_remaining <= 0:
seconds_remaining = 10
else: # queued etc
pass
print(f'...waiting for (another) {seconds_remaining}s', flush=True)
sleep(seconds_remaining)
return status

def download_tgt(fn, document_id, document_key):
"'
file translation step 3: download target language file
"'
url = f'https://api.deepl. com/v2/document/{document_id}/result'
data = dict()
data['auth_key'] = get_key()
data['document_key'] = document_key

res = requests.post(url, data=data)
res_status = res.status_code # 200 if successful (not used now)
tgt_bin = res._content
with open(fn, 'w', encoding ='utf-8′) as f:
print(tgt_bin.decode('utf-8'), end=", file=f)

def main():
fn_src = 'test.txt'
fn_tgt = fn_src.replace('.txt', '.trans.txt')

print(f'fn_src: {fn_src}')
print(f'fn_tgt: {fn_tgt}')

print(f'uploading: {fn_src}')
document_id, document_key = upload_src(fn_src)
status = check_proceeding(document_id, document_ key)
if status == 'done':
print(f'downloading: {fn_tgt}')
download_tgt(fn_tgt, document_id, document_key)

if __name__ == '__main__':
main()

 

Although the three API calls are similar,
I have not summarized them and written them in a verbose way because of the theme of "writing curl commands in Python"
.

That's all for this article:

The API of DeepL was used to translate the file.
The contents of the curl command were expressed in Python and programmed.

Thank you for reading.