forked from richchang0/337RecipeTransform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe_run.py
195 lines (177 loc) · 6.3 KB
/
recipe_run.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import recipe_parser, database, recipe_methods, pprint
from termcolor import colored
#model of recipe dictionary
#
#recipe ={
# "ingredients": [{
# "name": "",
# "quantity": "",
# "measurement": "",
# "descriptor": "",
# "preparation": "",
# "category": ""
# }
# ],
# "cooking method": "",
# "cooking tools": [],
# "name": "",
# "directions": [],
# "intermediate methods": ""
#}
recipe = {'ingredients':[]}
transform_codes = {
'v':'Vegetarian',
'nv':'Non-Vegetarian',
'h':'Healthier',
'lh':'Less Healthy',
'a':'American',
'e':'East-Asian',
's':'Spanish',
'in':'Indian',
'it':'Italian'}
def ingredient_display(ingredient):
out = ['{0:.2f}'.format(ingredient['quantity'])]
if out[0][-3::]=='.00':
out[0] = out[0][0:-3]
elif out[0][-3::]=='.25':
if out[0][0]=='0':
out[0] = '1/4'
else:
out[0] = out[0][0:-3]+' 1/4'
elif out[0][-3::]=='.33':
if out[0][0]=='0':
out[0] = '1/3'
else:
out[0] = out[0][0:-3]+' 1/3'
elif out[0][-3::]=='.50':
if out[0][0]=='0':
out[0] = '1/2'
else:
out[0] = out[0][0:-3]+' 1/2'
elif out[0][-3::]=='.67':
if out[0][0]=='0':
out[0] = '2/3'
else:
out[0] = out[0][0:-3]+' 2/3'
elif out[0][-3::]=='.75':
if out[0][0]=='0':
out[0] = '3/4'
else:
out[0] = out[0][0:-3]+' 3/4'
if ingredient['measurement'] != '' and ingredient['quantity']>1:
out.append(ingredient['measurement']+'s')
elif ingredient['measurement']!= '':
out.append(ingredient['measurement'])
if ingredient['descriptor'] != '':
out.append(ingredient['descriptor'])
out.append(ingredient['name'])
if ingredient['preparation'] != '':
out.append('-')
out.append(ingredient['preparation'])
return out
def make_recipe_dict(url):
recipe = {'ingredients':[],'cooking tools':[]}
contents = recipe_parser.http_string(url)
recipe['name'] = recipe_parser.recipe_name(url)
recipe['directions'] = recipe_parser.directions(contents)
ingredients = recipe_parser.ingredients(contents)
for ingredient in ingredients:
[iname,quantity,measure,descriptor,prep,category] = ingredient
recipe['ingredients'].append({'name':iname.lower(),'quantity':quantity,'measurement':measure,'descriptor':descriptor,'preparation':prep, 'category':category})
#find all tools implied by prep fields, add to 'cooking tools', (TODO:)and maybe add actions to 'intermediate methods'
tool = database.find_prep_tool_for_action(prep)
if tool != 'notfound' and tool not in recipe['cooking tools']:
recipe['cooking tools'].append(tool)
#find all tools implied by actions in directions, adding where appropriate
#find all tools mentioned in directions, add to 'cooking tools', and maybe add actions to 'intermediate methods'
recipe['cooking tools'].extend(database.detect_tools(recipe['directions']))
#make 'cooking tools' a set
recipe['cooking tools']= list(set(recipe['cooking tools']))
#find cooking method and add to 'cooking method'
#start with the directions and then move on to the tools if still not found
methods = recipe_methods.getMethods(url)
if methods ==[]:
for tool in recipe['cooking tools']:
method = database.find_method_from_tool(tool)
if method != 'notfound':
recipe['cooking method'] = method
else:
#grab last one because it is most likely to be the main cooking method
recipe['cooking method'] = methods[-1]
return recipe
def print_recipe(recipe):
title_color = "green"
print
print colored(recipe['name'],title_color)
print '-----------------------'
print
print colored('Ingredients',title_color)
print '------------'
for ingredient in recipe['ingredients']:
out = ingredient_display(ingredient)
print ' '.join(out)
print
print colored('Directions',title_color)
print '------------'
for (num,step) in enumerate(recipe['directions']):
print '{}. {:<30}'.format(str(num+1),step)
print
def print_transform_prompt():
#print table of transforms and codes
print
print '{:<18} {:<18}'.format('Transform','Code')
print '{:<18} {:<18}'.format('------------','------------')
for code in sorted(transform_codes.keys()):
print '{:<18} {:<18}'.format(transform_codes[code],code)
print
def transform_recipe(recipe, transform):
replacement_names = {}
for ingredient in recipe["ingredients"]:
if ingredient["category"] != False:
original_name = ingredient["name"]
if transform=="v":
ingredient["name"]=database.to_vegetarian(ingredient["name"])
elif transform=="nv":
ingredient["name"]=database.to_meat(ingredient["name"])
elif transform=="h":
ingredient["name"]=database.to_healthy(ingredient["name"])
elif transform=="lh":
ingredient["name"]=database.to_unhealthy(ingredient["name"])
elif transform=="a":
ingredient["name"]=database.to_cuisine("american",ingredient["name"])
elif transform=="e":
ingredient["name"]=database.to_cuisine("east-asian",ingredient["name"])
elif transform=="s":
ingredient["name"]=database.to_cuisine("spanish",ingredient["name"])
elif transform=="in":
ingredient["name"]=database.to_cuisine("indian",ingredient["name"])
elif transform=="it":
ingredient["name"]=database.to_cuisine("italian",ingredient["name"])
##if there was a change, color the text red
if ingredient["name"] != original_name:
ingredient["name"] = colored(ingredient["name"],"red")
ingredient["descriptor"] = ''
replacement_names[original_name] = ingredient["name"]
ordering = []
for ingredient in replacement_names.keys():
length = len(ingredient.split(' '))
ordering.append([length,ingredient])
ordering = sorted(ordering,reverse=True)
#from longest ingredient name to shortest, replace the name with the new ingredient in each direction step
for [num, ingredient] in ordering:
for (count,step) in enumerate(recipe['directions']):
recipe['directions'][count] = step.replace(ingredient,replacement_names[ingredient])
return recipe
def pretty_print_dict(dictionary):
pprint.PrettyPrinter(indent=4).pprint(dictionary)
def main ():
url = raw_input('Enter URL to an AllRecipes recipe: ')
recipe = make_recipe_dict(url)
print_recipe(recipe)
print_transform_prompt()
transform = raw_input('How would you like to change ' + recipe["name"] + ' (enter code): ')
recipe = transform_recipe(recipe, transform)
recipe["name"] = transform_codes[transform] + " "+ recipe["name"]
print_recipe(recipe)
# pretty_print_dict(recipe)
#main()