Last active
December 13, 2015 22:59
-
-
Save igneus/47ef9e2b368574dc85f6 to your computer and use it in GitHub Desktop.
My daily task - find some meatless meal in the daily menu of a local pizzeria - automated
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
# encoding: utf-8 | |
from __future__ import unicode_literals | |
import sys, re | |
from bs4 import BeautifulSoup | |
import requests | |
from termcolor import colored | |
MENU_URL = 'http://www.pizzeria-manna.cz/index.asp?id=dennimenu' | |
def meals(soup): | |
""" | |
Yields plaintext one-line meal descriptions. | |
""" | |
for tr in soup.body.find_all('tr'): | |
cells = tr.find_all('td', recursive=False) | |
if len(cells) != 4: | |
continue | |
if not cells[0].get_text().isdigit(): | |
continue | |
text = tr.get_text() | |
text = re.sub(r'\s+', ' ', text.strip()) | |
yield text | |
def vegetarian(meal): | |
""" | |
Says if a given string describing a meal is a description | |
of a vegetarian meal | |
""" | |
deadly_strings = ['kuře', 'vepř', 'hověz', 'slanin', 'šunk', 'boloň', 'losos', 'salám', 'steak', 'krůt', 'krab', 'tuňák', 'ančovič'] | |
m_normalized = meal.lower() | |
for d in deadly_strings: | |
if d in m_normalized: | |
return False | |
return True | |
def highlight(s): | |
return colored(s, 'yellow') | |
# Get menu of Pizzeria Manna | |
response = requests.get(MENU_URL) | |
if response.status_code != 200: | |
sys.stderr.puts('Responded with HTTP status {0}'.format(response.status_code)) | |
exit(1) | |
response.encoding = 'windows-1250' # requests don't guess it correctly | |
html = response.text | |
soup = BeautifulSoup(html, 'html5lib') | |
# filter out meals including dead animals, | |
# print the rest | |
for m in meals(soup): | |
if vegetarian(m): | |
print highlight(m) | |
else: | |
print m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment