import requests
import time

URL = "https://api.aditu.eus/"
API_ID = "YOUR_API_ID"
API_KEY = "YOUR_API_KEY"
VIDEO = 'PATH_TO_FILE'


def transcribe_file_api(api_id, api_key, language, file, channel=2):
	print("Transcribing: %s" % file)
	PARAMS = {"api_id":api_id, "api_key": api_key, "language": language, "channel": channel}
	files = {'file': open(file,'rb')}
	r = requests.post(url = URL + "transcribe", data = PARAMS, files=files)
	if r.status_code != 200:
		print(r.content)
		print("There was an error: %d" % r.status_code)
		return None
	return r.json()

def get_file_api(api_id, api_key, task_id):
	PARAMS = {"api_id":api_id, "api_key": api_key, "task_id": task_id}
	r = requests.post(url = URL + "get_file", data = PARAMS)
	if r.status_code != 200:
		print(r.content)
		print("There was an error: %d" % r.status_code)
		return None
	return r.json()

def export_api(api_id, api_key, file_id, export_format, language='none', speaker_info='none'):
	print(f"Exporting {file_id}-{language} to {export_format} with speaker {speaker_info}")
	PARAMS = {"api_id":api_id, "api_key": api_key, "file_id": file_id, "language": language, "format": export_format, "speaker_info": speaker_info}
	r = requests.post(url = URL + "export", data = PARAMS)
	if r.status_code != 200:
		print(r.content)
		print("There was an error: %d" % r.status_code)
		return None
	return r.content

def get_user_time_info_api(api_id, api_key):
	PARAMS = {"api_id":api_id, "api_key": api_key}
	r = requests.post(url = URL + "time", data = PARAMS)
	if r.status_code != 200:
		print(r.content)
		print("There was an error: %d" % r.status_code)
		return None
	return r.json()

def get_user_space_info_api(api_id, api_key):
	PARAMS = {"api_id":api_id, "api_key": api_key}
	r = requests.post(url = URL + "space", data = PARAMS)
	if r.status_code != 200:
		print(r.content)
		print("There was an error: %d" % r.status_code)
		return None
	return r.json()


def transcribe_file(api_id, api_key, language, file, json, transcription, docx, docx2, subtitles, vtt_subtitles, stl_subtitles, channel=2):
	response = transcribe_file_api(api_id, api_key, language, file, channel)
	if not response:
		return False
	print("File succesfully uploaded to server")

	retries = 0
	while True:
		if retries == 5:
			return False
		time.sleep(2)
		result = get_file_api(api_id, api_key, response["task_id"])
		print (result)
		if result:
			if result["finished"] and not result["error"]:
				export_content=export_api(api_id, api_key, result["file_id"], "WORDS", 'none', 'none')
				open(json, 'wb').write(export_content)
				export_content=export_api(api_id, api_key, result["file_id"], "TXT", 'none', 'none')
				open(transcription, 'wb').write(export_content)
				export_content=export_api(api_id, api_key, result["file_id"], "DOCX", 'none', 'names')
				open(docx, 'wb').write(export_content)
				export_content=export_api(api_id, api_key, result["file_id"], "MIN", 'none', 'none')
				open(docx2, 'wb').write(export_content)
				export_content=export_api(api_id, api_key, result["file_id"], "SRT", 'none', 'hyphens')
				open(subtitles, 'wb').write(export_content)
				export_content=export_api(api_id, api_key, result["file_id"], "VTT", 'none', 'colors')
				open(vtt_subtitles, 'wb').write(export_content)
				export_content=export_api(api_id, api_key, result["file_id"], "STL", 'none', 'none')
				open(stl_subtitles, 'wb').write(export_content)
				return True
			elif result["finished"] and result["error"]:
				return False
			else:
				print("[ " + str(result["percentage"]) + "% ] " + result["message"])
		else:
			retries += 1




result = transcribe_file(API_ID, API_KEY, "eu", VIDEO, "proba.json", "proba.txt", "proba.docx", "proba2.docx", "proba.srt", "proba.vtt", "proba.stl")
if result:
	print("File succesfully transcribed")
else:
	print("There was an error")

print(get_user_time_info_api(API_ID, API_KEY))
