-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_allrecipes_html.py
219 lines (164 loc) · 6.7 KB
/
parse_allrecipes_html.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
from __future__ import division
import re
import datetime
import json
import glob
import os
import multiprocessing
import argparse
import hashlib
import random
from bs4 import BeautifulSoup
from pyparsing import Optional, Group, Word, nums, Literal
from tqdm import tqdm
import numpy as np
from toolbox.strings import pad_left
def autoconvert(s):
try:
return int(s)
except ValueError:
try:
return float(s)
except ValueError:
return s
class ExtendedEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.timedelta):
# NOTE: uses default string representation, e.g. "00:30:00"
return str(obj)
return json.JSONEncoder.default(self, obj)
# Integer numbers
number = Word(nums)
# Optional hour part
hour_symbol = Literal('h')
hour = Group(number + hour_symbol)('hour').setParseAction(lambda s,l,t: int(t[0][0][0]))
# Optional minute part
minute_symbol = Literal('m')
minute = Group(number + minute_symbol)('minute').setParseAction(lambda s,l,t: int(t[0][0]))
# Put the hours and minutes together
duration = Optional(hour) + Optional(minute)
# Convert resulting dictionaries into datetime.timedelta
def timedict_to_timedelta(d):
hours = d['hour'] if 'hour' in d else 0
minutes = d['minute'] if 'minute' in d else 0
return datetime.timedelta(hours=hours, minutes=minutes)
# Put everything together: parse string and output a timedelta
duration_parser = lambda s: timedict_to_timedelta(duration.parseString(s).asDict())
def dict_to_string(d):
return ''.join([str(value) for value in d.itervalues()])
def hash_dict(d):
return hashlib.md5(dict_to_string(d)).hexdigest()
def parse_ingredients(li):
ingredients = []
for el in li:
label = el.find('label', attrs={'ng-class': "{true: 'checkList__item'}[true]"})
if label:
span = label.find('span')
ingredient = dict()
ingredient['id'] = int(span['data-id'])
ingredient['name'] = span.text
ingredients.append(ingredient)
return ingredients
def parse_file(filename):
fp = open(filename)
soup = BeautifulSoup(fp, 'html5lib')
# Get ID from filename
basename = os.path.basename(filename)
id_ = os.path.splitext(basename)[0]
recipe = {'id': int(id_)}
name = soup.find('h1', itemprop='name').text
recipe['name'] = name
ingredients = []
li = soup.find('ul', id="lst_ingredients_1")('li')
ingredients = parse_ingredients(li)
li = soup.find('ul', id='lst_ingredients_2')('li')
ingredients2 = parse_ingredients(li)
ingredients.extend(ingredients2)
recipe['ingredients'] = ingredients
yield_ = soup.find('meta', itemprop='recipeYield')
recipe['yields'] = int(yield_['content']) if yield_ is not None else None
cal = soup.find('span', class_='calorie-count')
recipe['calories'] = int(cal.find('span').text) if cal is not None else None
nut = soup.find('h3', text='Nutrition')
nutrients = dict()
if nut:
for ul in nut.find_next_siblings(class_='nutrientLine'):
try:
nutrient_type = ul.find('li').text.rstrip(': ')
amount = ul.find('li', class_='nutrientLine__item--amount')
nutrients[nutrient_type] = autoconvert(amount.find('span').text)
except:
continue
recipe['nutrients'] = nutrients if nutrients else None
prep = soup.find('span', class_='ready-in-time')
recipe['preparation_time'] = duration_parser(prep.text) if prep else None
prep_root = soup.find('ul', class_='prepTime')
if prep_root: # Has time information
preptime = prep_root.find('time', itemprop='prepTime')
recipe['preparation_time'] = duration_parser(preptime.text).seconds if preptime else None
cooktime = prep_root.find('time', itemprop='cookTime')
recipe['cooking_time'] = duration_parser(cooktime.text).seconds if cooktime else None
totaltime = prep_root.find('time', itemprop='totalTime')
recipe['total_time'] = duration_parser(totaltime.text).seconds if totaltime else None
start_number = re.compile(r'(\d+).*?')
try:
rating_stars = soup.find('section', id='reviews').find('ol').find_all('li')
assert len(rating_stars) == 6, "Expected 5 degrees of ratings and a total count, for a total of 6, but got %d" % len(rating_stars)
recipe['rating_count'] = int(rating_stars.pop(0).text.rstrip(' Ratings'))
ratings = dict()
for idx, degree in enumerate(rating_stars):
stars_title = degree.div['title']
number_of_stars = str(5 - idx)
ratings[number_of_stars + ' stars'] = int(start_number.match(stars_title).group(1))
recipe['ratings'] = ratings
except Exception:
pass
directory = random_dir(args.output)
np.savez_compressed(os.path.join(directory, str(recipe['id'])), recipe)
def split_list(l, n):
"""
Split list into n smaller lists, not preserving ordering.
"""
res = [[] for i in xrange(n)]
x = 0
while x < len(l):
for sublist_idx in xrange(n):
if x == len(l):
break
res[sublist_idx].append(l[x])
x = x + 1
return res
def wrapper(filename):
try:
parse_file(filename)
except Exception as err:
print('Error parsing {0}: {1}'.format(filename, err))
def random_dir(root):
dirsize = 469
strlen = len(str(dirsize))
rand1 = random.randint(0, dirsize) # Using sqrt(number_files)
dir1 = pad_left(str(rand1), '0', strlen)
rand2 = random.randint(0, dirsize)
dir2 = pad_left(str(rand2), '0', strlen)
level1 = os.path.join(os.path.abspath(root), dir1)
level2 = os.path.join(level1, dir2)
if not os.path.exists(level1):
os.makedirs(level1)
if not os.path.exists(level2):
os.makedirs(level2)
return level2
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process recipe files')
parser.add_argument('input', type=str, help='input directory')
parser.add_argument('output', type=str, help='output file')
parser.add_argument('--pool-size', '-p', type=int, help='pool size (= number of workers)')
parser.add_argument('--chunk-size', '-c', type=int, default=1, help='chunk size (= worker batch size)')
args = parser.parse_args()
recipes = []
p = multiprocessing.Pool(args.pool_size)
# Parse all HTML files in target folder
filenames = glob.glob(args.input + '/*.html')
print("Got %d files." % len(filenames))
for result in tqdm(p.imap_unordered(wrapper, filenames, chunksize=args.chunk_size), total=len(filenames)):
pass
# p.map_async(parse_file, filenames)#, chunksize=args.chunk_size)