forked from Boris-code/feapder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem_buffer.py
More file actions
429 lines (347 loc) · 14.2 KB
/
item_buffer.py
File metadata and controls
429 lines (347 loc) · 14.2 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# -*- coding: utf-8 -*-
"""
Created on 2018-06-19 17:17
---------
@summary: item 管理器, 负责缓冲添加到数据库中的item, 由该manager统一添加。防止多线程同时访问数据库
---------
@author: Boris
@email: boris_liu@foxmail.com
"""
import threading
from queue import Queue
import feapder.utils.tools as tools
from feapder import setting
from feapder.db.redisdb import RedisDB
from feapder.dedup import Dedup
from feapder.network.item import Item, UpdateItem
from feapder.pipelines import BasePipeline
from feapder.pipelines.mysql_pipeline import MysqlPipeline
from feapder.utils import metrics
from feapder.utils.log import log
MYSQL_PIPELINE_PATH = "feapder.pipelines.mysql_pipeline.MysqlPipeline"
class ItemBuffer(threading.Thread):
dedup = None
__redis_db = None
def __init__(self, redis_key, task_table=None):
if not hasattr(self, "_table_item"):
super(ItemBuffer, self).__init__()
self._thread_stop = False
self._is_adding_to_db = False
self._redis_key = redis_key
self._task_table = task_table
self._items_queue = Queue(maxsize=setting.ITEM_MAX_CACHED_COUNT)
self._table_request = setting.TAB_REQUESTS.format(redis_key=redis_key)
self._table_failed_items = setting.TAB_FAILED_ITEMS.format(
redis_key=redis_key
)
self._item_tables = {
# 'item_name': 'table_name' # 缓存item名与表名对应关系
}
self._item_update_keys = {
# 'table_name': ['id', 'name'...] # 缓存table_name与__update_key__的关系
}
self._pipelines = self.load_pipelines()
self._have_mysql_pipeline = MYSQL_PIPELINE_PATH in setting.ITEM_PIPELINES
self._mysql_pipeline = None
if setting.ITEM_FILTER_ENABLE and not self.__class__.dedup:
if setting.ITEM_FILTER_SETTING.get(
"filter_type"
) == Dedup.BloomFilter or setting.ITEM_FILTER_SETTING.get("name"):
self.__class__.dedup = Dedup(
to_md5=False, **setting.ITEM_FILTER_SETTING
)
else:
self.__class__.dedup = Dedup(
to_md5=False,
name=self._redis_key,
**setting.ITEM_FILTER_SETTING,
)
# 导出重试的次数
self.export_retry_times = 0
# 导出失败的次数 TODO 非air爬虫使用redis统计
self.export_falied_times = 0
@property
def redis_db(self):
if self.__class__.__redis_db is None:
self.__class__.__redis_db = RedisDB()
return self.__class__.__redis_db
def load_pipelines(self):
pipelines = []
for pipeline_path in setting.ITEM_PIPELINES:
pipeline = tools.import_cls(pipeline_path)()
if not isinstance(pipeline, BasePipeline):
raise ValueError(f"{pipeline_path} 需继承 feapder.pipelines.BasePipeline")
pipelines.append(pipeline)
return pipelines
@property
def mysql_pipeline(self):
if not self._mysql_pipeline:
self._mysql_pipeline = tools.import_cls(MYSQL_PIPELINE_PATH)()
return self._mysql_pipeline
def run(self):
self._thread_stop = False
while not self._thread_stop:
self.flush()
tools.delay_time(setting.ITEM_UPLOAD_INTERVAL)
self.close()
def stop(self):
self._thread_stop = True
self._started.clear()
def put_item(self, item):
if isinstance(item, Item):
# 入库前的回调
item.pre_to_db()
self._items_queue.put(item)
def flush(self):
try:
items = []
update_items = []
requests = []
callbacks = []
items_fingerprints = []
data_count = 0
while not self._items_queue.empty():
data = self._items_queue.get_nowait()
data_count += 1
# data 分类
if callable(data):
callbacks.append(data)
elif isinstance(data, UpdateItem):
update_items.append(data)
elif isinstance(data, Item):
items.append(data)
if setting.ITEM_FILTER_ENABLE:
items_fingerprints.append(data.fingerprint)
else: # request-redis
requests.append(data)
if data_count >= setting.ITEM_UPLOAD_BATCH_MAX_SIZE:
self.__add_item_to_db(
items, update_items, requests, callbacks, items_fingerprints
)
items = []
update_items = []
requests = []
callbacks = []
items_fingerprints = []
data_count = 0
if data_count:
self.__add_item_to_db(
items, update_items, requests, callbacks, items_fingerprints
)
except Exception as e:
log.exception(e)
def get_items_count(self):
return self._items_queue.qsize()
def is_adding_to_db(self):
return self._is_adding_to_db
def __dedup_items(self, items, items_fingerprints):
"""
去重
@param items:
@param items_fingerprints:
@return: 返回去重后的items, items_fingerprints
"""
if not items:
return items, items_fingerprints
is_exists = self.__class__.dedup.get(items_fingerprints)
is_exists = is_exists if isinstance(is_exists, list) else [is_exists]
dedup_items = []
dedup_items_fingerprints = []
items_count = dedup_items_count = dup_items_count = 0
while is_exists:
item = items.pop(0)
items_fingerprint = items_fingerprints.pop(0)
is_exist = is_exists.pop(0)
items_count += 1
if not is_exist:
dedup_items.append(item)
dedup_items_fingerprints.append(items_fingerprint)
dedup_items_count += 1
else:
dup_items_count += 1
log.info(
"待入库数据 {} 条, 重复 {} 条,实际待入库数据 {} 条".format(
items_count, dup_items_count, dedup_items_count
)
)
return dedup_items, dedup_items_fingerprints
def __pick_items(self, items, is_update_item=False):
"""
将每个表之间的数据分开 拆分后 原items为空
@param items:
@param is_update_item:
@return:
"""
datas_dict = {
# 'table_name': [{}, {}]
}
while items:
item = items.pop(0)
# 取item下划线格式的名
# 下划线类的名先从dict中取,没有则现取,然后存入dict。加快下次取的速度
item_name = item.item_name
table_name = self._item_tables.get(item_name)
if not table_name:
table_name = item.table_name
self._item_tables[item_name] = table_name
if table_name not in datas_dict:
datas_dict[table_name] = []
datas_dict[table_name].append(item.to_dict)
if is_update_item and table_name not in self._item_update_keys:
self._item_update_keys[table_name] = item.update_key
return datas_dict
def __export_to_db(self, table, datas, is_update=False, update_keys=()):
for pipeline in self._pipelines:
if is_update:
if table == self._task_table and not isinstance(
pipeline, MysqlPipeline
):
continue
if not pipeline.update_items(table, datas, update_keys=update_keys):
log.error(
f"{pipeline.__class__.__name__} 更新数据失败. table: {table} items: {datas}"
)
return False
else:
if not pipeline.save_items(table, datas):
log.error(
f"{pipeline.__class__.__name__} 保存数据失败. table: {table} items: {datas}"
)
return False
# 若是任务表, 且上面的pipeline里没mysql,则需调用mysql更新任务
if not self._have_mysql_pipeline and is_update and table == self._task_table:
if not self.mysql_pipeline.update_items(
table, datas, update_keys=update_keys
):
log.error(
f"{self.mysql_pipeline.__class__.__name__} 更新数据失败. table: {table} items: {datas}"
)
return False
self.metric_datas(table=table, datas=datas)
return True
def __add_item_to_db(
self, items, update_items, requests, callbacks, items_fingerprints
):
export_success = True
self._is_adding_to_db = True
# 去重
if setting.ITEM_FILTER_ENABLE:
items, items_fingerprints = self.__dedup_items(items, items_fingerprints)
# 分捡
items_dict = self.__pick_items(items)
update_items_dict = self.__pick_items(update_items, is_update_item=True)
# item批量入库
failed_items = {"add": [], "update": [], "requests": []}
while items_dict:
table, datas = items_dict.popitem()
log.debug(
"""
-------------- item 批量入库 --------------
表名: %s
datas: %s
"""
% (table, tools.dumps_json(datas, indent=16))
)
if not self.__export_to_db(table, datas):
export_success = False
failed_items["add"].append({"table": table, "datas": datas})
# 执行批量update
while update_items_dict:
table, datas = update_items_dict.popitem()
log.debug(
"""
-------------- item 批量更新 --------------
表名: %s
datas: %s
"""
% (table, tools.dumps_json(datas, indent=16))
)
update_keys = self._item_update_keys.get(table)
if not self.__export_to_db(
table, datas, is_update=True, update_keys=update_keys
):
export_success = False
failed_items["update"].append(
{"table": table, "datas": datas, "update_keys": update_keys}
)
if export_success:
# 执行回调
while callbacks:
try:
callback = callbacks.pop(0)
callback()
except Exception as e:
log.exception(e)
# 删除做过的request
if requests:
self.redis_db.zrem(self._table_request, requests)
# 去重入库
if setting.ITEM_FILTER_ENABLE:
if items_fingerprints:
self.__class__.dedup.add(items_fingerprints, skip_check=True)
else:
failed_items["requests"] = requests
if self.export_retry_times > setting.EXPORT_DATA_MAX_RETRY_TIMES:
if self._redis_key != "air_spider":
# 失败的item记录到redis
self.redis_db.sadd(self._table_failed_items, failed_items)
# 删除做过的request
if requests:
self.redis_db.zrem(self._table_request, requests)
log.error(
"入库超过最大重试次数,不再重试,数据记录到redis,items:\n {}".format(
tools.dumps_json(failed_items)
)
)
self.export_retry_times = 0
else:
tip = ["入库不成功"]
if callbacks:
tip.append("不执行回调")
if requests:
tip.append("不删除任务")
exists = self.redis_db.zexists(self._table_request, requests)
for exist, request in zip(exists, requests):
if exist:
self.redis_db.zadd(self._table_request, requests, 300)
if setting.ITEM_FILTER_ENABLE:
tip.append("数据不入去重库")
if self._redis_key != "air_spider":
tip.append("将自动重试")
tip.append("失败items:\n {}".format(tools.dumps_json(failed_items)))
log.error(",".join(tip))
self.export_falied_times += 1
if self._redis_key != "air_spider":
self.export_retry_times += 1
if self.export_falied_times > setting.EXPORT_DATA_MAX_FAILED_TIMES:
# 报警
msg = "《{}》爬虫导出数据失败,失败次数:{},请检查爬虫是否正常".format(
self._redis_key, self.export_falied_times
)
log.error(msg)
tools.send_msg(
msg=msg,
level="error",
message_prefix="《%s》爬虫导出数据失败" % (self._redis_key),
)
self._is_adding_to_db = False
def metric_datas(self, table, datas):
"""
打点 记录总条数及每个key情况
@param table: 表名
@param datas: 数据 列表
@return:
"""
total_count = 0
for data in datas:
total_count += 1
for k, v in data.items():
metrics.emit_counter(k, int(bool(v)), classify=table)
metrics.emit_counter("total count", total_count, classify=table)
def close(self):
# 调用pipeline的close方法
for pipeline in self._pipelines:
try:
pipeline.close()
except:
pass