added try except statements and logging

This commit is contained in:
simplypower-bbj
2022-12-18 19:36:46 +01:00
parent be529bf189
commit db4c31d75e
+30 -19
View File
@@ -5,10 +5,12 @@ import multiprocessing as mp
import numpy as np
from dotenv import load_dotenv
from tqdm import tqdm
import logging
from database import Data_table
from database import insert_translated
from clean_data import clean_content
from setup_logging import setup_logging
def translate(text, target_lang='en'):
try:
@@ -42,29 +44,38 @@ def get_untranslated():
return val_dict
def continously_translate():
with Data_table() as dt:
query = (
f'UPDATE {dt._name} '
' SET content_en = (%(content_en)s) '
'WHERE id = (%(id)s) '
)
while True:
logging.info('began translating')
while True:
try:
val_dict = get_untranslated()
if val_dict is None: break
# clean
except Exception as e:
logging.warning(f'error when getting untranslated content from database: {e}')
continue
if val_dict is None: break
# clean
try:
content_clean = clean_content(val_dict['content'])
# translate
try:
content_en = translate(content_clean)
except Exception as e:
content_en = ''
val_dict['content_en'] = content_en
# upload to database
dt.execute(query, val_dict)
print('translated content')
print('no more untranslated content located')
except Exception as e:
logging.warning(f'error when cleaning content: {e}')
continue
# translate
try:
content_en = translate(content_clean)
except Exception as e:
logging.warning(f'error when translating content: {e}')
continue
# upload to database
val_dict['content_en'] = content_en
try:
insert_translated(val_dict)
except Exception as e:
logging.warning(f'error when sending translated content to database: {e}')
continue
logging.info('translated content')
logging.info('no more untranslated content located')
if __name__ == '__main__':
load_dotenv()
setup_logging()
continously_translate()
# later try to set all empty strings back to null and retranslate failed...