Wrap translation call in try-except

This commit is contained in:
Mikkel Munch Mortensen 2023-06-06 13:54:39 +02:00
parent a83382ded8
commit 17c56f957e

View file

@ -31,10 +31,15 @@ except KeyError as e:
sys.exit(1) sys.exit(1)
sys.stdout.write("Configuration loaded.\n") sys.stdout.write("Configuration loaded.\n")
def get_photo_url(menu: str): def get_photo_url(menu: str):
"""Retrieve a photo of the menu.""" """Retrieve a photo of the menu."""
# Translate menu from Danish to English. # Translate menu from Danish to English.
translation = Translator().translate(menu, src="da", dest="en").text try:
translation = Translator().translate(menu, src="da", dest="en").text
except TypeError:
sys.stderr.write("Unable to translate menu :(\n")
return None
sys.stdout.write(f"English translation of the menu is: {translation}\n") sys.stdout.write(f"English translation of the menu is: {translation}\n")
# Ask Crayion to be creative. # Ask Crayion to be creative.
@ -97,18 +102,14 @@ def get_photo_url(menu: str):
# Set up and execute request to SharePoint. # Set up and execute request to SharePoint.
cookies = browser_cookie3.firefox( cookies = browser_cookie3.firefox(domain_name="sharepoint.com")
domain_name="sharepoint.com"
)
headers = { headers = {
"Accept": "application/json;odata=verbose", "Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose",
} }
request_data = { request_data = {
"parameters": { "parameters": {
"__metadata": { "__metadata": {"type": "SP.RenderListDataParameters"},
"type": "SP.RenderListDataParameters"
},
"AddRequiredFields": True, "AddRequiredFields": True,
"AllowMultipleValueFilterForTaxonomyFields": True, "AllowMultipleValueFilterForTaxonomyFields": True,
"FilterOutChannelFoldersInDefaultDocLib": True, "FilterOutChannelFoldersInDefaultDocLib": True,
@ -126,9 +127,9 @@ sys.stdout.write("Data retrieved from SharePoint.\n")
if response.status_code != 200: if response.status_code != 200:
hook_url = SLACK_ERROR_HOOK hook_url = SLACK_ERROR_HOOK
photo = None photo = None
message = plain_message = ( message = (
f"SharePoint responded with status {response.status_code} :(" plain_message
) ) = f"SharePoint responded with status {response.status_code} :("
else: else:
# Extract today's menu from the SharePoint data. # Extract today's menu from the SharePoint data.
today = datetime.date.today().strftime("%d-%m-%Y") today = datetime.date.today().strftime("%d-%m-%Y")
@ -190,6 +191,7 @@ else:
"cloud": ["sky"], "cloud": ["sky"],
"potato": ["kartoffel", "kartofler"], "potato": ["kartoffel", "kartofler"],
"apple": ["æble"], "apple": ["æble"],
"peanuts": ["peanut"],
"baguette_bread": ["flute"], "baguette_bread": ["flute"],
"flag-dk": ["tillykke", "fødselsdag"], "flag-dk": ["tillykke", "fødselsdag"],
"wave": ["farvel"], "wave": ["farvel"],
@ -207,13 +209,15 @@ else:
sys.stdout.write("No emojis determined.\n") sys.stdout.write("No emojis determined.\n")
# Pick an introduction for the menu. # Pick an introduction for the menu.
introduction = random.choice([ introduction = random.choice(
"Dagens menu er", [
"I dag forkæler kantinen os med", "Dagens menu er",
"Du kan godt glæde dig til senere! For vi skal have", "I dag forkæler kantinen os med",
"Der bliver knoklet i køkkenet for at blive klar til at servere", "Du kan godt glæde dig til senere! For vi skal have",
"Klokken 11:30 har kantinen fremtryllet en lækker omgang", "Der bliver knoklet i køkkenet for at blive klar til at servere",
]) "Klokken 11:30 har kantinen fremtryllet en lækker omgang",
]
)
sys.stdout.write(f"Introduction picked: {introduction}\n") sys.stdout.write(f"Introduction picked: {introduction}\n")
# Retrieve a photo of the menu from Craiyon. # Retrieve a photo of the menu from Craiyon.
@ -249,7 +253,7 @@ if photo is not None:
"type": "plain_text", "type": "plain_text",
"text": photo_text, "text": photo_text,
}, },
} },
) )
sys.stdout.write("Posting menu to Slack...\n") sys.stdout.write("Posting menu to Slack...\n")