import requests
from tenacity import retry, wait_fixed, stop_after_delay
api_url = 'http://api.conspecto.ru'
api_key = 'API_KEY_HERE'
headers = {
'Authorization': f'Bearer {api_key}'
}
@retry(wait=wait_fixed(20), stop=stop_after_delay(1800))
def check_order_status(api_url, job_id, headers):
response = requests.post(f'{api_url}/v1/status/{job_id}', headers=headers)
if response.status_code != 200:
raise Exception("Не удалось получить статус")
status = response.json().get('status')
if status != 'Completed':
raise Exception("Заказ еще не завершен")
return response.json()
def download_file(api_url, job_id, headers, file_type):
response = requests.get(f'{api_url}/v1/download_file/{job_id}/transcribe/{file_type}/', headers=headers)
if response.status_code == 200:
with open(f'{job_id}_transcribe.{file_type}', 'wb') as f:
f.write(response.content)
else:
print("Не удалось скачать файл")
return response
files = {'file': open('example_api.mp3', 'rb')}
data = {
'language': 'ru',
'order_type': 'transcribe',
'num_speakers': 2
}
response = requests.post(f'{api_url}/v1/start', headers=headers, files=files, data=data)
job_id = response.json().get('job_id')
if job_id:
try:
status_response = check_order_status(api_url, job_id, headers)
if status_response.get('status') == 'Completed':
download_response = download_file(api_url, job_id, headers, 'txt')
except Exception as e:
print(f"Ошибка: {e}")
else:
print("Не удалось создать заказ, job_id не получен.")