From 654540fd175606036657d2bb3e36e5c942d79f22 Mon Sep 17 00:00:00 2001 From: Linus Karlsson Date: Wed, 7 Dec 2016 11:15:06 +0100 Subject: [PATCH 1/2] Fix MoP menu (finally). Also add requirements.txt. --- menus/mop.py | 6 ++++-- requirements.txt | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 requirements.txt diff --git a/menus/mop.py b/menus/mop.py index 0a31e28..00003d0 100644 --- a/menus/mop.py +++ b/menus/mop.py @@ -35,8 +35,10 @@ def get_week(self): for menu_item in li.find('div', {'class': 'event-info'}).find_all('p'): menu_items.append(menu_item.text.strip()) - # add the list of dishes to the menu - self.menu[weekday] = menu_items + # add the list of dishes to the menu, but only if it doesn't already + # exist. Otherwise we'll overwrite the current menu with next week's + if weekday not in self.menu: + self.menu[weekday] = menu_items return self.menu diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fb6203e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +beautifulsoup4==4.5.1 +bs4==0.0.1 +PyYAML==3.12 +requests==2.12.3 +schedule==0.4.2 +six==1.10.0 +slackclient==1.0.2 +websocket-client==0.39.0 From eff6053dfe45c1d08bcd3d688fd3b27184771abe Mon Sep 17 00:00:00 2001 From: Linus Karlsson Date: Wed, 7 Dec 2016 11:51:17 +0100 Subject: [PATCH 2/2] Add parser for Bryggan. --- geoffrey_config.yaml | 2 +- menus/bryggan.py | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 menus/bryggan.py diff --git a/geoffrey_config.yaml b/geoffrey_config.yaml index 4600964..053e2a3 100644 --- a/geoffrey_config.yaml +++ b/geoffrey_config.yaml @@ -1,3 +1,3 @@ UPDATE_TIME: "07:00" POST_TIME: "11:00" -MENUS: ['mop.MOP', 'finnin.FinnIn', 'finnut.FinnUt', 'avesta.Avesta'] +MENUS: ['mop.MOP', 'finnin.FinnIn', 'finnut.FinnUt', 'avesta.Avesta', 'bryggan.Bryggan'] diff --git a/menus/bryggan.py b/menus/bryggan.py new file mode 100644 index 0000000..0fb3490 --- /dev/null +++ b/menus/bryggan.py @@ -0,0 +1,67 @@ + +import requests +from bs4 import BeautifulSoup + +from menus.menu import Menu + +class Bryggan(Menu): + + def __init__(self): + self.url = 'http://www.bryggancafe.se/veckans-lunch/' + self.menu = {} + # swedish day of week names + self.dow = {0: 'Måndag', 1: 'Tisdag', 2: 'Onsdag', 3: 'Torsdag', 4: 'Fredag'} + + def __repr__(self): + return "Bryggan Kök och Café" + + def get_week(self): + """ + Fetches the menu data from the given URL, returns a menu dictionary: + { + 'dayofweek 1': ['dish 1', 'dish 2', ..., 'dish N'], + 'dayofweek 2': [ ... ] + } + """ + content = requests.get(self.url) + soup = BeautifulSoup(content.text, 'html.parser') + # menu list + menu_list = soup.find('div', {'class': 'et_pb_promo_description'}) + last_day = None + for p in menu_list.find_all('p', recursive=False): + # get data in each p-tag and ignore em and u tags. + data = p.text.strip() + if len(data) == 0: + continue + + # remove trailing : + if data[-1] == ':': + data = data[:-1] + + # now determine if this is a day-label or menu. + if data in self.dow.values(): + # it was a day label, note it down so we know when we see food + last_day = data + continue + + # if we reach here, it was probably food, anything that starts with + # the string "Dagens:" is considered food. + if last_day and data.startswith('Dagens:'): + self.menu[last_day] = [data[7:].strip()] + last_day = None + + return self.menu + + def get_day(self, dow): + """ + Returns the menu, as a list, of the given day, dow, + where 0 is Monday and 6 is Sunday. + """ + # If the menu hasn't been fetched, do it, it will be cached. + if self.menu == {}: + self.get_week() + + dow_name = self.dow[dow] + if dow_name not in self.menu: + return ['404 - Food not found'] + return self.menu[dow_name]