-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal draft
154 lines (132 loc) · 6.55 KB
/
final draft
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
import requests
import random
import webbrowser
print('Hello, and welcome to my project!\nToday I am going to help you decide what to eat')
print()
#first create a user profile
name = input('First, what is your name?\n')
#we'll need to check if the user is either vegan/vegetarian/pescatarian
is_pescatarian = 'no'
is_vegetarian = 'no'
is_vegan = 'no'
#we first want to see if they're vegan, as this is the most restrictive diet
is_vegan = input('are you vegan, {}? yes/no\n'.format(name))
if is_vegan == 'no':
is_vegetarian = input('are you vegetarian, {}? yes/no\n'.format(name))
if is_vegetarian == 'no':
is_pescatarian = input('are you pescatarian, then? yes/no\n')
if is_pescatarian == 'no':
print('Ok, you are making my job much easier :)')
is_something_else = 'yes'
#now we want to quantify the user's dietary requirement (we'll need it when choosing a recipe)
def lifestyle_choice():
if is_vegan == 'yes':
return 'Vegan'
elif is_vegetarian == 'yes':
return 'Vegetarian'
elif is_pescatarian == 'yes':
return 'Pescatarian'
elif is_something_else =='yes':
return 'something else'
#function to find a recipe based on a base ingredient:
def recipe_search(ingredient):
app_id = '2f14f3d0'
app_key = 'fe5c53899fe9621e10c1efdd8a1e8010'
result = requests.get('https://api.edamam.com/search?q={}&app_id={}&app_key={}'.format(ingredient, app_id,app_key))
data = result.json()
return data['hits']
#we can check the type of information by running e.g.
#print(recipe_search('milk'))
#create lists of what we find - the full recipies which will be saved in a text file,
#and a list of the names from which we will choose later
recipe_list = []
label_list = []
uri_list = []
sugar_list = []
calories_list = []
#create a function that runs through all the recipies that contain this ingredient and will make orderred lists of
#the label, uri, sugar and caloric intake
def run():
ingredient = input('What are you craving today?\n')
print('Here is what I found according to your dietary requirements:')
results = recipe_search(ingredient)
sugar_total = 100000 #assign these variables some random 'ceiling' numbers that we will compare our values with
calories_total = 1000000
def recipe_output(recipe):
print(recipe['label'])
print('This meal is {}'.format(lifestyle_choice()))
label_list.append(recipe['label'])
uri_list.append(recipe['uri'])
recipe_list.append(recipe['label'])
recipe_list.append('You can find the full recipe at:' + recipe['uri'])
print('you can find the recipe at {}'.format(recipe['uri']))
#print('the type of dish is {}'.format(recipe['dishType'][0])) this raises some errors
print('which belongs to the {} cuisine'.format(recipe['cuisineType'][0]))
if recipe['dietLabels']:
print('this dish is ' + str(recipe['dietLabels'][0]))
totalNutrients_dict = recipe['totalNutrients']
for nutrient in totalNutrients_dict:
print(str(totalNutrients_dict[nutrient]['label']) + ': ' + str(
totalNutrients_dict[nutrient]['quantity']) + ' ' + str(totalNutrients_dict[nutrient]['unit']))
for other_ingredients in recipe['ingredientLines']:
recipe_list.append(other_ingredients)
recipe_list.append('\n')
def calories(recipe):
quantity = float(recipe['totalWeight'])
caloric_amount = float(recipe['calories'])
caloric_amount_normalized = caloric_amount / quantity * 100
calories_list.append(caloric_amount_normalized)
return caloric_amount_normalized
def sugar(recipe):
quantity = float(recipe['totalWeight'])
totalNutrients_dict = recipe['totalNutrients']
sugar_amount_normalized = float(totalNutrients_dict["SUGAR"]['quantity']) / quantity * 100
sugar_list.append(sugar_amount_normalized)
return sugar_amount_normalized
for result in results:
recipe = result['recipe']
healthLabels_list = recipe['healthLabels']
if lifestyle_choice() in ['Vegan', 'Vegetarian', 'Pescatarian'] and (lifestyle_choice() in healthLabels_list):
recipe_output(recipe)
caloric_amount = calories(recipe)
sugar_amount = sugar(recipe)
print('this recipe contains {} calories per 100g'.format(caloric_amount))
print('this recipe contains {} g of sugar per 100g'.format(sugar_amount))
calories_total = min(caloric_amount, calories_total)
sugar_total = min(sugar_amount, sugar_total)
print()
elif lifestyle_choice() == 'something else':
recipe_output(recipe)
caloric_amount = calories(recipe)
sugar_amount = sugar(recipe)
print('this recipe contains {} calories per 100g'.format(caloric_amount))
print('this recipe contains {} g of sugar per 100g'.format(sugar_amount))
calories_total = min(caloric_amount, calories_total)
sugar_total = min(sugar_amount, sugar_total)
print()
if sugar_total != 100000 and len(sugar_list) == len(label_list): #apparently some ingredients have more than one recipe with the same name
print('The recipe lowest in sugar is {} with {} grams of sugar per 100g'.format(label_list[sugar_list.index(sugar_total)], sugar_total))
print('The recipe lowest in calories is {} with {} calories per 100g'.format(label_list[calories_list.index(calories_total)], calories_total))
elif sugar_total != 100000 and len(sugar_list)!=len(label_list):
print('There is an error with edamam and I cannot check accurate information about a dishes with the same name')
else:
print('We are sorry, no such recipies exist :(')
#now writing the file:
with open('{} recipes.txt'.format(ingredient), 'w+') as text_thing:
for element in recipe_list:
text_thing.write(element + '\n')
#option to choose a recipe at random
def recipe_choice():
if label_list !=[]:
if input('\nwould you like me to choose a recipe for you to make? yes/no\n') == 'yes':
your_choice = str(random.choice(label_list))
position = label_list.index(your_choice)
your_choice_uri = uri_list[position]
print('\nToday you should eat ' + your_choice)
print(your_choice_uri)
if input('\nwould you like me to open it in the browser? yes/no\n')=='yes':
webbrowser.open(your_choice_uri)
print('\nHave a great day!')
#relax and enjoy the functions
run()
recipe_choice()