From 46fc6ee87bab8d32768b07cc79381cdffb76aaf4755192c6ee394b6bb5c88b8a Mon Sep 17 00:00:00 2001 From: Hadrian Burkhardt Date: Sun, 3 Dec 2023 03:23:09 +0100 Subject: [PATCH] more linting and availability added. --- stw_potsdam/swp_webspeiseplan_api.py | 18 ++++++++++++++++++ stw_potsdam/swp_webspeiseplan_parser.py | 2 +- stw_potsdam/xml_types/builder.py | 2 ++ stw_potsdam/xml_types/canteen_xml.py | 14 +++++++++++--- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/stw_potsdam/swp_webspeiseplan_api.py b/stw_potsdam/swp_webspeiseplan_api.py index 6efc03d..dd8d11b 100644 --- a/stw_potsdam/swp_webspeiseplan_api.py +++ b/stw_potsdam/swp_webspeiseplan_api.py @@ -20,6 +20,11 @@ class SWPWebspeiseplanAPI: logging.basicConfig() proxy_token = self.parse_token() self.outlets = self.parse_outlets(proxy_token) + self.locations: dict[str, dict] = {} + locations = { + item["id"]: item + for item in self.parse_location(proxy_token) + } self.menus: dict[str, dict] = {} self.meal_categories: dict[str, dict] = {} for outlet in self.outlets.values(): @@ -29,6 +34,7 @@ class SWPWebspeiseplanAPI: id2cat = {item["gerichtkategorieID"]: item for item in categories} self.menus[outlet["name"]] = menu self.meal_categories[outlet["name"]] = id2cat + self.locations[outlet["name"]] = locations[location] def __spoof_req_headers(self, req: urllib.request.Request): """Add headers to a request . @@ -139,3 +145,15 @@ class SWPWebspeiseplanAPI: } menu = self.parse_model(params) return menu + + def parse_location(self, proxy_token: str) -> list[dict]: + """Get the meal catrgories for a specific location.""" + params = { + "token": proxy_token, + "model": "location", + "location": "", + "languagetype": 1, + "_": int(time.time() * 1000), + } + menu = self.parse_model(params) + return menu diff --git a/stw_potsdam/swp_webspeiseplan_parser.py b/stw_potsdam/swp_webspeiseplan_parser.py index 35ae7d4..a622ec6 100644 --- a/stw_potsdam/swp_webspeiseplan_parser.py +++ b/stw_potsdam/swp_webspeiseplan_parser.py @@ -28,6 +28,7 @@ class SWPWebspeiseplanParser: "city": addr_info["city"], "phone": outlet["contactInfo"][0]["phone"], "email": outlet["contactInfo"][0]["email"], + "availability": outlet["isPublic"] } if outlet["positionInfo"]: @@ -36,7 +37,6 @@ class SWPWebspeiseplanParser: outlet["positionInfo"]["latitude"], ) canteen_meta = CanteenMeta(**meta) - # TODO: availability via locations isPublic weekday_dict = { "monday": f"{outlet['moZeit1']}, {outlet['moZeit2']}", "tuesday": f"{outlet['diZeit1']}, {outlet['diZeit2']}", diff --git a/stw_potsdam/xml_types/builder.py b/stw_potsdam/xml_types/builder.py index e66f7a0..a2ecaa7 100644 --- a/stw_potsdam/xml_types/builder.py +++ b/stw_potsdam/xml_types/builder.py @@ -29,6 +29,8 @@ class Builder: outlet = swp_api.outlets[ntup.name] menus = swp_api.menus[ntup.name] categories = swp_api.meal_categories[ntup.name] + locations = swp_api.locations[ntup.name] + outlet["isPublic"] = locations["isPublic"] canteen = swp_parser.parse_canteen_meta_times(outlet) meals = swp_parser.parse_meals(menus, categories) for kwargs in meals: diff --git a/stw_potsdam/xml_types/canteen_xml.py b/stw_potsdam/xml_types/canteen_xml.py index bdfebf6..c25ffd2 100644 --- a/stw_potsdam/xml_types/canteen_xml.py +++ b/stw_potsdam/xml_types/canteen_xml.py @@ -32,7 +32,11 @@ class CanteenMeta: @availability.setter def availability(self, value: str): - if value in ("public", "restricted"): + if value is True: + self._availability = "public" + elif value is False: + self._availability = "restricted" + elif value in ("public", "restricted"): self._availability = value else: raise ValueError("only 'public' or 'restricted' are allowed.") @@ -89,8 +93,12 @@ class CanteenXML: canteen.appendChild(email) if self.canteen_meta.location: location = doc.createElement("location") - location.setAttribute("longitude", str(self.canteen_meta.location[0])) - location.setAttribute("latitude", str(self.canteen_meta.location[1])) + location.setAttribute( + "longitude", str(self.canteen_meta.location[0]) + ) + location.setAttribute( + "latitude", str(self.canteen_meta.location[1]) + ) canteen.appendChild(location) availability = self.__create_node( doc, "availability", self.canteen_meta.availability