-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.py
252 lines (213 loc) · 7.6 KB
/
menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
"""Foodstoffi Menu Scraper"""
from __future__ import annotations
__copyright__ = "Copyright (c) 2024 STAIR. All Rights Reserved."
__email__ = "[email protected]"
from dataclasses import dataclass, field
import re
import datetime
import logging
import aiohttp
import discord
import bs4
from pyaddict import JDict, JList
from common.aioschedule import AioSchedule
from common.constants import STAIR_GREEN
from db.datamodels.announcement import AnnouncementType
from integration.discord.stan import Stan
from integration.discord.persona import PersonaSender, Persona
URL = "https://app.food2050.ch/de/foodstoffi/foodstoffi/menu/foodstoffi/weekly"
_PERSONA = Persona.get("Chef Stan-dwich", Persona.default())
_ANNOUNCEMENT_TYPE = AnnouncementType.get("canteen-menu", AnnouncementType.default())
@dataclass
class Recipe: # pylint: disable=too-many-instance-attributes
"""A recipe"""
typename: str
id: str
category: str
is_balanced: bool
title: str
climate_prediction: str
slug: str
is_vegan: bool
is_vegetarian: bool
_allergens: list[str] = field(default_factory=list)
@classmethod
def from_dict(cls, value: JDict, category: str) -> Recipe | None:
"""Create a recipe from a dictionary"""
if not value:
return None
if "Geschlossen" in value.ensure("title", str):
return None
return cls(
typename=value.ensure("__typename", str),
id=value.ensure("id", str),
category=category,
is_balanced=value.ensure("isBalanced", bool),
title=value.ensure("title", str),
climate_prediction=value.ensureCast("climatePrediction", JDict)
.ensure("rating", str)
.lower(),
slug=value.ensure("slug", str),
_allergens=value.ensure("allergens", str).split(","),
is_vegan=value.ensure("isVegan", bool),
is_vegetarian=value.ensure("isVegetarian", bool),
)
@property
def allergens(self) -> list[str]:
"""Get the allergens"""
return [
re.sub(r"(?<=[a-z])(?=[A-Z])", " ", allergen).title()
for allergen in self._allergens
if allergen != ""
]
@property
def as_embed(self) -> discord.Embed:
"""Get the recipe as an embed"""
embed = discord.Embed(
title=self.category,
url=f"https://app.food2050.ch/de/foodstoffi/foodstoffi/food-profile/{self.slug}",
description=self.title,
color=STAIR_GREEN,
)
tags: list[str] = []
if self.is_vegan:
tags.append("🌱 Vegan")
elif self.is_vegetarian:
tags.append("🌿 Vegetarian")
if self.is_balanced:
tags.append("⚖️ Balanced")
if self.climate_prediction:
tags.append(f" 🌍 {self.climate_prediction.title()} Climate Impact")
if self.allergens:
tags.append(f"🚫 {', '.join(self.allergens)}")
embed.set_footer(text=" | ".join(tags))
return embed
@dataclass
class RecipeItem:
"""A recipe item"""
typename: str
date: datetime.date
recipe: Recipe | None
@classmethod
def from_dict(cls, value: JDict, category: str) -> RecipeItem | None:
"""Create a recipe item from a dictionary"""
item = cls(
typename=value.ensure("__typename", str),
date=datetime.datetime.fromisoformat(value.ensure("date", str)).date()
+ datetime.timedelta(days=1),
recipe=Recipe.from_dict(value.ensureCast("recipe", JDict), category),
)
if not item.recipe:
return None
return item
@dataclass
class Category:
"""A category of recipes"""
typename: str
id: str
name: str
recipes: list[RecipeItem]
@classmethod
def from_dict(cls, value: JDict) -> Category:
"""Create a category from a dictionary"""
name = value.ensure("displayName", str)
recipes = [
RecipeItem.from_dict(recipe, name)
for recipe in value.ensureCast("dailyRecipies", JList)
.iterator()
.ensureCast(JDict)
]
recipes = [recipe for recipe in recipes if recipe]
return cls(
typename=value.ensure("__typename", str),
id=value.ensure("id", str),
name=name,
recipes=recipes, # type: ignore
)
@property
def todays_recipe(self) -> Recipe | None:
"""Get the recipe for today"""
today = datetime.date.today()
for recipe in self.recipes:
if recipe.date == today:
return recipe.recipe
return None
@dataclass
class Menu:
"""A menu"""
typename: str
id: str
note: str
categories: list[Category]
@classmethod
def _from_dict(cls, value: JDict) -> Menu:
return cls(
typename=value.ensure("__typename", str),
id=value.ensure("id", str),
note=value.ensure("note", str),
categories=[
Category.from_dict(category)
for category in value.ensureCast("categories", JList)
.ensureCast(0, JDict)
.ensureCast("items", JList)
.iterator()
.ensureCast(JDict)
],
)
@property
def today_recipes(self) -> list[Recipe]:
"""Get the recipes for today"""
return [
category.todays_recipe
for category in self.categories
if category.todays_recipe
]
@staticmethod
async def get_todays_menu() -> list[Recipe] | None:
"""Get the menu for today"""
async with aiohttp.ClientSession() as session:
async with session.get(URL) as response:
html = await response.text()
soup = bs4.BeautifulSoup(html, "html.parser")
tag = soup.find("script", {"id": "__NEXT_DATA__"})
if not isinstance(tag, bs4.Tag):
return None
props = JDict.fromString(str(tag.string)).chain()
raw_menu = props.ensureCast(
"props.pageProps.query.location.kitchen.digitalMenu", JDict
)
menu = Menu._from_dict(raw_menu)
today_recipes = menu.today_recipes
if not today_recipes:
return None
if any("ferien" in x.title.lower() for x in today_recipes):
return None
return today_recipes
class SendFoodstoffiMenuTask:
"""Task to send the Foodstoffi menu"""
def __init__(self, discord_bot: Stan) -> None:
self._logger = logging.getLogger("FoodstoffiMenu")
self._discord_bot = discord_bot
async def start(self) -> None:
"""Start the task"""
AioSchedule.run_daily_at(
datetime.time(hour=8, minute=0), # in UTC
self.trigger,
)
async def trigger(self) -> None:
"""Send a foodstoffi menu update to all servers"""
todays_menu = await Menu.get_todays_menu()
if todays_menu is None:
self._logger.warning("No menu available")
return
for server in self._discord_bot.servers.values():
channel = server.get_announcement_channel(_ANNOUNCEMENT_TYPE)
if channel is None:
self._logger.warning("No channel found for server %s", server.guild)
continue
role = server.get_announcement_role(_ANNOUNCEMENT_TYPE)
await PersonaSender(channel, _PERSONA).send(
f"Hiya, {role.mention}! This is today's menu:",
[x.as_embed for x in todays_menu],
)