#!/usr/bin/env python3
_A='install'
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup as bs
from os import path
from sys import argv,exit
try:from bs4 import BeautifulSoup as bs
except:
	try:from pip._internal import main as pip;pip([_A,'--user','bs4']);from bs4 import BeautifulSoup as bs
	except:from pip._internal import main as pip;pip([_A,'bs4']);from bs4 import BeautifulSoup as bs
def get_note_kdenlive(name_file):'Get the note written in kdenlive file\n\n    Args:\n        name_file (path): Path from file kdenlive\n\n    Returns:\n        str: Content of note\n    ';A=ET.parse(name_file);B=A.getroot();C=B.findall('*/property[@name="kdenlive:documentnotes"]')[0].text;return C
def get_name_default(name_kdenlive):A=name_kdenlive.split(path.sep)[-1];A=A.replace('.kdenlive','');return f"{A}.description"
def save_note_html(content,path_save='assets/'):
	'Function to save the html got from kdenlive file \n\n    Args:\n        content (str): html note raw\n        path_save (str, optional): Path to save the file. Defaults to "assets/".\n    '
	with open(f"{path_save}/note.html",'+w',encoding='utf-8')as A:A.write(content)
def save_description(content,file_name='description'):
	with open(file_name,'+w',encoding='utf-8')as A:A.write(content)
	return True
def clear_time(line):
	'Clear the format default from kdenlive file to youtube format\n\n    Args:\n        line (str): Line from note\n\n    Returns:\n        str: New line with the format correct to youtube\n    ';B=':';A=line;A=A.split(',')[0]
	if A.startswith('00:')and len(A.split(B))>2:A=A.split(B)[1:-1];C=B.join(A)
	return C
def clear_line(line):A='';B=line.strip().split(' ')[0];C=clear_time(B);A=line.replace(B,C);return A
def get_times(html):
	C=bs(html,'html.parser');D=C.body;E=D.find_all('p');B=''
	for F in E:
		A=F.text.strip()
		if len(A)and A[0].isdigit():A=clear_line(A)
		B+=A+'\n'
	return B.strip()
def get_note(path_file,save=False):
	'Main function to export\n\n    Args:\n        path_file (str): Path file kdenlive\n\n    Returns:\n        str: All content formatted\n    ';A=path_file;C=get_note_kdenlive(A);B=get_times(C)
	if save:save_description(B,get_name_default(A))
	return B
def test():'Function to the the module\n    ';A='./assets/notas.kdenlive';B=get_note_kdenlive(A);C=get_times(B);save_description(C,get_name_default(A))
def cli():
	A='\n    Script to get note from file kdenlive a youtube time format\n    \n    HOW TO USE:\n    \n        getnotex file.kdenlive \n    '
	if len(argv)<2 or len(argv)>2:print(A);exit(1)
	B=argv[1]
	if B=='--help':print(A);exit(0)
	try:get_note(B,save=True)
	except Exception as C:print(C);print(A);exit(1)
	exit(0)
if __name__=='__main__':cli()