import os

input_path= "https://emilia-dataset.github.io/Emilia-Demo-Page/"
xpath_wav = "/html/body/div[1]/div/div/div[8]/div/div[2]/div/div[2]/div[1]/div[2]/div[1]/audio/source/@src"
language_xpath = "/html/body/div[1]/div/div/div[8]/div/div[2]/div/div[1]/text()"
l =              "/html/body/div[1]/div/div/div[8]/div/div[7]/div/div[1]/text()"
text_xpath = '/html/body/div[1]/div/div/div[8]/div/div[2]/div/div[2]/div[1]/div[1]/div/text()'
t =          '/html/body/div[1]/div/div/div[8]/div/div[7]/div/div[2]/div[1]/div[1]/text()'
from gxl_ai_utils.utils import utils_file, utils_spider
html_text_str = utils_spider.get_source_page_from_url(input_path)
utils_spider.write_to_html(html_text_str[0], 'text.html')
html_text_str = utils_spider.load_from_html('./text.html')

# for i in range(2,8):
#     """"""
#     output_dir = './data/emilia_demo'
#     output_dir_1 = os.path.join(output_dir, "demo_1")
#     os.makedirs(output_dir_1, exist_ok=True)
#     utils_file.write_list_to_file(['Samples generated by models trained with the full Emilia (101k hours) multilingual dataset.'], os.path.join(output_dir_1, f'title.txt'))
#
#     language_xpath = f"/html/body/div[1]/div/div/div[8]/div/div[{i}]/div/div[1]/text()"
#     language_i = utils_spider.handle_xpath(html_text_str, language_xpath)
#     print(language_i)
#
#     text_xpath = f'/html/body/div[1]/div/div/div[8]/div/div[{i}]/div/div[2]/div[1]/div[1]/div/text()'
#     text_i = utils_spider.handle_xpath(html_text_str, text_xpath)
#     print(text_i)
#     language_dir_i = os.path.join(output_dir_1, f'{language_i[0]}')
#     os.makedirs(language_dir_i, exist_ok=True)
#     utils_file.write_list_to_file([text_i[0]], os.path.join(language_dir_i, f'Target_Text:.txt'))
#     for j in range(1,4):
#         wav_xpath = f'/html/body/div[1]/div/div/div[8]/div/div[{i}]/div/div[2]/div[1]/div[2]/div[{j}]/audio/source/@src'
#         wav_i = utils_spider.handle_xpath(html_text_str, wav_xpath)
#         wav_path = f'https://emilia-dataset.github.io/Emilia-Demo-Page/{wav_i[0]}'
#         if j == 1:
#             file_name = "Speech_Prompt"
#         elif j == 2:
#             file_name = "SoundStorm"
#         else:
#             file_name = "VoiceBox"
#         utils_spider.download_file(wav_path, os.path.join(language_dir_i, f'{file_name}.wav'))

output_dir = "../data/emilia_demo/demo_2"
os.makedirs(output_dir, exist_ok=True)
for i in range(2,5):
    output_dir_i = os.path.join(output_dir, f"row_{i-1}")
    xpath_text = f"/html/body/div[1]/div/div/div[9]/div/div[{i}]/div[1]/div/text()"
    text = utils_spider.handle_xpath(html_text_str, xpath_text)
    utils_file.write_list_to_file([text[0]], os.path.join(output_dir_i, f'Target_Text.txt'))
    for j in range(1,6):
        wav_xpath = f"/html/body/div[1]/div/div/div[9]/div/div[{i}]/div[2]/div[{j}]/audio/source/@src"
        wav_i = utils_spider.handle_xpath(html_text_str, wav_xpath)
        wav_path = f'https://emilia-dataset.github.io/Emilia-Demo-Page/{wav_i[0]}'
        print(wav_path)
        if j == 1:
            filename = 'Speech_Prompt'
        elif j == 2:
            filename = 'SoundStorm-Emilia'
        elif j == 3:
            filename = 'SoundStorm-MLS'
        elif j == 4:
            filename = 'VoiceBox-Emilia'
        elif j == 5:
            filename = 'VoiceBox-MLS'
        utils_spider.download_file(wav_path, os.path.join(output_dir_i, f'{filename}.wav'))

