-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
162 lines (134 loc) · 4.71 KB
/
app.py
File metadata and controls
162 lines (134 loc) · 4.71 KB
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
from flask import Flask, request, jsonify, render_template
import requests
import json
import os
import environ
from pathlib import Path, PurePath
ROOT_PATH = Path.cwd()
ENV_PATH = os.path.join(ROOT_PATH, '.env')
env = environ.Env(
NUTRITIONX_ID=str,
NUTRITIONX_KEY=str,
EDAMAM_ID=str,
EDAMAM_KEY=str,
)
environ.Env.read_env(ENV_PATH)
app = Flask(__name__)
@app.route('/')
def home():
return "<h1>Welcome to Flex's Microservice</h1>"
@app.route('/food_info/<string:meal>/')
def calories(meal):
size = request.args['size'].lower()
headers = {
'x-app-id': env('NUTRITIONX_ID'),
'x-app-key': env('NUTRITIONX_KEY'),
'Content-Type': 'application/json'
}
data = {
"query": meal,
"timezone": "US/Eastern"
}
qty = 0
if size == 'small':
qty = 0.75
elif size == 'medium':
qty = 1
elif size == 'large':
qty = 2
else:
return render_template('size_error.html')
response = requests.post('https://trackapi.nutritionix.com/v2/natural/nutrients/', json=data, headers=headers)
food_data = json.loads(response.text)
try:
if food_data["foods"][0]["nf_sugars"].__class__.__name__ == 'NoneType':
sugars = 0
else:
sugars = int(food_data["foods"][0]["nf_sugars"] / food_data["foods"][0]["serving_qty"] * qty)
calories = int(food_data["foods"][0]["nf_calories"] / food_data["foods"][0]["serving_qty"] * qty)
fats = int(food_data["foods"][0]["nf_total_fat"] / food_data["foods"][0]["serving_qty"] * qty)
protein = int(food_data["foods"][0]["nf_protein"] / food_data["foods"][0]["serving_qty"] * qty)
sodium = int(food_data["foods"][0]["nf_sodium"] / food_data["foods"][0]["serving_qty"] * qty)
carbs = int(food_data["foods"][0]["nf_total_carbohydrate"] / food_data["foods"][0]["serving_qty"] * qty)
thumbnail = food_data["foods"][0]["photo"]["thumb"]
name = food_data["foods"][0]["food_name"]
food_data = {
'thumbnail': thumbnail,
'calories': calories,
'sugars': sugars,
'fats': fats,
'protein': protein,
'sodium': sodium,
'carbs': carbs,
'size': size,
'meal': meal
}
if meal == name:
return jsonify(food_data)
else:
return render_template('meal_not_found.html'), 404
except KeyError:
return render_template('meal_not_found.html'), 404
@app.route('/meals')
def meals():
try:
calorie_max = request.args['calories']
except KeyError:
return render_template('calories_required.html'), 404
try:
excluded = request.args['excluded']
except KeyError:
excluded = 'None'
try:
diet = request.args['diet']
except KeyError:
diet = 'balanced'
try:
restriction = request.args['restriction']
if restriction == 'vegan':
health = 'vegan'
elif restriction == 'vegetarian':
health = 'vegetarian'
else:
health = 'alcohol-free'
except KeyError:
health = 'alcohol-free'
id = env('EDAMAM_ID')
key = env('EDAMAM_KEY')
response = requests.get(f'https://api.edamam.com/search?q=*&app_id={id}&app_key={key}&from=0&to=100&calories=0-{calorie_max}&excluded={excluded}&diet={diet}&health={health}')
meal_data = json.loads(response.text)
meals = {
'params': {
'calorie_max': calorie_max,
'excluded': excluded,
'diet': diet,
'restriction': health
},
'recipes':[]
}
try:
for meal in meal_data['hits']:
name = meal['recipe']['label']
thumbnail = meal['recipe']['image']
url = meal['recipe']['url']
servings = int(meal['recipe']['yield'])
calories_per_serving = int(meal['recipe']['calories'] / servings)
carbs_per_serving = int(meal['recipe']['digest'][1]['total'] / servings)
protein_per_serving = int(meal['recipe']['digest'][2]['total'] / servings)
meals['recipes'].append({
'name': name,
'thumbnail': thumbnail,
'servings': servings,
'url': url,
'calories_per_serving': calories_per_serving,
'carbs_per_serving': carbs_per_serving,
'protein_per_serving': protein_per_serving
})
except KeyError:
return render_template('meal_not_found.html'), 404
return jsonify(meals)
@app.errorhandler(404)
def page_not_found(e):
return render_template('meal_not_found.html'), 404
if __name__ == '__main__':
app.run(debug=True)