# public packages
from langdetect import detect
from google_trans_new import google_translator # had to download package to fix bug and be able to run code
# database-related
from database import Data_table, get_untranslated, insert_translated
from dotenv import load_dotenv
# own scripts
from clean_data import clean_content

def translate(text, target_lang='en'):
    try:
        input_lang = detect(text)
    except Exception as e:
        print(f'error when detecting language: {e}')
        return ''
    if input_lang == target_lang: return text
    translator = google_translator()
    try:
        text_trans = translator.translate(text, lang_src=input_lang, lang_tgt=target_lang)
    except Exception as e:
        print(f'error when translating text: {e}')
        return ''
    return text_trans

def continously_translate():
    with Data_table() as dt:
        print('began translating')
        while True: # keep going forever
            try:
                val_dict = get_untranslated(dt)
            except Exception as e:
                print(f'error when getting untranslated content from database: {e}')
                continue
            # if nothing left untranslated, stop
            if val_dict is None: break
            # clean text
            try:
                content_clean = clean_content(val_dict['content'])
            except Exception as e:
                print(f'error when cleaning content: {e}')
                continue
            # translate text
            try:
                content_en = translate(content_clean)
            except Exception as e:
                print(f'error when translating content: {e}')
                continue
            # upload to database
            val_dict['content_en'] = content_en
            try:
                insert_translated(dt, val_dict)
            except Exception as e:
                print(f'error when sending translated content to database: {e}')
                continue
            print('translated content')
        print('stopped translating')

if __name__ == '__main__':
    load_dotenv()
    continously_translate()
