Created
December 19, 2012 16:03
-
-
Save bmispelon/4337835 to your computer and use it in GitHub Desktop.
Daily menus for restaurants around the office
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://balettcipo.hu/hu/napi-menu | |
| from lxml import html | |
| from textwrap import dedent | |
| import sys | |
| from base import get_weekday | |
| URL = 'http://balettcipo.hu/hu/napi-menu' | |
| def get_weekly_menu(): | |
| """Return a list of all daily menus for the current week.""" | |
| tree = html.parse(URL) | |
| nodes = tree.xpath("//table[@class='foodTable']") | |
| return [dedent(node.text_content()).strip() for node in nodes] | |
| def get_daily_menu(when=None): | |
| """Return the daily menu for the given weekday (or for the current day if no | |
| argument is passed). | |
| """ | |
| day = get_weekday(when) | |
| menus = get_weekly_menu() | |
| return menus[day] | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 1: | |
| when = sys.argv[1] | |
| else: | |
| when = None | |
| print "The daily menu for %s is:" % (when or "today") | |
| print get_daily_menu(when) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import date | |
| def get_weekday(s=None): | |
| """Given a string representation, return the int corresponding to the weekday. | |
| If no argument is passed, return the current day.""" | |
| if s is None: | |
| day = date.today().weekday() | |
| if day > 4: | |
| raise ValueError('There is no daily menus on weekends.') | |
| return day | |
| try: | |
| return { | |
| 'monday': 0, 'mon': 0, | |
| 'tuesday': 1, 'tue': 1, | |
| 'wednesday': 2, 'wed': 2, | |
| 'thursday': 3, 'thu': 3, | |
| 'friday': 4, 'fri': 4, | |
| }[s.lower()] | |
| except KeyError: | |
| raise ValueError('The day "%s" is not supported' % s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import date | |
| from HTMLParser import HTMLParser | |
| import sys | |
| from urllib import urlopen | |
| from base import get_weekday | |
| URL = "http://www.chagallcafe.hu/heti-menu/%s" | |
| HU_WEEKDAYS_ASCII = "hetfo kedd szerda csutortok pentek".split() | |
| class ChagallMenuParser(HTMLParser): | |
| """Parse the page on chagallcafe.hu where the weekly menu is. | |
| It works by looking for a particular header corresponding the day of the week | |
| and putting all the text that follows it into a temporary variable. | |
| When a particular endtag is found, the content of the temporary variable | |
| is concatenated and appended to the list of daily menus. | |
| """ | |
| def reset(self): | |
| self.found_title = False | |
| self.found_ul = False | |
| self.daily_menus = [] | |
| self.temp_daily = [] | |
| HTMLParser.reset(self) | |
| def handle_starttag(self, tag, attrs): | |
| # Look for a weekday header | |
| if tag == 'h2' and dict(attrs).get('class') == "slider_title": | |
| self.found_title = True | |
| if self.found_title and tag == 'ul': | |
| self.found_ul = True | |
| def handle_data(self, data): | |
| if self.found_title and self.found_ul: | |
| data = data.strip() | |
| if data: | |
| self.temp_daily.append(data) | |
| def handle_endtag(self, tag): | |
| if self.found_title and tag == 'div': | |
| self.daily_menus.append('\n'.join(self.temp_daily)) | |
| self.temp_daily = [] | |
| self.found_title = False | |
| if self.found_ul and tag == 'ul': | |
| self.found_ul = False | |
| def get_weekly_menu(): | |
| """Return a list of all daily menus for the current week.""" | |
| day = date.today().weekday() | |
| hu_weekday = HU_WEEKDAYS_ASCII[day] | |
| html = urlopen(URL % hu_weekday).read() | |
| p = ChagallMenuParser() | |
| p.feed(html.decode('utf-8')) | |
| return p.daily_menus | |
| def get_daily_menu(when=None): | |
| """Return the daily menu for the given weekday (or for the current day if no | |
| argument is passed). | |
| """ | |
| day = get_weekday(when) | |
| return get_weekly_menu()[day] | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 1: | |
| when = sys.argv[1] | |
| else: | |
| when = None | |
| print "The daily menu for %s is:" % (when or "today") | |
| print get_daily_menu(when) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from lxml import html | |
| from itertools import dropwhile | |
| from base import get_weekday | |
| import sys | |
| URL = 'http://www.ringcafe.hu/index.php?option=com_content&view=article&id=12&Itemid=2' | |
| WEEKDAYS_HU = u"H\xe9tf\u0151 Kedd Szerda Cs\xfct\xf6rt\xf6k P\xe9ntek".split() | |
| def stripped_lines(txt, splitchar="\n"): | |
| """Return the lines of the given text, stripped of whitespace.""" | |
| for line in txt.split(splitchar): | |
| yield line.strip() | |
| def foo(txt): | |
| DAYS = WEEKDAYS_HU | |
| lines = dropwhile(lambda l: l != DAYS[0], stripped_lines(txt)) | |
| i = 0 | |
| acc = [] | |
| for line in lines: | |
| if not line: | |
| continue | |
| if i < len(DAYS) and line == DAYS[i]: | |
| if i: | |
| yield "\n".join(acc) | |
| acc = [] | |
| i += 1 | |
| else: | |
| acc.append(line) | |
| if acc: | |
| yield "\n".join(acc) | |
| def get_weekly_menu(): | |
| """Return a list of all daily menus for the current week.""" | |
| tree = html.parse(URL) | |
| content = tree.xpath("//div[@id='page']")[0] | |
| raw = content.text_content().strip() | |
| return list(foo(raw)) | |
| def get_daily_menu(when=None): | |
| """Return the daily menu for the given weekday (or for the current day if no | |
| argument is passed). | |
| """ | |
| day = get_weekday(when) | |
| return get_weekly_menu()[day] | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 1: | |
| when = sys.argv[1] | |
| else: | |
| when = None | |
| print "The daily menu for %s is:" % (when or "today") | |
| print get_daily_menu(when) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment