Compare commits

..

2 commits

4 changed files with 46 additions and 10 deletions

View file

@ -4,4 +4,5 @@ WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0", "app:app"]

16
app.py
View file

@ -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/<parser_name>/<mensa_name>')
def mensa(parser_name, mensa_name):
@app.route('/mensa/<parser_name>.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/<parser_name>/feed/<canteen_name>')
def mensa(parser_name, canteen_name):
try:
return utils.get_parser(parser_name).parse(canteen_name)
except KeyError:
abort(404)

View file

@ -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']))

View file

@ -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: