-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recipe.py
executable file
·112 lines (99 loc) · 3.48 KB
/
Recipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from BeautifulSoup import BeautifulSoup
import urllib2
import re
import IngredientParser
class Recipe:
ingredients = []
directions = []
class Ingredient:
quantity = 1.0 #e.g. 1
unit = 'unit' #e.g. tablespoon
meat = False
name = 'unknown' #e.g. salt
def FetchRecipe(url):
try:
usock = urllib2.urlopen(url)
html_data = usock.read()
usock.close()
except ValueError:
return None
#Create the soup object from the HTML data
soup = BeautifulSoup(html_data)
recipeFromURL = Recipe()
ingredient_tags = soup.findAll('li',attrs={"class" : "plaincharacterwrap ingredient"})
ingredients = [tag.contents[0] for tag in ingredient_tags]
#Format the ingredients to remove \r\n and leading spaces
#Assign the formatted ingredients to
regex = '\\r\\n.[ ]+'
ingparser = IngredientParser.IngredientParser()
for ingredient_name in ingredients:
t = re.sub(regex, '', ingredient_name).lower()
if t.find(":") == -1:
recipeFromURL.ingredients.append(ingparser.CreateIngredientFromString(t))
#Directions
directions_tags = soup.findAll('div', attrs={"class" : "directions"})
directions_tags = directions_tags[0]('span', attrs={"class" : "plaincharacterwrap break"})
directions = [tag.contents[0] for tag in directions_tags]
regex = '\\r\\n.[ ]+'
for step_in_directions in directions:
recipeFromURL.directions.append(
re.sub(regex, '', step_in_directions).lower()
)
return recipeFromURL
def PrintRecipe(recipe):
print 'INGREDIENTS\n',
print '-------------\n',
for cur_ingredient in recipe.ingredients:
print 'Quantity:',
print cur_ingredient.quantity,
print 'Unit:',
print cur_ingredient.unit,
print 'Name:',
print cur_ingredient.name,
print 'Meat:' ,
print cur_ingredient.meat,
print '\n',
print '\n',
print 'DIRECTIONS\n',
print '-------------\n',
count = 1
for cur_direction in recipe.directions:
print 'Step ',
print count,
print ': ',
print cur_direction,
print '\n',
count = count + 1
return None
def IsVeggie(recipe):
for ing in recipe.ingredients:
if ing.meat:
return False
return True
def VegetarianVersion(recipe):
#Replace the meat ingredients with a random vegitarian substitute
<<<<<<< HEAD
meat_subs = eval(open("meat_subs.txt",'r').read())
for cur_ingredient in recipe.ingredients:
if cur_ingredient.meat:
cur_ingredient.name = meat_subs[cur_ingredient.name]
=======
my_dict = eval(open("meats.txt").read())
#normalize dictionary keys
for k in my_dict.keys():
my_dict[k.strip().lower()] = my_dict[k]
for cur_ingredient in recipe.ingredients:
if cur_ingredient.meat == True:
oldname = cur_ingredient.name
if cur_ingredient.name in my_dict.keys():
cur_ingredient.name = my_dict[cur_ingredient.name]
else:
cur_ingredient.name = 'meatless ' + cur_ingredient.name
>>>>>>> 0ffcec3af9cd1b9c5f5f1a37b0abf7c2c4ed09fc
cur_ingredient.meat = False
#update directions
new_directions = []
for d in recipe.directions:
new_directions.append(d.replace(oldname, cur_ingredient.name))
recipe.directions = new_directions;
return recipe