diff --git a/NHC.py b/NHC.py index 12fa3c3..c6f1598 100644 --- a/NHC.py +++ b/NHC.py @@ -1,7 +1,7 @@ import re from bs4 import BeautifulSoup -from util import get_translator_func,load_parsed_data , save_parsed_data,contains_area +from util import Translator,load_parsed_data, save_parsed_data,contains_area from jinja2 import Template import logging @@ -14,7 +14,7 @@ def clean_str(str): def get_tropical_bulletin(bulletin): PARSED_ID_FILE = 'NHCdata.json' - translate = get_translator_func() + translate = Translator() parsed_ids = load_parsed_data(PARSED_ID_FILE) separatorpattern = r"\.\s+" diff --git a/NWS.py b/NWS.py index 8e58d31..db80a7f 100644 --- a/NWS.py +++ b/NWS.py @@ -1,5 +1,5 @@ import json -from util import load_parsed_data, save_parsed_data, contains_area, get_translator_func +from util import load_parsed_data, save_parsed_data, contains_area, Translator from jinja2 import Template from datetime import datetime import requests @@ -31,7 +31,7 @@ def fetch_nws_data(): def get_weather_bulletin(bulletin): PARSED_ID_FILE = 'NWSdata.json' - translate = get_translator_func() + translate = Translator() data = [] areas = ["puerto rico", "vieques","culebra" , "pr"] diff --git a/main.py b/main.py index 0b8235a..38ccb28 100755 --- a/main.py +++ b/main.py @@ -70,10 +70,10 @@ def main_nws(testfile=None, actually_post_articles=False): # TODO: pass JSON instead of file generated = generate_nws_stories(filelocation) - - with open("nws_generated.json", 'w') as f: # temporary - json.dump(generated, f, indent=2) - logger.debug("wrote nws_generated.json") + if generated: + with open("nws_generated.json", 'w') as f: # temporary + json.dump(generated, f, indent=2) + logger.debug("wrote nws_generated.json") for story in generated: post_story(story["headline"], story["content"] , story.get('image_code'), actually_post_articles=actually_post_articles) diff --git a/util.py b/util.py index 55a863b..b16fb5c 100644 --- a/util.py +++ b/util.py @@ -71,29 +71,35 @@ def save_parsed_data(parsed_data: dict, filepath: str): with open(filepath, "w") as file: json.dump(parsed_data, file) -def get_translator_func(): - """Making this a factory function lets us see that the key isn't set without logging something - every single time translate is called. Before we did it when this file was compiled, which led to - log messages being lost because logging hadn't been configured yet.""" - try: - TRANSLATOR = deepl.Translator(os.environ['DEEPL_API_KEY']) - def translate(lines): - result = TRANSLATOR.translate_text( - lines, - source_lang="EN", - target_lang="ES", - formality="prefer_more", - split_sentences="nonewlines", - preserve_formatting=True ) # Corrected to a boolean value - - return result.text - return translate - except KeyError: - logger.warning("Missing DEEPL_API_KEY env variable. Translations will be a no-op") - def noop_translate(lines): - return lines - return noop_translate - +class Translator(object): + """A callable which manages translation with DeepL, if its configured, or just passes text through otherwise""" + auth_ok = False + translator = None + logger = logging.getLogger('Translator') + def __init__(self) -> None: + api_key = os.environ.get('DEEPL_API_KEY') + if api_key: + self.translator = deepl.Translator(api_key) + self.auth_ok = True + else: + self.logger.warning("Missing DEEPL_API_KEY env variable. Translations will be a no-op") + + def __call__(self, lines: str) -> str: + if self.translator and self.auth_ok: + try: + result = self.translator.translate_text( + lines, + source_lang="EN", + target_lang="ES", + formality="prefer_more", + split_sentences="nonewlines", + preserve_formatting=True ) # Corrected to a boolean value + return result.text + except Exception as e: + self.logger.error(f"Error using translation: {e}") + # TODO maybe send email? + self.auth_ok = False + return lines def walk_storms (dir) : for file in dir.glob("*.txt"):