From 17c56f957e7a2eea961a2ac9c5472a92e3383cc4 Mon Sep 17 00:00:00 2001 From: Mikkel Munch Mortensen <3xm@detfalskested.dk> Date: Tue, 6 Jun 2023 13:54:39 +0200 Subject: [PATCH] Wrap translation call in try-except --- frokostbot.py | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/frokostbot.py b/frokostbot.py index 6722080..e5faf59 100644 --- a/frokostbot.py +++ b/frokostbot.py @@ -31,10 +31,15 @@ except KeyError as e: sys.exit(1) sys.stdout.write("Configuration loaded.\n") + def get_photo_url(menu: str): """Retrieve a photo of the menu.""" # 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") # Ask Crayion to be creative. @@ -97,18 +102,14 @@ def get_photo_url(menu: str): # Set up and execute request to SharePoint. -cookies = browser_cookie3.firefox( - domain_name="sharepoint.com" -) +cookies = browser_cookie3.firefox(domain_name="sharepoint.com") headers = { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", } request_data = { - "parameters": { - "__metadata": { - "type": "SP.RenderListDataParameters" - }, + "parameters": { + "__metadata": {"type": "SP.RenderListDataParameters"}, "AddRequiredFields": True, "AllowMultipleValueFilterForTaxonomyFields": True, "FilterOutChannelFoldersInDefaultDocLib": True, @@ -126,9 +127,9 @@ sys.stdout.write("Data retrieved from SharePoint.\n") if response.status_code != 200: hook_url = SLACK_ERROR_HOOK photo = None - message = plain_message = ( - f"SharePoint responded with status {response.status_code} :(" - ) + message = ( + plain_message + ) = f"SharePoint responded with status {response.status_code} :(" else: # Extract today's menu from the SharePoint data. today = datetime.date.today().strftime("%d-%m-%Y") @@ -190,6 +191,7 @@ else: "cloud": ["sky"], "potato": ["kartoffel", "kartofler"], "apple": ["æble"], + "peanuts": ["peanut"], "baguette_bread": ["flute"], "flag-dk": ["tillykke", "fødselsdag"], "wave": ["farvel"], @@ -207,13 +209,15 @@ else: sys.stdout.write("No emojis determined.\n") # Pick an introduction for the menu. - introduction = random.choice([ - "Dagens menu er", - "I dag forkæler kantinen os med", - "Du kan godt glæde dig til senere! For vi skal have", - "Der bliver knoklet i køkkenet for at blive klar til at servere", - "Klokken 11:30 har kantinen fremtryllet en lækker omgang", - ]) + introduction = random.choice( + [ + "Dagens menu er", + "I dag forkæler kantinen os med", + "Du kan godt glæde dig til senere! For vi skal have", + "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") # Retrieve a photo of the menu from Craiyon. @@ -249,7 +253,7 @@ if photo is not None: "type": "plain_text", "text": photo_text, }, - } + }, ) sys.stdout.write("Posting menu to Slack...\n")