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 .
|
COPY requirements.txt .
|
||||||
RUN pip install -r requirements.txt
|
RUN pip install -r requirements.txt
|
||||||
COPY . .
|
COPY . .
|
||||||
EXPOSE 8000
|
|
||||||
CMD ["gunicorn", "-b", "0.0.0.0", "app:app"]
|
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
|
from flask import Flask, abort
|
||||||
|
|
||||||
import parsers
|
import parsers
|
||||||
|
@ -9,19 +7,11 @@ app = Flask(__name__)
|
||||||
|
|
||||||
parsers.define_parsers()
|
parsers.define_parsers()
|
||||||
|
|
||||||
VIRTUAL_HOST=os.getenv('VIRTUAL_HOST')
|
|
||||||
|
|
||||||
@app.route('/mensa/<parser_name>.json')
|
@app.route('/mensa/<parser_name>/<mensa_name>')
|
||||||
def index(parser_name):
|
def mensa(parser_name, mensa_name):
|
||||||
try:
|
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:
|
except KeyError:
|
||||||
abort(404)
|
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 pyopenmensa.feed import LazyBuilder
|
||||||
|
|
||||||
from utils import Parser, Canteen
|
from utils import Parser
|
||||||
|
|
||||||
allergens = {
|
allergens = {
|
||||||
'Gl-a': 'Weizen',
|
'Gl-a': 'Weizen',
|
||||||
|
@ -90,4 +90,4 @@ def define_parsers():
|
||||||
|
|
||||||
parser = Parser(canteen_json['name'], handler=parse_url, shared_prefix=canteen_json['base_url'])
|
parser = Parser(canteen_json['name'], handler=parse_url, shared_prefix=canteen_json['base_url'])
|
||||||
for canteen in canteen_json['canteens']:
|
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 = {}
|
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:
|
class Parser:
|
||||||
canteens: dict[str, Canteen] = {}
|
sources = {}
|
||||||
|
|
||||||
def __init__(self, name, handler, shared_prefix: str):
|
def __init__(self, name, handler, shared_prefix: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -28,18 +10,11 @@ class Parser:
|
||||||
self.shared_prefix = shared_prefix
|
self.shared_prefix = shared_prefix
|
||||||
parser_list[name] = self
|
parser_list[name] = self
|
||||||
|
|
||||||
def define(self, canteen: Canteen):
|
def define(self, name: str, suffix: str):
|
||||||
self.canteens[canteen.canteen_id] = canteen
|
self.sources[name] = suffix
|
||||||
|
|
||||||
def parse(self, canteen: str):
|
def parse(self, source: str):
|
||||||
return self.handler(self.shared_prefix + self.canteens[canteen].suffix)
|
return self.handler(self.shared_prefix + self.sources[source])
|
||||||
|
|
||||||
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:
|
def get_parser(parser_name: str) -> Parser:
|
||||||
|
|
Loading…
Reference in a new issue