Compare commits
No commits in common. "64e52acbdb96de81347c713dfb06a206d1b77297" and "86e508bc9d0716e004ca8ca6220d9eb1c15c3737" have entirely different histories.
64e52acbdb
...
86e508bc9d
4 changed files with 10 additions and 46 deletions
|
@ -4,5 +4,4 @@ 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
16
app.py
|
@ -1,5 +1,3 @@
|
|||
import os
|
||||
|
||||
from flask import Flask, abort
|
||||
|
||||
import parsers
|
||||
|
@ -9,19 +7,11 @@ app = Flask(__name__)
|
|||
|
||||
parsers.define_parsers()
|
||||
|
||||
VIRTUAL_HOST=os.getenv('VIRTUAL_HOST')
|
||||
|
||||
@app.route('/mensa/<parser_name>.json')
|
||||
def index(parser_name):
|
||||
@app.route('/mensa/<parser_name>/<mensa_name>')
|
||||
def mensa(parser_name, mensa_name):
|
||||
try:
|
||||
return utils.get_parser(parser_name).get_canteen_index(f'https://{VIRTUAL_HOST}/meta')
|
||||
return utils.get_parser(parser_name).parse(mensa_name)
|
||||
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)
|
||||
|
|
|
@ -4,7 +4,7 @@ from urllib.request import urlopen, Request
|
|||
|
||||
from pyopenmensa.feed import LazyBuilder
|
||||
|
||||
from utils import Parser, Canteen
|
||||
from utils import Parser
|
||||
|
||||
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(canteen['id'], canteen['suffix'], canteen['name'], canteen['street'], canteen['city']))
|
||||
parser.define(canteen['id'], canteen['suffix'])
|
||||
|
|
35
utils.py
35
utils.py
|
@ -1,26 +1,8 @@
|
|||
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:
|
||||
canteens: dict[str, Canteen] = {}
|
||||
sources = {}
|
||||
|
||||
def __init__(self, name, handler, shared_prefix: str):
|
||||
self.name = name
|
||||
|
@ -28,18 +10,11 @@ class Parser:
|
|||
self.shared_prefix = shared_prefix
|
||||
parser_list[name] = self
|
||||
|
||||
def define(self, canteen: Canteen):
|
||||
self.canteens[canteen.canteen_id] = canteen
|
||||
def define(self, name: str, suffix: str):
|
||||
self.sources[name] = suffix
|
||||
|
||||
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 parse(self, source: str):
|
||||
return self.handler(self.shared_prefix + self.sources[source])
|
||||
|
||||
|
||||
def get_parser(parser_name: str) -> Parser:
|
||||
|
|
Loading…
Reference in a new issue