Compare commits
2 commits
86e508bc9d
...
64e52acbdb
Author | SHA1 | Date | |
---|---|---|---|
64e52acbdb | |||
ebe3c864fa |
4 changed files with 46 additions and 10 deletions
|
@ -4,4 +4,5 @@ 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,3 +1,5 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from flask import Flask, abort
|
from flask import Flask, abort
|
||||||
|
|
||||||
import parsers
|
import parsers
|
||||||
|
@ -7,11 +9,19 @@ app = Flask(__name__)
|
||||||
|
|
||||||
parsers.define_parsers()
|
parsers.define_parsers()
|
||||||
|
|
||||||
|
VIRTUAL_HOST=os.getenv('VIRTUAL_HOST')
|
||||||
|
|
||||||
@app.route('/mensa/<parser_name>/<mensa_name>')
|
@app.route('/mensa/<parser_name>.json')
|
||||||
def mensa(parser_name, mensa_name):
|
def index(parser_name):
|
||||||
try:
|
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:
|
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
|
from utils import Parser, Canteen
|
||||||
|
|
||||||
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['id'], canteen['suffix'])
|
parser.define(Canteen(canteen['id'], canteen['suffix'], canteen['name'], canteen['street'], canteen['city']))
|
||||||
|
|
35
utils.py
35
utils.py
|
@ -1,8 +1,26 @@
|
||||||
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:
|
||||||
sources = {}
|
canteens: dict[str, Canteen] = {}
|
||||||
|
|
||||||
def __init__(self, name, handler, shared_prefix: str):
|
def __init__(self, name, handler, shared_prefix: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -10,11 +28,18 @@ class Parser:
|
||||||
self.shared_prefix = shared_prefix
|
self.shared_prefix = shared_prefix
|
||||||
parser_list[name] = self
|
parser_list[name] = self
|
||||||
|
|
||||||
def define(self, name: str, suffix: str):
|
def define(self, canteen: Canteen):
|
||||||
self.sources[name] = suffix
|
self.canteens[canteen.canteen_id] = canteen
|
||||||
|
|
||||||
def parse(self, source: str):
|
def parse(self, canteen: str):
|
||||||
return self.handler(self.shared_prefix + self.sources[source])
|
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:
|
def get_parser(parser_name: str) -> Parser:
|
||||||
|
|
Loading…
Reference in a new issue