fixed salattheke prices

This commit is contained in:
Hadrian Burkhardt
2026-05-01 00:38:18 +00:00
parent e9b7866bb1
commit b31796da39
2 changed files with 113 additions and 4 deletions
+53 -4
View File
@@ -1,10 +1,14 @@
import logging
import re
from datetime import datetime, date
from stw_potsdam.xml_types.canteen_xml import CanteenMeta, CanteenXML
from stw_potsdam.xml_types.times_xml import CanteenOpenTimespec, TimesXML
from stw_potsdam.xml_types.meal_xml import MealXML
EURO_PRICE_PATTERN = re.compile(r"(\d+(?:[,.]\d{1,2})?)\s*€")
class SWPWebspeiseplanParser:
"""Class method to parse SWP_Webspeiseplan."""
@@ -56,6 +60,42 @@ class SWPWebspeiseplanParser:
canteen = CanteenXML(canteen_meta, canteen_times)
return canteen
def _parse_price(self, value):
if value in (None, "", {}):
return None
return float(str(value).replace(",", "."))
def _parse_embedded_prices(
self, name: str, price: dict[str, float | None]
) -> tuple[str, dict[str, float | None]]:
if any(price.values()):
return name, price
matches = EURO_PRICE_PATTERN.findall(name)
if len(matches) < 2:
return name, price
parsed = [self._parse_price(match) for match in matches]
if len(parsed) >= 3:
price = {
"student": parsed[0],
"employee": parsed[1],
"other": parsed[2],
}
elif "Stud" in name and ("Gäste" in name or "Gaeste" in name):
price = {
"student": parsed[0],
"employee": price["employee"],
"other": parsed[1],
}
else:
return name, price
name = EURO_PRICE_PATTERN.sub("", name)
name = re.sub(r"\s*/\s*", " ", name)
name = re.sub(r"\s+", " ", name).strip()
return name, price
def parse_meals(
self, menu_data, meal_categories
) -> list[tuple[date, str, MealXML]]:
@@ -66,11 +106,20 @@ class SWPWebspeiseplanParser:
info = meal_data["speiseplanAdvancedGericht"]
additional_info = meal_data["zusatzinformationen"]
price = {
"student": additional_info["mitarbeiterpreisDecimal2"],
"employee": additional_info["price3Decimal2"],
"other": additional_info["gaestepreisDecimal2"],
"student": self._parse_price(
additional_info["mitarbeiterpreisDecimal2"]
),
"employee": self._parse_price(
additional_info["price3Decimal2"]
),
"other": self._parse_price(
additional_info["gaestepreisDecimal2"]
),
}
meal = MealXML(name=info["gerichtname"], price=price)
name, price = self._parse_embedded_prices(
info["gerichtname"], price
)
meal = MealXML(name=name, price=price)
day = datetime.fromisoformat(info["datum"]).date()
category = meal_categories[info["gerichtkategorieID"]]["name"]
meals.append(