This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniFSChecker.py
executable file
·252 lines (178 loc) · 5.5 KB
/
MiniFSChecker.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
from Messages import INFO
import sys, re, Common
from Yemek import Yemek, Carb
from urllib.request import urlopen as uopen
from urllib.error import URLError
from bs4 import BeautifulSoup as bs
class HTMLMethods:
@staticmethod
def toHTMLChars(query):
replacemap = {}
replacemap[" "]="+"
replacemap["'"]="%27"
for key in replacemap:
query = query.replace(key, replacemap[key])
return query
class FHandler:
base_url = "http://www.fatsecret.co.uk"
mobile_url = "http://m.fatsecret.co.uk"
query_url = base_url + "/calories-nutrition/search?q="
def __init__(self, query, foodobj=0):
INFO("\r\tChecking online...", end=' ')
self.query = HTMLMethods.toHTMLChars(query)
try:
self.pagedata = uopen(FHandler.query_url+self.query).read()
except URLError:
print(" stopped, no connection?")
exit(-1)
# # offline saved
# print(self.pagedata)
# exit(0)
# self.pagedata = open("test_sub.html").read()
self.results = self.ParseResults()
if foodobj==0:
self.found = self.resHandler()
else:
#Check current food obj against results list
self.found = self.checkFoodHomology(foodobj)
def checkFoodHomology(self,fobj):
if len(self.results)==0:
RESULT("No matches")
return -1
good_results = []
for fo in self.results:
#ignore meaningless foods
if fo.kC==0:
continue
diff = fobj.kC - fo.kC
scale = float(fobj.kC)/float(fo.kC)
if diff < 0:diff *=-1
if diff < 10:
good_results.append((fo,scale))
continue
th_prot = fobj.prot
th_fat = fobj.fat
if th_prot==0:th_prot=0.00001
if th_fat==0:th_fat=0.00001
my_prot = fo.prot
my_fat = fo.fat
if my_prot==0:my_prot=0.00001
if my_fat==0:my_fat=0.00001
my_rat = my_prot/my_fat
th_rat = th_prot/th_fat
diff = th_rat - my_rat
if diff<0:diff*=-1
if diff < 0.1:
good_results.append((fo,scale))
return Common.choice(good_results, fobj)
def resHandler(self, max_split=30):
if len(self.results)==0:
RESULT("No matches")
return -1
return Common.choice(self.results)
@staticmethod
def handleFoodInfo(food_data):
#Header indexes and make a column map
headers = [tr.find('td', {'class':'label borderTop'}) for tr in food_data.table.find_all('tr')]
fact_map = {
'carbohydrate' : (0,'g'),
'fibre' : (0,'g'),
'sugar' : (0,'g'),
'calories': (0,'kC'),
'protein' : (0,'g'),
'fat' : (0,'g')
}
for t in range(len(headers)):
tag = headers[t]
if tag == None:
continue
value = tag.findNext('td')
try:
value = value.b.text.strip()
except AttributeError:
value = value.text.strip()
fact_map[tag.text.lower().strip()] = Common.amountsplit( value )
# Serving size
fact_map['serving'] = Common.amountsplit(
food_data.table.find('td',attrs={'class':'title'}).findNext('tr').td.text.split('Size:')[-1],
resolve_unit=False
)
car = Carb(
fact_map['carbohydrate'][0],
fact_map['fibre'][0],
fact_map['sugar'][0]
)
return (fact_map['calories'][0], car, fact_map['protein'][0], fact_map['fat'][0],
fact_map['serving'][0], fact_map['serving'][1])
@staticmethod
def handlePortionData(portion_data):
res = [
(tr.td.div.a.text, int(tr.td.findNext('td').div.a.text) )\
for tr in portion_data.find_all('tr')\
if tr.find('div',{'class':'other-link'})\
]
return res
@staticmethod
def __debugDump(text, dest):
with open(dest,'w') as file:file.write(str(text));file.close();
RESULT(" -> page dumped to", dest)
@staticmethod
def getFoodInfo(url):
try:
newurl = FHandler.mobile_url + url
tempdata = uopen(newurl).read()
#tempdata = open('test_foodinfo.html','r').read()
bsobj = bs(tempdata,'html.parser')
name = bsobj.body.find('div', attrs={'class':'page-title'}).text.strip(' \\r\\t\\n').lower()
except URLError:
RESULT(" stopped, no connection?")
FHandler.__debugDump(tempdata, '/tmp/debug_foodinfo.no_connection.html')
exit(-1)
except AttributeError:
RESULT(" stopped, page is being redirected?")
FHandler.__debugDump(tempdata, '/tmp/debug_foodinfo.redirected.html')
exit(-1)
food_tagline = bsobj.find('div', attrs={'class':'page-info-text'}).text.strip(' \\r\\t\\n').lower()
food_table = bsobj.find('div', attrs={'class':'nutpanel'})
food_info = FHandler.handleFoodInfo(food_table)
name = ' '.join(name.split())
yem = Yemek(name,
food_info[0], food_info[1], food_info[2],
food_info[3], food_info[4], food_info[5])
#import pdb; pdb.set_trace()
section_titles = bsobj.body.find_all('div', {'class':'section-title'})
portion_data = [x for x in section_titles if x.text == "Common serving sizes"]
if len(portion_data)>0:
portion_data = portion_data[0]
portion_info = FHandler.handlePortionData(portion_data.findNext('table'))
if portion_info!=-1:
for key,val in portion_info:
yem.portions.insert(key,val)
return yem
@staticmethod
def tryFacts(meta):
try:
n,a = meta.split("per ")
except ValueError:
return -1
def ParseResults(self, num=30):
bsobj = bs(self.pagedata, "html.parser")
bsurls = bsobj.body.find_all('a', attrs={'class':'prominent'});
res = []
for meta in bsurls:
url = meta.attrs['href']
text = meta.findNext('div', attrs={'class':'greyLink'}).text
try:
n,a, = text.split('per ')
except IndexError:
# Don't parse urls without proper parts
continue
result = FHandler.getFoodInfo(url)
print("\r\t\t\t", len(res)+1, end=' ', file=sys.stderr)
res.append(result)
# count
num -= 1
if num==0: break
return res
#f = FHandler("milk")