Compare commits
No commits in common. "199048207a3a29db07d634cec96d45bb5fcf55a5" and "64e52acbdb96de81347c713dfb06a206d1b77297" have entirely different histories.
199048207a
...
64e52acbdb
4 changed files with 23 additions and 84 deletions
13
app.py
13
app.py
|
@ -11,19 +11,10 @@ parsers.define_parsers()
|
||||||
|
|
||||||
VIRTUAL_HOST=os.getenv('VIRTUAL_HOST')
|
VIRTUAL_HOST=os.getenv('VIRTUAL_HOST')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/mensa/<parser_name>.json')
|
@app.route('/mensa/<parser_name>.json')
|
||||||
def index(parser_name):
|
def index(parser_name):
|
||||||
try:
|
try:
|
||||||
return utils.get_parser(parser_name).get_canteen_index(f'http://{VIRTUAL_HOST}/meta')
|
return utils.get_parser(parser_name).get_canteen_index(f'https://{VIRTUAL_HOST}/meta')
|
||||||
except KeyError:
|
|
||||||
abort(404)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/mensa/<parser_name>/meta/<canteen_name>')
|
|
||||||
def meta(parser_name, canteen_name):
|
|
||||||
try:
|
|
||||||
return utils.get_parser(parser_name).get_meta_data(canteen_name, f'http://{VIRTUAL_HOST}/mensa/{parser_name}/feed')
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
||||||
|
@ -31,6 +22,6 @@ def meta(parser_name, canteen_name):
|
||||||
@app.route('/mensa/<parser_name>/feed/<canteen_name>')
|
@app.route('/mensa/<parser_name>/feed/<canteen_name>')
|
||||||
def mensa(parser_name, canteen_name):
|
def mensa(parser_name, canteen_name):
|
||||||
try:
|
try:
|
||||||
return utils.get_parser(parser_name).get_meal_data(canteen_name)
|
return utils.get_parser(parser_name).parse(canteen_name)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
abort(404)
|
abort(404)
|
||||||
|
|
|
@ -7,100 +7,70 @@
|
||||||
"suffix": "611",
|
"suffix": "611",
|
||||||
"name": "Mensa Shedhalle",
|
"name": "Mensa Shedhalle",
|
||||||
"street": "Schlachthausstraße 13",
|
"street": "Schlachthausstraße 13",
|
||||||
"zipCode": "72074",
|
"city": "72074 Tübingen"
|
||||||
"city": "Tübingen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-shedhalle/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-morgenstelle",
|
"id": "mensa-morgenstelle",
|
||||||
"suffix": "621",
|
"suffix": "621",
|
||||||
"name": "Mensa Morgenstelle",
|
"name": "Mensa Morgenstelle",
|
||||||
"street": "Auf der Morgenstelle 26",
|
"street": "Auf der Morgenstelle 26",
|
||||||
"zipCode": "72076",
|
"city": "72076 Tübingen"
|
||||||
"city": "Tübingen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-morgenstelle-tuebingen/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-prinz_karl",
|
"id": "mensa-prinz_karl",
|
||||||
"suffix": "623",
|
"suffix": "623",
|
||||||
"name": "Mensa Prinz Karl",
|
"name": "Mensa Prinz Karl",
|
||||||
"street": "Hafengasse 6",
|
"street": "Hafengasse 6",
|
||||||
"zipCode": "72070",
|
"city": "72070 Tübingen"
|
||||||
"city": "Tübingen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-prinz-karl-tuebingen/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-hohenheim",
|
"id": "mensa-hohenheim",
|
||||||
"suffix": "661",
|
"suffix": "661",
|
||||||
"name": "Mensa Hohenheim",
|
"name": "Mensa Hohenheim",
|
||||||
"street": "Garbenstraße 29",
|
"street": "Garbenstraße 29",
|
||||||
"zipCode": "70599",
|
"city": "70599 Stuttgart"
|
||||||
"city": "Stuttgart",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-hohenheim/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-reutlingen",
|
"id": "mensa-reutlingen",
|
||||||
"suffix": "630",
|
"suffix": "630",
|
||||||
"name": "Mensa Reutlingen",
|
"name": "Mensa Reutlingen",
|
||||||
"street": "Alteburgstraße 150 Gebäude 11",
|
"street": "Alteburgstraße 150 Gebäude 11",
|
||||||
"zipCode": "72762",
|
"city": "72762 Reutlingen"
|
||||||
"city": "Reutlingen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-reutlingen/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-nuertingen",
|
"id": "mensa-nuertingen",
|
||||||
"suffix": "665",
|
"suffix": "665",
|
||||||
"name": "Mensa Nürtingen",
|
"name": "Mensa Nürtingen",
|
||||||
"street": "Neckarsteige 6-10",
|
"street": "Neckarsteige 6-10",
|
||||||
"zipCode": "72622",
|
"city": "72622 Nürtingen"
|
||||||
"city": "Nürtingen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-nuertingen/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-rottenburg",
|
"id": "mensa-rottenburg",
|
||||||
"suffix": "655",
|
"suffix": "655",
|
||||||
"name": "Mensa Rottenburg",
|
"name": "Mensa Rottenburg",
|
||||||
"street": "Schadenweiler Hof 1",
|
"street": "Schadenweiler Hof 1",
|
||||||
"zipCode": "72108",
|
"city": "72108 Rottenburg"
|
||||||
"city": "Rottenburg",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-rottenburg/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-albstadt",
|
"id": "mensa-albstadt",
|
||||||
"suffix": "645",
|
"suffix": "645",
|
||||||
"name": "Mensa Albstadt",
|
"name": "Mensa Albstadt",
|
||||||
"street": "Jakobstraße 1",
|
"street": "Jakobstraße 1",
|
||||||
"zipCode": "72458",
|
"city": "72458 Albstadt"
|
||||||
"city": "Albstadt",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-albstadt/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "mensa-sigmaringen",
|
"id": "mensa-sigmaringen",
|
||||||
"suffix": "640",
|
"suffix": "640",
|
||||||
"name": "Mensa Sigmaringen",
|
"name": "Mensa Sigmaringen",
|
||||||
"street": "Anton-Günther-Straße 51",
|
"street": "Anton-Günther-Straße 51",
|
||||||
"zipCode": "72488",
|
"city": " 72488 Sigmaringen"
|
||||||
"city": "Sigmaringen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/mensa-sigmaringen/"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "cafeteria-morgenstelle",
|
"id": "cafeteria-morgenstelle",
|
||||||
"suffix": "724",
|
"suffix": "724",
|
||||||
"name": "Cafeteria Morgenstelle",
|
"name": "Cafeteria Morgenstelle",
|
||||||
"street": "Auf der Morgenstelle 26",
|
"street": "Auf der Morgenstelle 26",
|
||||||
"zipCode": "72076",
|
"city": "72076 Tübingen"
|
||||||
"city": "Tübingen",
|
|
||||||
"public": true,
|
|
||||||
"source": "https://www.my-stuwe.de/mensa/cafeteria-morgenstelle-tuebingen/"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -52,7 +52,7 @@ legend = {
|
||||||
multiple_whitespaces_regex = re.compile('\\s{2,}')
|
multiple_whitespaces_regex = re.compile('\\s{2,}')
|
||||||
|
|
||||||
|
|
||||||
def get_meal_data(url, today=False):
|
def parse_url(url, today=False):
|
||||||
canteen = LazyBuilder()
|
canteen = LazyBuilder()
|
||||||
with urlopen(Request(url, None, {'User-Agent': 'Mozilla/5.0'})) as response:
|
with urlopen(Request(url, None, {'User-Agent': 'Mozilla/5.0'})) as response:
|
||||||
data = json.loads(response.read())
|
data = json.loads(response.read())
|
||||||
|
@ -88,8 +88,6 @@ def define_parsers():
|
||||||
with open('parsers/tuebingen.json') as canteen_file:
|
with open('parsers/tuebingen.json') as canteen_file:
|
||||||
canteen_json = json.load(canteen_file)
|
canteen_json = json.load(canteen_file)
|
||||||
|
|
||||||
parser = Parser(canteen_json['name'], meal_data_handler=get_meal_data,
|
parser = Parser(canteen_json['name'], handler=parse_url, shared_prefix=canteen_json['base_url'])
|
||||||
base_url=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['zipCode'],
|
parser.define(Canteen(canteen['id'], canteen['suffix'], canteen['name'], canteen['street'], canteen['city']))
|
||||||
canteen['city'], canteen['public'], canteen['source']))
|
|
||||||
|
|
34
utils.py
34
utils.py
|
@ -1,5 +1,3 @@
|
||||||
from pyopenmensa.feed import LazyBuilder, Feed
|
|
||||||
|
|
||||||
parser_list = {}
|
parser_list = {}
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,51 +6,33 @@ class Canteen:
|
||||||
suffix: str
|
suffix: str
|
||||||
name: str
|
name: str
|
||||||
street: str
|
street: str
|
||||||
zip_code: str
|
|
||||||
city: str
|
city: str
|
||||||
public: bool
|
|
||||||
source: str
|
|
||||||
|
|
||||||
def __init__(self, canteen_id, suffix, name, street, zip_code, city, public, source):
|
def __init__(self, canteen_id, suffix, name, street, city):
|
||||||
self.canteen_id = canteen_id
|
self.canteen_id = canteen_id
|
||||||
self.suffix = suffix
|
self.suffix = suffix
|
||||||
self.name = name
|
self.name = name
|
||||||
self.street = street
|
self.street = street
|
||||||
self.zip_code = zip_code
|
|
||||||
self.city = city
|
self.city = city
|
||||||
self.public = public
|
|
||||||
self.source = source
|
|
||||||
|
|
||||||
def address(self):
|
def address(self):
|
||||||
return f'{self.street}, {self.zip_code} {self.city}'
|
return f'{self.street}, {self.city}'
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
canteens: dict[str, Canteen] = {}
|
canteens: dict[str, Canteen] = {}
|
||||||
|
|
||||||
def __init__(self, name, meal_data_handler, base_url: str):
|
def __init__(self, name, handler, shared_prefix: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.meal_data_handler = meal_data_handler
|
self.handler = handler
|
||||||
self.base_url = base_url
|
self.shared_prefix = shared_prefix
|
||||||
parser_list[name] = self
|
parser_list[name] = self
|
||||||
|
|
||||||
def define(self, canteen: Canteen):
|
def define(self, canteen: Canteen):
|
||||||
self.canteens[canteen.canteen_id] = canteen
|
self.canteens[canteen.canteen_id] = canteen
|
||||||
|
|
||||||
def get_meal_data(self, canteen: str):
|
def parse(self, canteen: str):
|
||||||
return self.meal_data_handler(self.base_url + self.canteens[canteen].suffix)
|
return self.handler(self.shared_prefix + self.canteens[canteen].suffix)
|
||||||
|
|
||||||
def get_meta_data(self, canteen: str, prefix: str):
|
|
||||||
canteen = self.canteens[canteen]
|
|
||||||
meta_data = LazyBuilder()
|
|
||||||
meta_data.name = canteen.name
|
|
||||||
meta_data.address = canteen.address()
|
|
||||||
meta_data.city = canteen.city
|
|
||||||
meta_data.availability = 'public' if canteen.public else 'restricted'
|
|
||||||
meta_data.feeds.append(
|
|
||||||
Feed('full', priority='0', url=f'{prefix}/{canteen.canteen_id}', source=canteen.source, dayOfWeek='*', dayOfMonth='*',
|
|
||||||
hour='9', minute='30', retry='60 1 1440'))
|
|
||||||
return meta_data.toXMLFeed()
|
|
||||||
|
|
||||||
def get_canteen_index(self, prefix: str):
|
def get_canteen_index(self, prefix: str):
|
||||||
index: dict[str, str] = {}
|
index: dict[str, str] = {}
|
||||||
|
|
Loading…
Reference in a new issue