Skip to content

Commit

Permalink
refactor translation to use an object so that if authorization fails,…
Browse files Browse the repository at this point in the history
… it gets logged instead of being fatal
  • Loading branch information
JoeGermuska committed Aug 29, 2023
1 parent b7da90a commit 54eefc2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions NHC.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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+"
Expand Down
4 changes: 2 additions & 2 deletions NWS.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]
Expand Down
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 29 additions & 23 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down

0 comments on commit 54eefc2

Please sign in to comment.