-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnutrition.py
231 lines (191 loc) · 6.76 KB
/
nutrition.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# MUST USE FULL PATH FOR CRON JOBS TO WORK
# /home/qingweilan/public_html/cgi-bin
import os
import bs4
import json
import datetime
import argparse
import urllib.request
from urllib.parse import urlparse
from urllib.parse import urlencode
from recipes import Recipes
kCalories = 0
kFatCalories = 1
kVitaminA = 2
kVitaminC = 3
kCalcium = 4
kIron = 5
kTotalFat = 6
kTotalFatPCT = 7
kSaturatedFat = 8
kSaturatedFatPCT = 9
kTransFat = 10
kCholesterol = 11
kCholesterolPCT = 12
kSodium = 13
kSodiumPCT = 14
kTotalCarbohydrate = 15
kTotalCarbohydratePCT = 16
kDietaryFiber = 17
kDietaryFiberPCT = 18
kSugars = 19
kProtein = 20
class NutritionParser:
def __init__(self, recipe):
self.recipe = recipe
def downloadNutritionDataForURL(self, url, shouldSave, filename):
"""
Downloads the nutrition data for 'url' and saves to 'filename'
"""
print(url)
nutrition = ["0"] * (kProtein+1)
nutrition[kVitaminA] = "0%"
nutrition[kVitaminC] = "0%"
nutrition[kCalcium] = "0%"
nutrition[kIron] = "0%"
nutrition[kTotalFat] = "0g"
nutrition[kTotalFatPCT] = "0%"
nutrition[kSaturatedFat] = "0g"
nutrition[kSaturatedFatPCT] = "0%"
nutrition[kTransFat] = "0g"
nutrition[kCholesterol] = "0mg"
nutrition[kCholesterolPCT] = "0%"
nutrition[kSodium] = "0mg"
nutrition[kSodiumPCT] = "0%"
nutrition[kTotalCarbohydrate] = "0g"
nutrition[kTotalCarbohydratePCT] = "0%"
nutrition[kDietaryFiber] = "0g"
nutrition[kDietaryFiberPCT] = "0%"
nutrition[kSugars] = "0g"
nutrition[kProtein] = "0g"
response = urllib.request.urlopen(url)
html = response.read()
soup = bs4.BeautifulSoup(html, 'html.parser')
# find calories
cals = soup.findAll('p', {"class" : "nfcal"})
for cal in cals:
for child in cal.children:
# print(child)
if type(child) is bs4.element.NavigableString:
calories = str(child).strip()
nutrition[kCalories] = calories
else:
childclass = child.get('class')
if childclass != None and len(childclass) != 0 and childclass[0] == "nffatcal":
line = child.text
if "Fat Cal. " in line:
line = line.replace("Fat Cal. ", "")
nutrition[kFatCalories] = line
# find all vitamins
vits = soup.findAll( "span", { "class" : lambda x:
x == "nfvitleft" or x == "nfvitright" } )
for vit in vits:
vitname = ""
vitpct = ""
for child in vit:
if type(child) == bs4.element.Tag:
childclass = child.get('class')
if childclass != None and len(childclass) != 0 and childclass[0] == "nfvitname":
vitname = child.text.strip()
elif childclass != None and len(childclass) != 0 and childclass[0] == "nfvitpct":
vitpct = child.text.strip()
if vitname == "Vitamin A":
nutrition[kVitaminA] = vitpct
elif vitname == "Vitamin C":
nutrition[kVitaminC] = vitpct
elif vitname == "Calcium":
nutrition[kCalcium] = vitpct
elif vitname == "Iron":
nutrition[kIron] = vitpct
# find all other nutrient fields
ps = soup.findAll("p", { "class" : "nfnutrient" })
if len(ps) != 0:
for p in ps:
pct = ""
line = p.text.strip()
for child in p:
if type(child) is bs4.element.Tag:
childclass = child.get('class')
if childclass != None and len(childclass) != 0 and childclass[0] == "nfdvval":
# print("nfdvval", child.text)
pct = child.text
if pct != "":
rp = " " + pct
line = line.replace(rp, "")
if "Total Fat " in line:
line = line.replace("Total Fat ", "")
nutrition[kTotalFat] = line
nutrition[kTotalFatPCT] = pct
elif "Saturated Fat " in line:
line = line.replace("Saturated Fat ", "")
nutrition[kSaturatedFat] = line
nutrition[kSaturatedFatPCT] = pct
elif "Trans Fat " in line:
line = line.replace("Trans Fat ", "")
nutrition[kTransFat] = line
elif "Cholesterol " in line:
line = line.replace("Cholesterol ", "")
nutrition[kCholesterol] = line
nutrition[kCholesterolPCT] = pct
elif "Sodium " in line:
line = line.replace("Sodium ", "")
nutrition[kSodium] = line
nutrition[kSodiumPCT] = pct
elif "Total Carbohydrate " in line:
line = line.replace("Total Carbohydrate ", "")
nutrition[kTotalCarbohydrate] = line
nutrition[kTotalCarbohydratePCT] = pct
elif "Dietary Fiber " in line:
line = line.replace("Dietary Fiber ", "")
nutrition[kDietaryFiber] = line
nutrition[kDietaryFiberPCT] = pct
elif "Sugars " in line:
line = line.replace("Sugars ", "")
nutrition[kSugars] = line
elif "Protein " in line:
line = line.replace("Protein ", "")
nutrition[kProtein] = line
for i in range(0, len(nutrition)):
nutrition[i] = nutrition[i].replace('--', '0')
nutritionJSON = json.dumps(nutrition, separators=(',',':'))
# print(nutritionJSON)
# create file to save to
if shouldSave and filename != None:
file = open(filename, "w")
file.write(nutritionJSON)
file.close()
return nutritionJSON
def downloadNutritionData(self, shouldSave=False, filebase='./nutrition/'):
"""
Downloads the nutrition data for recipe number 'recipe'.
First it checks if the nutrition data exists and is up to date.
If not, then it downloads the nutrition data from the UCLA site.
"""
base = "http://menu.dining.ucla.edu/Recipes/"
url = base + self.recipe + "/1"
filename = None
if shouldSave:
filename = filebase + self.recipe
# check if file exists yet
if os.path.exists(filename) and os.path.isfile(filename):
# if exists, check the time stamp
statinfo = os.stat(filename)
filetime = datetime.datetime.fromtimestamp(statinfo.st_mtime)
servertime = datetime.datetime.now()
td = servertime - filetime
diffseconds = td.seconds
hourseconds = 72000
# if the time difference is greater than 4 days, re-download
if diffseconds >= hourseconds:
return self.downloadNutritionDataForURL(url, shouldSave, filename)
else:
return self.downloadNutritionDataForURL(url, shouldSave, filename)
else:
return self.downloadNutritionDataForURL(url, shouldSave, filename)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("recipe", help="the recipe number you wish to download")
args = parser.parse_args()
parser = NutritionParser(args.recipe)
nutritionJSON = parser.downloadNutritionData()
print(nutritionJSON)