mensa-parser/utils.py

67 lines
2 KiB
Python
Raw Normal View History

2023-02-04 19:20:01 +01:00
from pyopenmensa.feed import LazyBuilder, Feed
2022-04-23 10:15:17 +02:00
parser_list = {}
class Canteen:
canteen_id: str
suffix: str
name: str
street: str
2023-02-04 19:20:01 +01:00
zip_code: str
city: str
2023-02-04 19:20:01 +01:00
public: bool
source: str
2023-02-04 19:20:01 +01:00
def __init__(self, canteen_id, suffix, name, street, zip_code, city, public, source):
self.canteen_id = canteen_id
self.suffix = suffix
self.name = name
self.street = street
2023-02-04 19:20:01 +01:00
self.zip_code = zip_code
self.city = city
2023-02-04 19:20:01 +01:00
self.public = public
self.source = source
def address(self):
2023-02-04 19:20:01 +01:00
return f'{self.street}, {self.zip_code} {self.city}'
2022-04-23 10:15:17 +02:00
class Parser:
canteens: dict[str, Canteen] = {}
2022-04-23 10:15:17 +02:00
def __init__(self, name, meal_data_handler, base_url: str):
2022-04-23 10:15:17 +02:00
self.name = name
self.meal_data_handler = meal_data_handler
self.base_url = base_url
2022-04-23 10:15:17 +02:00
parser_list[name] = self
def define(self, canteen: Canteen):
self.canteens[canteen.canteen_id] = canteen
def get_meal_data(self, canteen: str):
return self.meal_data_handler(self.base_url + self.canteens[canteen].suffix)
2022-04-23 10:15:17 +02:00
2023-02-04 19:20:01 +01:00
def get_meta_data(self, canteen: str, prefix: str):
canteen = self.canteens[canteen]
meta_data = LazyBuilder()
meta_data.name = canteen.name
meta_data.address = canteen.address()
meta_data.city = canteen.city
meta_data.availability = 'public' if canteen.public else 'restricted'
meta_data.feeds.append(
Feed('full', priority='0', url=f'{prefix}/{canteen.canteen_id}', source=canteen.source, dayOfWeek='*', dayOfMonth='*',
hour='9', minute='30', retry='60 1 1440'))
return meta_data.toXMLFeed()
def get_canteen_index(self, prefix: str):
index: dict[str, str] = {}
canteen: Canteen
for canteen in self.canteens.values():
index[canteen.canteen_id] = f'{prefix}/{canteen.canteen_id}.xml'
return index
2022-04-23 10:15:17 +02:00
def get_parser(parser_name: str) -> Parser:
return parser_list[parser_name]