refactor: make webspeiseplan fetching explicit
This commit is contained in:
@@ -1,9 +1,22 @@
|
||||
import logging
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import re
|
||||
import time
|
||||
import json
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SWPWebspeiseplanData:
|
||||
"""Downloaded SWP Webspeiseplan data grouped by outlet name."""
|
||||
|
||||
outlets: dict[str, dict]
|
||||
locations: dict[str, dict]
|
||||
menus: dict[str, dict]
|
||||
meal_categories: dict[str, dict]
|
||||
|
||||
|
||||
class SWPWebspeiseplanAPI:
|
||||
"""This class is used download content from SWP_Webspeiseplan.
|
||||
@@ -16,25 +29,34 @@ class SWPWebspeiseplanAPI:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the configuration for the web service."""
|
||||
"""Initialize the web service client."""
|
||||
logging.basicConfig()
|
||||
|
||||
def fetch_all(self) -> SWPWebspeiseplanData:
|
||||
"""Download all data required to render OpenMensa feeds."""
|
||||
proxy_token = self.parse_token()
|
||||
self.outlets = self.parse_outlets(proxy_token)
|
||||
self.locations: dict[str, dict] = {}
|
||||
outlets = self.parse_outlets(proxy_token)
|
||||
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():
|
||||
menus: dict[str, dict] = {}
|
||||
meal_categories: dict[str, dict] = {}
|
||||
outlet_locations: dict[str, dict] = {}
|
||||
for outlet in outlets.values():
|
||||
location = outlet["standortID"]
|
||||
menu = self.parse_menu(proxy_token, location)
|
||||
categories = self.parse_meal_category(proxy_token, location)
|
||||
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]
|
||||
menus[outlet["name"]] = menu
|
||||
meal_categories[outlet["name"]] = id2cat
|
||||
outlet_locations[outlet["name"]] = locations[location]
|
||||
return SWPWebspeiseplanData(
|
||||
outlets=outlets,
|
||||
locations=outlet_locations,
|
||||
menus=menus,
|
||||
meal_categories=meal_categories,
|
||||
)
|
||||
|
||||
def __spoof_req_headers(self, req: urllib.request.Request):
|
||||
"""Add headers to a request .
|
||||
@@ -77,9 +99,8 @@ class SWPWebspeiseplanAPI:
|
||||
Returns:
|
||||
[type]: [description]
|
||||
"""
|
||||
url = f"{SWPWebspeiseplanAPI.URL_BASE}/index.php?" + "&".join(
|
||||
[f"{k}={v}" for k, v in params.items()]
|
||||
)
|
||||
query = urllib.parse.urlencode(params)
|
||||
url = f"{SWPWebspeiseplanAPI.URL_BASE}/index.php?{query}"
|
||||
SWPWebspeiseplanAPI.logger.debug("__parse_model: %s", url)
|
||||
req = urllib.request.Request(url)
|
||||
self.__spoof_req_headers(req)
|
||||
|
||||
Reference in New Issue
Block a user