Handle variation in API response: offers can also be a hash

This commit is contained in:
f4lco
2020-10-09 17:23:19 +02:00
parent ecc23b9e6b
commit 7c70ed61aa
4 changed files with 4359 additions and 1 deletions
+18 -1
View File
@@ -40,7 +40,7 @@ def _prices(offer):
def _process_day(builder, day):
for offer in day['angebote']:
for offer in _offers(day):
builder.addMeal(date=day['data'],
category=offer['titel'],
name=offer['beschreibung'],
@@ -49,6 +49,23 @@ def _process_day(builder, day):
roles=None)
def _offers(day):
offers = day['angebote']
if isinstance(offers, list):
return offers
if isinstance(offers, dict):
# allows for the following structure:
# {'-1': <garbage>, '0': first_offer, ...}
# This case is degenerate and occurs only on semi-regular basis
# as of 2020-10-20. The assumption that offers at logical index -1
# are garbage can be challenged, it is simply a result of observing
# the API responses over several months.
return [offer for index, offer in offers.items() if int(index) >= 0]
raise AssertionError(f'cannot handle offers of type {type(offers)}')
def render_menu(menu):
"""Render the menu for a canteen into an OpenMensa XML feed.