modular parser framework
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
"""Potsdam parser/provider implementation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from openmensa_parsers.config import Canteen
|
||||
from openmensa_parsers.parsers.base import BaseOpenMensaParser, FeedDefinition
|
||||
from openmensa_parsers.webspeiseplan_api import (
|
||||
WebspeiseplanAPI,
|
||||
WebspeiseplanData,
|
||||
)
|
||||
from openmensa_parsers.webspeiseplan_parser import WebspeiseplanParser
|
||||
from openmensa_parsers.xml_types.canteen_xml import CanteenXML
|
||||
|
||||
|
||||
class PotsdamParser(BaseOpenMensaParser):
|
||||
"""Parser for Studentenwerk Potsdam's Webspeiseplan source."""
|
||||
|
||||
id = "potsdam"
|
||||
BASE_URL = "https://swp.webspeiseplan.de"
|
||||
feed = FeedDefinition(source=BASE_URL)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api: WebspeiseplanAPI | None = None,
|
||||
parser: WebspeiseplanParser | None = None,
|
||||
) -> None:
|
||||
"""Initialize the Potsdam parser with fetch and parse helpers."""
|
||||
self.api = WebspeiseplanAPI(self.BASE_URL) if api is None else api
|
||||
self.parser = WebspeiseplanParser() if parser is None else parser
|
||||
self.logger = logging.getLogger(__name__)
|
||||
|
||||
def fetch(self) -> WebspeiseplanData:
|
||||
"""Download all data required by the Potsdam parser."""
|
||||
return self.api.fetch_all()
|
||||
|
||||
def parse(
|
||||
self,
|
||||
config: dict[str, Canteen],
|
||||
raw_data: WebspeiseplanData,
|
||||
) -> dict[str, CanteenXML]:
|
||||
"""Convert Potsdam Webspeiseplan data into canteen structures."""
|
||||
parsed: dict[str, CanteenXML] = {}
|
||||
for canteen_key, configured_canteen in config.items():
|
||||
source_name = configured_canteen.name
|
||||
if source_name not in raw_data.outlets:
|
||||
self.logger.warning("%s not found in keys", source_name)
|
||||
continue
|
||||
|
||||
outlet = dict(raw_data.outlets[source_name])
|
||||
menus = raw_data.menus[source_name]
|
||||
categories = raw_data.meal_categories[source_name]
|
||||
locations = raw_data.locations[source_name]
|
||||
outlet["isPublic"] = locations["isPublic"]
|
||||
|
||||
canteen = self.parser.parse_canteen_meta_times(outlet)
|
||||
for meal_data in self.parser.parse_meals(menus, categories):
|
||||
canteen.add_meal(**meal_data)
|
||||
parsed[canteen_key] = canteen
|
||||
return parsed
|
||||
Reference in New Issue
Block a user