Presentation Maker
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}/v1/summarization"
GET_RESULT_ENDPOINT = f"{BASE_URL}/v1/result/"
# summarization settings
FILE_TYPE = "application/docx"
DEST_LANG = 'ja'
SLIDES_NUMBER = "10"
TEMPLATE_PATH="system/Template_2.pptx"
# File paths
FILE_PATH = '/path/to/your/file.docx' # Input file path
OUTPUT_FOLDER = '/path/to/output/folder/' # Output folder path
# Authentication token
auth_token = "<YOUR_API_TOKEN>"
# Corrected variable names according to their declaration at the top
form_data = {
'file_type': FILE_TYPE,
'dest_lang': DEST_LANG,
'slides_number': SLIDES_NUMBER,
'template_path': TEMPLATE_PATH
}
headers = {'Authorization': f'Bearer {auth_token}'}
# Corrected file upload part to match the expected key 'file' in the API
with open(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()
def download_file(url_download, output_folder):
response = requests.get(url_download)
filename = url_download.split('/')[-1].split('?')[0]
output_file_path = f"{output_folder}{filename}"
with open(output_file_path, 'wb') as file:
file.write(response.content)
return output_file_path
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('url_download'):
url_download = response_data['data']['url_download']
output_file = download_file(url_download, OUTPUT_FOLDER)
print(f'Downloaded file: {output_file}')
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