Document Translation
import requests
import datetime
import time
# Constants for URLs and content type
BASE_URL = "https://doctranslate-api.doctranslate.io"
TRANSLATE_DOCUMENT_URL = f"{BASE_URL}/v2/translate/document"
GET_RESULT_ENDPOINT = f"{BASE_URL}/v1/result/"
CONTENT_TYPE = 'application/docx'
# Translation settings
original_language = None # Automatically detect language if not specified
destination_language = 'ja'
process_mode = 'append'
translate_type = 'Professional'
publish_api = True
# File paths
input_file_path = '/data/example.docx' # Input file path
output_folder_path = '/data/translated/' # Output folder path
# Authentication token
auth_token = "<YOUR_API_TOKEN>"
# Corrected variable names according to their declaration at the top
form_data = {
'file_type': CONTENT_TYPE,
'original_lang': original_language,
'dest_lang': destination_language,
'process_mode': process_mode,
'translate_type': translate_type,
'bilingual_text_style__font': 'Helvetica',
'bilingual_text_style__color': '#96d35f'
}
headers = {'Authorization': f'Bearer {auth_token}'}
# Corrected file upload part to match the expected key 'file' in the API
with open(input_file_path, 'rb') as file_to_translate:
try:
response = requests.post(TRANSLATE_DOCUMENT_URL, data=form_data, files={'file': file_to_translate}, headers=headers)
response.raise_for_status() # Proper error handling
response_data = response.json()
task_id = response_data.get('data', {}).get('task_id')
if task_id:
print(f'Task ID: {task_id}')
else:
print('Failed to get the task ID from the response.')
except requests.exceptions.RequestException as e:
print(f'An error occurred: {e}')
# Check for task_id before proceeding
if 'task_id' not in locals():
print('Error: missing task_id')
exit()
start_time = datetime.datetime.now()
url_download = ''
while True:
if (datetime.datetime.now() - start_time).seconds >= 600: # 10 minutes timeout
print('Error: Processing timed out.')
break
try:
response = requests.get(GET_RESULT_ENDPOINT + task_id, headers=headers) # Use GET and append task_id to URL
response_data = response.json()
if response_data.get('status') == 'success' and response_data.get('data', {}).get('status') == 'done':
url_download = response_data['data']['url_download']
print(f'URL Download: {url_download}')
break
except requests.exceptions.RequestException as e:
print(f'An error occurred while checking task status: {e}')
break
time.sleep(5) # Wait before the next check to avoid flooding the server
if not url_download:
print('Error: missing URL for download.')
Last updated