-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
298 lines (236 loc) · 8.01 KB
/
model.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import time
import urllib2
from bs4 import BeautifulSoup
def get_specs(url):
try:
html_text = urllib2.urlopen(url).read()
except urllib2.HTTPError:
return "specs not available"
soup = BeautifulSoup(html_text, 'html.parser')
specs = soup.find_all('div', {'class': 'spec-details'})
all_specs = list()
[all_specs.append(spec.encode('utf-8')) for spec in specs]
return ' '.join(all_specs)
def get_quick_specs(url):
try:
html_text = urllib2.urlopen(url).read()
except urllib2.HTTPError:
return "quick specs not available"
soup = BeautifulSoup(html_text, 'html.parser')
quick_specs = soup.find('p', {'class': 'specs'})
# replace a tag with b make it bold
bold = soup.new_tag('b')
bold.string = quick_specs.a.text
quick_specs.a.insert_before(bold)
# remove a tags
quick_specs.a.extract()
return str(quick_specs)
def get_image_for_item(url):
try:
html_text = urllib2.urlopen(url).read()
except urllib2.HTTPError:
return "image not available"
soup = BeautifulSoup(html_text, 'html.parser')
image = soup.find('a', {'class': 'sku-image'})
a = image['href']
return 'http:'+a
class Model(object):
db = None
data_collection_name = None
items_collection_name = None
results_limit = 10
def __init__(self, config, db):
self.db = db
self.data_collection_name = config['data_collection']
self.items_collection_name = config['items_collection']
self.specs_collection_name = config['specs_collection']
self.config = config
def add_new_item(self, item):
for store in item['stores']:
rec = {
'item_name': item['item_name'],
}
rec.update(store)
self.db.mongodb[self.items_collection_name].insert(rec)
if store['store_name'] == 'skroutz':
skroutz_site_url = store['url']
specs = {
'item_name': item['item_name'],
'specs': get_specs(skroutz_site_url),
'quick_specs': get_quick_specs(skroutz_site_url),
'image': get_image_for_item(skroutz_site_url)
}
self.db.mongodb[self.specs_collection_name].insert(specs)
# specs = {
# 'item_name': item['item_name'],
# 'specs': get_specs('http://www.gsmarena.com/microsoft_lumia_950-7262.php')
# }
# self.db.mongodb['specs'].insert(specs)
from price_crawler import PriceCrawler
pc = PriceCrawler(self.config, self, item['item_name'])
rec = {
'timestamp': time.time(),
'item_name': item['item_name'],
}
try:
rec.update(pc.get_store_price(store['store_name']))
self.add_record(rec)
except TypeError:
pass # no price for store
def add_record(self, record):
self.db.mongodb[self.data_collection_name].insert(record)
def get_all(self):
return self.db.mongodb[self.data_collection_name].find()
def get_all_items(self):
return self.db.mongodb[self.items_collection_name]\
.distinct('item_name')
def get_all_stores(self):
return self.db.mongodb[self.items_collection_name]\
.distinct('store_name')
def get_all_for_store(self, store):
mongo_filter = {
'store_name': store,
}
mongo_projection = {
'store_name': 1,
'item_name': 1,
'timestamp': 1,
'price': 1,
'_id': 0
}
result = self.db.mongodb[self.data_collection_name]\
.find(mongo_filter, mongo_projection)\
.sort([('timestamp', -1)])\
.limit(self.results_limit)
return_list = list(result)
return_list.reverse()
return return_list
def get_item_for_all_stores(self, item):
mongo_filter = {
'item_name': item,
}
mongo_projection = {
'store_name': 1,
'item_name': 1,
'timestamp': 1,
'price': 1,
'_id': 0
}
result = self.db.mongodb[self.data_collection_name]\
.find(mongo_filter, mongo_projection)\
.sort([('timestamp', -1)])\
.limit(self.results_limit)
return_list = list(result)
return_list.reverse()
return return_list
def get_item_for_store(self, item, store):
mongo_filter = {
'store_name': store,
'item_name': item
}
mongo_projection = {
'price': 1,
'_id': 0
}
result = self.db.mongodb[self.data_collection_name]\
.find(mongo_filter, mongo_projection)\
.sort([('timestamp', -1)])\
.limit(self.results_limit)
return_list = list(result)
return_list.reverse()
return return_list
def get_timestamps_for_item(self, item):
mongo_filter = {
'item_name': item,
'store_name': 'skroutz',
}
mongo_projection = {
'timestamp': 1,
'_id': 0
}
return_list = list()
mongo_response = self.db.mongodb[self.data_collection_name]\
.find(mongo_filter, mongo_projection)\
.sort([('timestamp', -1)])\
.limit(self.results_limit)
for item in mongo_response:
return_list.append(item['timestamp'])
return_list.reverse()
return return_list
def get_url_for_item(self, item, store):
mongo_filter = {
'item_name': item,
'store_name': store,
}
mongo_projection = {
'url': 1,
'_id': 0
}
return self.db.mongodb[self.items_collection_name]\
.find(mongo_filter, mongo_projection).next()
def get_stores_for_item(self, item):
mongo_filter = {
'item_name': item
}
mongo_projection = {
'store_name': 1,
'_id': 0
}
result = self.db.mongodb[self.items_collection_name]\
.find(mongo_filter, mongo_projection)
return_list = list()
for item in result:
return_list.append(item['store_name'])
return return_list
def maintenance(self):
now = time.time()
delta = self.config['keep_data_for']
delete_from = now - delta
mongo_filter = {
'timestamp': {'$lte': delete_from}
}
result = self.db.mongodb[self.data_collection_name]\
.delete_many(mongo_filter)
return "deleted {} records".format(result)
def get_specs(self, item):
mongo_filter = {
'item_name': item,
}
mongo_projection = {
'specs': 1,
'_id': 0
}
try:
res = self.db.mongodb[self.specs_collection_name]\
.find(mongo_filter, mongo_projection).next()
return res['specs']
except Exception:
return "specs not available"
def get_quick_specs(self, item):
mongo_filter = {
'item_name': item,
}
mongo_projection = {
'quick_specs': 1,
'_id': 0
}
try:
res = self.db.mongodb[self.specs_collection_name]\
.find(mongo_filter, mongo_projection).next()
return res['quick_specs']
except Exception:
return "quick specs not available"
def get_image(self, item):
mongo_filter = {
'item_name': item,
}
mongo_projection = {
'image': 1,
'_id': 0
}
try:
res = self.db.mongodb[self.specs_collection_name]\
.find(mongo_filter, mongo_projection).next()
return res['image']
except Exception:
return "quick specs not available"