srehwald / eat-api

Simple static API for some (student) food places in Munich.
https://srehwald.github.io/eat-api/
MIT License
14 stars 20 forks source link

Fixed crash for multirow dishes #13

Closed COM8 closed 6 years ago

COM8 commented 6 years ago

The program tries to access the last index of the dishes array although it's empty.

Which canteens produce this error:

stubistro-rosenheim mensa-leopoldstr mensa-arcisstrasse mensa-arcisstr

Which menu/dish produces this error (one example):

The dishes "Rahmchampignons" and "Semmelknödel" produce it for Tuesday 23.01.2018. (link to the menu).

Help/Test code:

Just replace line 137 to 146 with the following code snipped. It will output all dishes, that cause this problem.

        for name in dishes_dict:
            try:
                if not dishes_dict[name]:
                # some dishes are multi-row. That means that for the same type the dish is written in multiple rows.
                # From the second row on the type is then just empty. In that case, we just use the price of the
                # previous dish.
                    dishes.append(Dish(name, dishes[-1].price))
                else:
                    dishes.append(Dish(name, StudentenwerkMenuParser.prices.get(dishes_dict[name], "N/A")))
            except Exception as e:
                print(name +  ' ' + str(len(dishes)))

        return dishes

Exception:

Traceback (most recent call last):
  File "main.py", line 94, in <module>
    main()
  File "main.py", line 58, in main
    menus = parser.parse(location)
  File "/home/fabian/Dokumente/eat-api/src/menu_parser.py", line 86, in parse
    return self.get_menus(tree)
  File "/home/fabian/Dokumente/eat-api/src/menu_parser.py", line 109, in get_menus
    dishes = self.__parse_dishes(menu_html)
  File "/home/fabian/Dokumente/eat-api/src/menu_parser.py", line 142, in __parse_dishes
    dishes.append(Dish(name, dishes[-1].price))
IndexError: list index out of range
srehwald commented 6 years ago

can you please briefly describe for which menu/dish the error occurred? Just so that I understand the fix :)

COM8 commented 6 years ago

Updated The description.

srehwald commented 6 years ago

running it for mensa-leopoldstr does not produce the error on my machine. Did they maybe fix the webpage?

COM8 commented 6 years ago

I've pulled the latest version of your parse and it still results in mensa-leopoldstr, mensa-arcisstr, stubistro-rosenheim throwing errors.

raabf commented 6 years ago

Well this is one of the situations where have empty cells in the first column:

auswahl_029

This if condition https://github.com/srehwald/eat-api/pull/13/commits/17cf971bb81ab8ec6531dfa96250af8a033a8944#diff-bb2fc7cfe5a6a3d58ae2a97a2733be7aL138 is intended to use the price of the previous row (here Mensa Spezial of Bauerneintopf mit Rindfleisch (GQB)) if the price column is empty. So the index error means that dishes is empty. So the correct question is, why it fails to parse the row Mensa Spezial Bauerneintopf mit Rindfleisch (GQB) before?

srehwald commented 6 years ago

I'm convinced.