-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
219 lines (168 loc) · 6.67 KB
/
helpers.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# import lib
import sys
import json
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
from flask import redirect, render_template, session
from functools import wraps
def error(error_message, code=400):
"""Render message as an apology to user."""
return render_template("error.html", error_message=error_message), code
def success(success_message):
"""Render message as an apology to user."""
return render_template("success.html", success_message=success_message)
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/0.12/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
def getProtein(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1003:
return nutrition.get('value')
def getFat(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1004:
return nutrition.get('value')
def getCarbs(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1005:
return nutrition.get('value')
def getCalorie(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1008:
return nutrition.get('value')
def getSugar(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 2000:
return nutrition.get('value')
def getFiber(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1079:
return nutrition.get('value')
def getWater(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1051:
return nutrition.get('value')
def getCa(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1087:
return nutrition.get('value')
def getFe(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1089:
return nutrition.get('value')
def getMg(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1090:
return nutrition.get('value')
def getP(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1091:
return nutrition.get('value')
def getK(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1092:
return nutrition.get('value')
def getNa(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1093:
return nutrition.get('value')
def getZn(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1095:
return nutrition.get('value')
def getCu(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1098:
return nutrition.get('value')
def getSe(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1103:
return nutrition.get('value')
def getVitaminA(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1106:
return nutrition.get('value')
def getVitaminA1(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1105:
return nutrition.get('value')
def getVitaminB6(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1175:
return nutrition.get('value')
def getVitaminB12(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1178:
return nutrition.get('value')
def getVitaminC(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1162:
return nutrition.get('value')
def getVitaminD(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1114:
return nutrition.get('value')
def getVitaminE(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1109:
return nutrition.get('value')
def getVitaminK(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1185:
return nutrition.get('value')
def getFattedAcidSaturated(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1258:
return nutrition.get('value')
def getFattyAcidMonounsaturated(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1292:
return nutrition.get('value')
def getFattyAcidPolyunsaturated(foodNutritionlist):
for nutrition in foodNutritionlist:
if nutrition.get('nutrientId') == 1293:
return nutrition.get('value')
def getSearchList(foodname):
"""
provding a foodlist based on what's search,
space: %20 replace
"""
foodname = " ".join(foodname.strip().split())
foodname = foodname.replace(" ", "%20")
try:
api_key = 'tdHWwZ6dPvp170l950UUc9Yk4DypbaIh3fwlM746'
html = urlopen("https://api.nal.usda.gov/fdc/v1/foods/search?api_key={}&query={}&dataType=Survey%20%28FNDDS%29".format(api_key, foodname))
# bs parsing html file, whether XML or JSON
bs = BeautifulSoup(html, 'html.parser')
# json.loads only for str file
FoodDict = json.loads(str(bs))['foods']
return FoodDict
except (requests.RequestException, ValueError, KeyError, IndexError):
return None
def unit(value, unitname):
if unitname.lower() == 'g':
return f"{value:,.2f}g"
if unitname.lower() == 'mg':
return f"{value:,.2f}mg"
if unitname.lower() == 'ml':
return f"{value:,.2f}ml"
if unitname.lower() == 'kcal':
return f"{value:,.2f}KCAL"
def g(value):
"""format as g"""
return f"{value:,.2f}g"
def kcal(value):
"""format as kcal"""
return f"{value:,.2f}kcal"
def percentage(value):
"""Format value as %."""
return f"{(100 * value):,.2f}%"