spider-all-task.js
3.12 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
const _ = require('lodash');
const spider = require('../libs/spider');
const report = require('../libs/report');
const sknsData = require('../data/skns.json');
const legoData = require('../data/lego.json');
const {mysqlPool} = require('../libs/mysql');
const {logger} = require('../libs/logger');
const {sendMessage} = require('../libs/influx-report');
const {ProductRelationModel} = require('../models');
const {getSpecialSizeProducts, getSize} = require('../utils');
module.exports = async(time) => {
const specialSizeProducts = await getSpecialSizeProducts();
const sizeData = await mysqlPool.query('select `size_name`,min(`id`) as id from `size` where `size_name` <> \'-\' group by `size_name`');
const sizeRelation = {};
sizeData.forEach(size => {
sizeRelation[_.trim(size.size_name)] = {
sizeId: size.id
};
});
const allProducts = await ProductRelationModel.findAll();
const duProducts = allProducts.filter(p => _.has(p, 'third.du.productId'));
spider(duProducts.map(p => _.get(p, 'third.du.productId')), '', {}, 1)
.forEach(promise => promise.then(result => {
if (result.status !== 200 && !_.has(result, 'data.detail')) {
logger.warn(`[spider-task]爬取失败记录: ${JSON.stringify(result)}`);
return;
}
const {data: {detail, sizeList}} = result;
const yhProduct = duProducts.find(p => _.get(p, 'third.du.productId') === detail.productId);
const hrtime = process.hrtime();
const timestamp = parseInt(Date.now() / 1000, 10) * 1000000000;
const inSkns = sknsData.some(s => s.yhId === yhProduct.productId);
const isSpecialSize = Boolean(specialSizeProducts[yhProduct.productId]);
sizeList.forEach(s => {
if (_.isPlainObject(s.item) && s.item.price > 0) {
if (sizeList.length === 1) {
const isLego = legoData.some(lego => lego.productId === detail.productId);
if (isLego) {
s.size = 'F';
console.log(`is lego size = F, ${detail.productId}`);
}
}
const find = getSize(s.size, sizeRelation, isSpecialSize);
if (!find) {
return logger.error(`[spider-task]爬取尺码对应关系未找到: ${JSON.stringify(s)}`);
}
if (isSpecialSize) {
console.log(`=================>spider-all-task yhId: ${yhProduct.productId}, duId: ${detail.productId} size: ${s.size} => ${find.relationSize || s.size}, ${find.sizeId}`);
}
// const highRate = inSkns ? 0.02 : 0.05;
const highRate = 0.02;
report({
productId: yhProduct.productId,
sizeId: find.sizeId,
price: s.item.price / 100,
highRate,
time
}).then(() => {
const reportTime = timestamp + process.hrtime(hrtime)[1];
logger.info(`[spider-task]更新sku价格成功记录, duId:${detail.productId}, yhId: ${yhProduct.productId}, sizeId: ${find.sizeId}, time: ${time}, highRate: ${highRate}`);
sendMessage(1, reportTime);
}).catch(error => {
logger.error(error.message);
});
}
});
}));
};