-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
172 lines (139 loc) · 5.48 KB
/
main.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
from HTMLParser import HTMLParser
from Queue import Queue, Empty
from threading import Thread
import requests
__author__ = 'crystal'
class RecipesListParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.ok_recipe_link = False
self.recipe_links = []
def handle_starttag(self, tag, attrs):
tag = tag.lower()
attrs = dict((name.lower(), value) for name, value in attrs)
if tag == 'div' and attrs['class'] == 'recipe-link':
self.ok_recipe_link = True
if tag == 'a' and self.ok_recipe_link:
self.recipe_links.append(attrs['href'])
def handle_endtag(self, tag):
tag = tag.lower()
if tag == 'div' and self.ok_recipe_link:
self.ok_recipe_link = False
class RecipeParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.ok_recipe_link = False
self.recipe_links = []
self.ok_preptime = False
self.ok_servings = False
self.ok_difficulty = False
self.ok_directions = False
self.ok_heading = False
def __getattr__(self, item):
return ''
def handle_starttag(self, tag, attrs):
tag = tag.lower()
attrs = dict((name.lower(), value) for name, value in attrs)
if 'class' not in attrs.keys():
attrs['class'] = ''
if tag == 'h1' and 'title' in attrs['class'].split():
self.ok_heading = True
self.fetch_data_to_field = 'name'
if tag == 'div' and 'group-recipe-serving-size' in attrs['class'].split():
self.ok_servings = True
if tag == 'span' and self.ok_servings:
self.fetch_data_to_field = 'servings'
if tag == 'div' and 'field-field-recipe-directions' in attrs['class'].split():
self.ok_directions = True
if tag == 'ol' and self.ok_directions:
self.fetch_data_to_field = 'directions'
if tag == 'div' and 'field-field-recipe-difficulty' in attrs['class'].split():
self.ok_difficulty = True
if self.ok_difficulty and tag == 'div' and 'field-item' in attrs['class'].split():
self.fetch_data_to_field = 'difficulty'
if tag == 'span' and 'preptime' in attrs['class'].split():
self.ok_preptime = True
self.fetch_data_to_field = 'preptime'
return
# first nested span
if tag == 'span' and self.ok_preptime:
self.ok_preptime = False
self.fetch_data_to_field = None
def handle_endtag(self, tag):
tag = tag.lower()
if tag == 'h1' and self.ok_heading:
self.ok_heading = False
self.fetch_data_to_field = None
if tag == 'span' and self.ok_servings:
self.ok_servings = False
self.fetch_data_to_field = None
if tag == 'ol' and self.ok_directions:
self.ok_directions = False
self.fetch_data_to_field = None
if tag == 'div' and self.ok_difficulty and self.fetch_data_to_field:
self.ok_difficulty = False
self.fetch_data_to_field = None
def handle_data(self, data):
if getattr(self, 'fetch_data_to_field', None):
setattr(self,
self.fetch_data_to_field,
getattr(self, self.fetch_data_to_field, '') + ' ' + data.encode('utf8')
)
def get_recipe_list():
page_number = -1
first_url = None
while True:
page_number += 1
new_url = 'http://www.foodrepublic.com/views/ajax?page=%d&view_name=recipes&view_display_id=panel_pane_5&view_path=recipes&view_base_path=recipes'
resp = requests.get(new_url % page_number)
if resp.status_code != requests.codes.ok:
raise Exception('Hell! got %s', resp.text)
json = resp.json()
if not json['status']:
break
parser = RecipesListParser()
parser.feed(json['display'])
if first_url == parser.recipe_links[:1]:
break
first_url = first_url or parser.recipe_links[:1]
for url in parser.recipe_links:
yield url
def parse_recipe_by_url(input_queue, output_queue):
while True:
recipe_url = input_queue.get()
new_url = 'http://www.foodrepublic.com/%s' % recipe_url
resp = requests.get(new_url)
if resp.status_code != requests.codes.ok:
raise Exception('Hell! got %s', resp.text)
parser = RecipeParser()
parser.feed(resp.text)
input_queue.task_done()
print('- (%d) Recipe %s processed...' % (input_queue.qsize(), recipe_url))
output_queue.put(parser)
def main(num_worker_threads=4):
recipe_urls = Queue()
recipes = Queue()
for i in range(num_worker_threads):
t = Thread(target=parse_recipe_by_url,
kwargs={
'input_queue': recipe_urls,
'output_queue': recipes
}
)
t.daemon = True
t.start()
for recipe_url in get_recipe_list():
recipe_urls.put(recipe_url)
print('+ (%d) Recipe %s queued...' % (recipe_urls.qsize(), recipe_url))
recipe_urls.join()
while True:
try:
item = recipes.get_nowait()
except Empty:
break
print("Name:{0.name}\n"
"Servings:{0.servings}\n"
"Directions:{0.directions}\n"
"LevelofDifficulty:{0.difficulty}\n"
"PrepTime:{0.preptime}\n".format(item))
main()