2023-03-03 13:44:57 +00:00
|
|
|
"""
|
|
|
|
Post today's menu to Slack.
|
|
|
|
|
|
|
|
This is done by parsing the JSON data from SharePoint.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import random
|
|
|
|
from urllib import error, parse, request
|
|
|
|
|
|
|
|
import browser_cookie3
|
2023-03-03 14:00:18 +00:00
|
|
|
from googletrans import Translator
|
2023-03-03 13:44:57 +00:00
|
|
|
import requests
|
2023-03-06 12:47:43 +00:00
|
|
|
from requests.exceptions import JSONDecodeError, ReadTimeout
|
2023-03-03 13:44:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Load configuration.
|
|
|
|
try:
|
|
|
|
IS_LIVE = bool(os.environ.get("IS_LIVE"))
|
|
|
|
SLACK_ERROR_HOOK = os.environ["SLACK_ERROR_HOOK"]
|
|
|
|
SLACK_SUCCESS_HOOK = (
|
|
|
|
os.environ["SLACK_SUCCESS_HOOK"] if IS_LIVE else SLACK_ERROR_HOOK
|
|
|
|
)
|
|
|
|
SHAREPOINT_URL = os.environ["SHAREPOINT_URL"]
|
|
|
|
MENU_URL = os.environ["MENU_URL"]
|
|
|
|
except KeyError as e:
|
|
|
|
sys.stderr.write(f"Unable to load configuration for {e}.\n")
|
|
|
|
sys.exit(1)
|
|
|
|
sys.stdout.write("Configuration loaded.\n")
|
|
|
|
|
2023-06-06 11:54:39 +00:00
|
|
|
|
2023-03-06 12:47:43 +00:00
|
|
|
def get_photo_url(menu: str):
|
|
|
|
"""Retrieve a photo of the menu."""
|
|
|
|
# Translate menu from Danish to English.
|
2023-06-06 11:54:39 +00:00
|
|
|
try:
|
|
|
|
translation = Translator().translate(menu, src="da", dest="en").text
|
|
|
|
except TypeError:
|
|
|
|
sys.stderr.write("Unable to translate menu :(\n")
|
|
|
|
return None
|
2023-03-06 12:47:43 +00:00
|
|
|
sys.stdout.write(f"English translation of the menu is: {translation}\n")
|
|
|
|
|
|
|
|
# Ask Crayion to be creative.
|
|
|
|
cookies = browser_cookie3.firefox(domain_name="craiyon.com")
|
|
|
|
cookie = None
|
|
|
|
for cookie in cookies:
|
|
|
|
if cookie.name == "supabase-auth-token":
|
|
|
|
cookie = parse.unquote(cookie.value)
|
|
|
|
break
|
|
|
|
if cookie is None:
|
|
|
|
sys.stderr.write("Unable to extract Craiyon cookie.\n")
|
|
|
|
sys.exit(1)
|
|
|
|
token = json.loads(cookie)[0]
|
|
|
|
headers = {
|
2023-03-22 09:33:49 +00:00
|
|
|
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0",
|
|
|
|
"Accept": "application/json",
|
|
|
|
"Accept-Language": "en-US,en;q=0.5",
|
|
|
|
"Accept-Encoding": "gzip, deflate, br",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Origin": "https://www.craiyon.com",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"Sec-Fetch-Dest": "empty",
|
|
|
|
"Sec-Fetch-Mode": "cors",
|
|
|
|
"Sec-Fetch-Site": "same-site",
|
|
|
|
"Sec-GPC": "1",
|
|
|
|
"Pragma": "no-cache",
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
"TE": "trailers",
|
2023-03-06 12:47:43 +00:00
|
|
|
}
|
2023-03-22 09:33:49 +00:00
|
|
|
|
2023-03-06 12:47:43 +00:00
|
|
|
request_data = {
|
2023-05-09 08:19:07 +00:00
|
|
|
"model": "photo",
|
|
|
|
"negative_prompt": "",
|
2023-03-06 12:47:43 +00:00
|
|
|
"prompt": translation,
|
|
|
|
"token": token,
|
2023-05-09 08:19:07 +00:00
|
|
|
"version": "35s5hfwn9n78gb06",
|
2023-03-06 12:47:43 +00:00
|
|
|
}
|
|
|
|
sys.stdout.write("Asking Craiyon to generate images for the menu...\n")
|
2023-05-09 08:19:07 +00:00
|
|
|
craiyon_start = datetime.datetime.now()
|
2023-03-06 12:47:43 +00:00
|
|
|
try:
|
|
|
|
response = requests.post(
|
2023-05-09 08:19:07 +00:00
|
|
|
"https://api.craiyon.com/v3",
|
2023-03-06 12:47:43 +00:00
|
|
|
headers=headers,
|
|
|
|
json=request_data,
|
|
|
|
timeout=90,
|
|
|
|
)
|
|
|
|
except ReadTimeout:
|
|
|
|
sys.stderr.write("Timeout while waiting for Craiyon :(\n")
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
image = random.choice(response.json()["images"])
|
|
|
|
except JSONDecodeError:
|
|
|
|
sys.stderr.write("Unable to parse JSON from Craiyon response:\n")
|
|
|
|
sys.stderr.write(f"{response.text}\n")
|
2023-08-04 07:33:01 +00:00
|
|
|
sys.stderr.write(f"Waited for {datetime.datetime.now() - craiyon_start}.")
|
2023-03-06 12:47:43 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
return (f"https://img.craiyon.com/{image}", translation)
|
|
|
|
|
2023-03-03 13:44:57 +00:00
|
|
|
|
|
|
|
# Set up and execute request to SharePoint.
|
2023-06-06 11:54:39 +00:00
|
|
|
cookies = browser_cookie3.firefox(domain_name="sharepoint.com")
|
2023-03-03 13:44:57 +00:00
|
|
|
headers = {
|
|
|
|
"Accept": "application/json;odata=verbose",
|
|
|
|
"Content-Type": "application/json;odata=verbose",
|
|
|
|
}
|
|
|
|
request_data = {
|
2023-06-06 11:54:39 +00:00
|
|
|
"parameters": {
|
|
|
|
"__metadata": {"type": "SP.RenderListDataParameters"},
|
2023-03-03 13:44:57 +00:00
|
|
|
"AddRequiredFields": True,
|
|
|
|
"AllowMultipleValueFilterForTaxonomyFields": True,
|
|
|
|
"FilterOutChannelFoldersInDefaultDocLib": True,
|
|
|
|
"RenderOptions": 5707271,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response = requests.post(
|
|
|
|
SHAREPOINT_URL,
|
|
|
|
headers=headers,
|
|
|
|
cookies=cookies,
|
|
|
|
json=request_data,
|
|
|
|
)
|
|
|
|
data = response.json()
|
|
|
|
sys.stdout.write("Data retrieved from SharePoint.\n")
|
2023-05-09 08:19:07 +00:00
|
|
|
if response.status_code != 200:
|
2023-03-03 13:44:57 +00:00
|
|
|
hook_url = SLACK_ERROR_HOOK
|
2023-05-09 08:19:07 +00:00
|
|
|
photo = None
|
2023-06-06 11:54:39 +00:00
|
|
|
message = (
|
|
|
|
plain_message
|
|
|
|
) = f"SharePoint responded with status {response.status_code} :("
|
2023-03-03 13:44:57 +00:00
|
|
|
else:
|
2023-05-09 08:19:07 +00:00
|
|
|
# Extract today's menu from the SharePoint data.
|
|
|
|
today = datetime.date.today().strftime("%d-%m-%Y")
|
|
|
|
menu = None
|
|
|
|
if "error" in data:
|
|
|
|
message = plain_message = f"Error: {data['error']['message']['value']}"
|
|
|
|
photo = None
|
|
|
|
hook_url = SLACK_ERROR_HOOK
|
|
|
|
sys.stderr.write(f"SharePoint {message}\n")
|
2023-03-03 13:44:57 +00:00
|
|
|
else:
|
2023-05-09 08:19:07 +00:00
|
|
|
hook_url = SLACK_SUCCESS_HOOK
|
|
|
|
for entry in data["ListData"]["Row"]:
|
|
|
|
if entry.get("DAto") == today:
|
|
|
|
menu = entry
|
|
|
|
break
|
|
|
|
|
|
|
|
if menu is None:
|
|
|
|
sys.stderr.write("Unable to find the menu of today :(\n")
|
|
|
|
sys.exit(1)
|
2023-10-26 12:15:13 +00:00
|
|
|
menu = menu["Menutekst"].replace("\n", " ")
|
2023-05-09 08:19:07 +00:00
|
|
|
sys.stdout.write(f"The menu of today ({today}) is: {menu}\n")
|
|
|
|
|
|
|
|
# Determine appropriate emojis for the menu.
|
|
|
|
emojis = []
|
|
|
|
for emoji, keywords in {
|
2023-07-27 08:49:39 +00:00
|
|
|
"birthday": ["tillykke", "fødselsdag"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"flag-in": ["indisk", "indien"],
|
|
|
|
"flag-gr": ["græsk", "grækenland"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"flag-es": ["spansk", "spanien"],
|
2023-07-12 08:16:37 +00:00
|
|
|
"flag-kr": ["korea"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"flag-us": ["amerikansk"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"flag-th": ["thai"],
|
2023-07-04 08:06:03 +00:00
|
|
|
"hushed": ["surprise", "surprice"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"jack_o_lantern": ["halloween"],
|
|
|
|
"spider_web": ["halloween"],
|
|
|
|
"bat": ["halloween"],
|
|
|
|
"ghost": ["halloween"],
|
2023-07-27 08:49:39 +00:00
|
|
|
"building_construction": ["byg selv", "byg-selv", "bygselv"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"butter": ["smør", "butter"],
|
|
|
|
"fish": [
|
|
|
|
"fisk",
|
|
|
|
"laks","rødspætte",
|
|
|
|
"sej ",
|
|
|
|
"kulmule",
|
|
|
|
"brosme",
|
|
|
|
"kuller",
|
|
|
|
"multe",
|
|
|
|
"torsk",
|
|
|
|
],
|
2023-05-09 08:19:07 +00:00
|
|
|
"shrimp": ["reje"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"pig2": ["skinke", "gris", "nakkefilet", "nakke filet", "flæsk", "pork"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"cow": ["hakkebøf"],
|
|
|
|
"cow2": ["kalv", "okse"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"chicken": ["kylling", "chicken", "høns"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"turkey": ["kalkun"],
|
|
|
|
"rabbit2": [" hare"],
|
|
|
|
"rooster": ["coq au vin"],
|
|
|
|
"egg": [" æg ", " ægge"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"fried_egg": ["spejlæg"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"falafel": ["falafel"],
|
|
|
|
"hot_pepper": ["chili", "hot sauce"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"onion": [" løg"],
|
|
|
|
"carrot": ["gulerod", "gulerød"],
|
|
|
|
"garlic": ["hvidløg"],
|
2023-07-27 08:49:39 +00:00
|
|
|
"avocado": ["avocado", "avokado"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"mango": ["mango"],
|
2023-05-16 08:07:13 +00:00
|
|
|
"lemon": ["citron"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"mushroom": ["svampe", "kantarel", "champignon"],
|
|
|
|
"eggplant": ["moussaka", "mousakka"],
|
|
|
|
"cheese_wedge": [" ost"],
|
|
|
|
"beans": ["bønne"],
|
|
|
|
"olive": ["oliven"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"bell_pepper": ["peberfrugt"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"spaghetti": ["spaghetti"],
|
2023-07-04 08:06:03 +00:00
|
|
|
"pizza": ["pizza"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"hamburger": ["burger"],
|
|
|
|
"sandwich": ["sandwich"],
|
|
|
|
"stuffed_flatbread": ["pita"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"flatbread": ["naanbrød", " naan", "fladbrød"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"pie": ["tærte"],
|
2023-07-12 08:16:37 +00:00
|
|
|
"hotdog": ["hotdog", "pølse"],
|
|
|
|
"fries": ["fritter "],
|
2023-05-09 08:19:07 +00:00
|
|
|
"wine_glass": ["coq au vin"],
|
|
|
|
"bowl_with_spoon": ["suppe"],
|
2023-07-12 08:16:37 +00:00
|
|
|
"fire": ["grill", "brændt", "bbq"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"stew": ["gryde", "gullasch"],
|
|
|
|
"rice": [" ris "],
|
|
|
|
"ramen": ["nudler"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"burrito": ["tortilla"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"cloud": ["sky"],
|
|
|
|
"potato": ["kartoffel", "kartofler"],
|
2023-07-04 08:06:03 +00:00
|
|
|
"tomato": ["tomat"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"apple": ["æble"],
|
2023-10-26 12:15:13 +00:00
|
|
|
"beer": ["øl ", " øl"],
|
2023-06-06 11:54:39 +00:00
|
|
|
"peanuts": ["peanut"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"baguette_bread": ["flute"],
|
2023-09-18 07:47:43 +00:00
|
|
|
"green_salad": ["mixed salat"],
|
2023-08-04 07:33:01 +00:00
|
|
|
"the_horns": ["diablo"],
|
2023-05-09 08:19:07 +00:00
|
|
|
"wave": ["farvel"],
|
|
|
|
"gift_heart": ["valentines"],
|
2023-07-27 08:49:39 +00:00
|
|
|
"stars::fish::shrimp::lemon": ["stjerneskud"],
|
2023-05-09 08:19:07 +00:00
|
|
|
}.items():
|
|
|
|
for keyword in keywords:
|
|
|
|
if keyword in f" {menu.lower()} ":
|
|
|
|
emojis.append(emoji)
|
|
|
|
break
|
|
|
|
if emojis:
|
|
|
|
emojis = f":{': :'.join(emojis)}:"
|
|
|
|
sys.stdout.write(f"Emojis determined: {emojis}\n")
|
|
|
|
else:
|
2023-08-04 07:33:01 +00:00
|
|
|
emojis = random.choice([":cook:", ":knife_fork_plate:"])
|
|
|
|
sys.stdout.write("No emojis determined. Using a fallback emoji.\n")
|
2023-05-09 08:19:07 +00:00
|
|
|
|
|
|
|
# Pick an introduction for the menu.
|
2023-07-12 08:16:37 +00:00
|
|
|
tokens = {
|
|
|
|
"persons": random.choice([
|
|
|
|
"Abrahims",
|
|
|
|
"Elias'",
|
|
|
|
"Martins",
|
|
|
|
"Benjamins",
|
|
|
|
"Karstens",
|
|
|
|
"Josephines",
|
|
|
|
"Christians",
|
|
|
|
"Davids",
|
|
|
|
"Pias",
|
|
|
|
"Minas",
|
|
|
|
"Mariannes",
|
|
|
|
"Alexanders",
|
|
|
|
"Mettes",
|
|
|
|
"Jellings",
|
|
|
|
"Imers",
|
|
|
|
"Djinnies",
|
2023-07-27 08:49:39 +00:00
|
|
|
"Stefans",
|
|
|
|
"Pernilles",
|
2023-07-12 08:16:37 +00:00
|
|
|
]),
|
2023-07-12 08:27:07 +00:00
|
|
|
"weekday" : [
|
|
|
|
"mandag",
|
|
|
|
"tirsdag",
|
|
|
|
"onsdag",
|
|
|
|
"torsdag",
|
|
|
|
"fredag",
|
|
|
|
"lørdag",
|
|
|
|
"søndag",
|
|
|
|
][datetime.datetime.now().weekday()],
|
2023-07-12 08:16:37 +00:00
|
|
|
}
|
2023-06-06 11:54:39 +00:00
|
|
|
introduction = random.choice(
|
|
|
|
[
|
2023-07-12 08:27:07 +00:00
|
|
|
"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:00 har kantinen fremtryllet en lækker omgang",
|
|
|
|
"i dag skal vi have {persons} livret:",
|
2023-06-06 11:54:39 +00:00
|
|
|
]
|
|
|
|
)
|
2023-07-12 08:27:07 +00:00
|
|
|
if random.randint(0, 2) == 0:
|
2023-08-04 07:33:01 +00:00
|
|
|
introduction = (
|
|
|
|
random.choice(["Det er", "Så blev det"])
|
|
|
|
+ " {weekday} og "
|
|
|
|
+ introduction
|
|
|
|
)
|
2023-07-12 08:27:07 +00:00
|
|
|
else:
|
|
|
|
introduction = introduction.capitalize()
|
2023-07-12 08:16:37 +00:00
|
|
|
introduction = introduction.format(**tokens)
|
2023-05-09 08:19:07 +00:00
|
|
|
sys.stdout.write(f"Introduction picked: {introduction}\n")
|
|
|
|
|
|
|
|
# Retrieve a photo of the menu from Craiyon.
|
|
|
|
photo = get_photo_url(menu)
|
|
|
|
|
|
|
|
# Compose message for Slack.
|
|
|
|
plain_message = f"{introduction} {menu}"
|
|
|
|
menu_url = MENU_URL
|
|
|
|
message = f"{introduction} {emojis} <{menu_url}|*{menu}*>".strip()
|
2023-03-03 13:44:57 +00:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
"text": plain_message,
|
|
|
|
"blocks": [
|
|
|
|
{
|
|
|
|
"type": "section",
|
|
|
|
"text": {
|
|
|
|
"type": "mrkdwn",
|
|
|
|
"text": message,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2023-03-06 12:47:43 +00:00
|
|
|
if photo is not None:
|
2023-05-09 08:19:07 +00:00
|
|
|
photo_text = "Et ukvalificeret bud på hvordan dagens menu kunne se ud"
|
|
|
|
payload["blocks"].insert(
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"type": "image",
|
|
|
|
"image_url": photo[0],
|
|
|
|
"alt_text": photo_text,
|
|
|
|
"title": {
|
|
|
|
"type": "plain_text",
|
|
|
|
"text": photo_text,
|
|
|
|
},
|
2023-06-06 11:54:39 +00:00
|
|
|
},
|
2023-05-09 08:19:07 +00:00
|
|
|
)
|
2023-03-06 12:47:43 +00:00
|
|
|
|
2023-03-03 13:44:57 +00:00
|
|
|
sys.stdout.write("Posting menu to Slack...\n")
|
|
|
|
hook = request.Request(
|
|
|
|
hook_url,
|
|
|
|
data=json.dumps(payload).encode("utf-8"),
|
|
|
|
headers={
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method="POST",
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
response = request.urlopen(hook)
|
|
|
|
except error.HTTPError as e:
|
|
|
|
sys.stderr.write(f"{e}\n")
|
|
|
|
sys.exit(1)
|
|
|
|
sys.stdout.write("Done :)\n")
|