diff --git a/app.py b/app.py index e944251..16cdae2 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,5 @@ +import os + from flask import Flask, abort import parsers @@ -7,11 +9,19 @@ app = Flask(__name__) parsers.define_parsers() +VIRTUAL_HOST=os.getenv('VIRTUAL_HOST') -@app.route('/mensa//') -def mensa(parser_name, mensa_name): +@app.route('/mensa/.json') +def index(parser_name): try: - return utils.get_parser(parser_name).parse(mensa_name) + return utils.get_parser(parser_name).get_canteen_index(f'https://{VIRTUAL_HOST}/meta') except KeyError: abort(404) + +@app.route('/mensa//feed/') +def mensa(parser_name, canteen_name): + try: + return utils.get_parser(parser_name).parse(canteen_name) + except KeyError: + abort(404) diff --git a/parsers/tuebingen.py b/parsers/tuebingen.py index 734e94d..734a518 100644 --- a/parsers/tuebingen.py +++ b/parsers/tuebingen.py @@ -4,7 +4,7 @@ from urllib.request import urlopen, Request from pyopenmensa.feed import LazyBuilder -from utils import Parser +from utils import Parser, Canteen allergens = { 'Gl-a': 'Weizen', @@ -90,4 +90,4 @@ def define_parsers(): parser = Parser(canteen_json['name'], handler=parse_url, shared_prefix=canteen_json['base_url']) for canteen in canteen_json['canteens']: - parser.define(canteen['id'], canteen['suffix']) + parser.define(Canteen(canteen['id'], canteen['suffix'], canteen['name'], canteen['street'], canteen['city'])) diff --git a/utils.py b/utils.py index 7eb5f36..2adb9b5 100644 --- a/utils.py +++ b/utils.py @@ -1,8 +1,26 @@ parser_list = {} +class Canteen: + canteen_id: str + suffix: str + name: str + street: str + city: str + + def __init__(self, canteen_id, suffix, name, street, city): + self.canteen_id = canteen_id + self.suffix = suffix + self.name = name + self.street = street + self.city = city + + def address(self): + return f'{self.street}, {self.city}' + + class Parser: - sources = {} + canteens: dict[str, Canteen] = {} def __init__(self, name, handler, shared_prefix: str): self.name = name @@ -10,11 +28,18 @@ class Parser: self.shared_prefix = shared_prefix parser_list[name] = self - def define(self, name: str, suffix: str): - self.sources[name] = suffix + def define(self, canteen: Canteen): + self.canteens[canteen.canteen_id] = canteen - def parse(self, source: str): - return self.handler(self.shared_prefix + self.sources[source]) + def parse(self, canteen: str): + return self.handler(self.shared_prefix + self.canteens[canteen].suffix) + + 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 def get_parser(parser_name: str) -> Parser: