Skip to content

Commit

Permalink
Merge pull request #1 from zozs/master
Browse files Browse the repository at this point in the history
Fix MoP menu (finally). Also add requirements.txt.
  • Loading branch information
Noxet authored Dec 7, 2016
2 parents db3fbca + eff6053 commit 05d1174
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion geoffrey_config.yaml
Original file line number Diff line number Diff line change
@@ -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']
67 changes: 67 additions & 0 deletions menus/bryggan.py
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 4 additions & 2 deletions menus/mop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 05d1174

Please sign in to comment.