spider-task.js
1.92 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
const _ = require('lodash');
const spider = require('../libs/spider');
const report = require('../libs/report');
const sknsData = require('../data/skns.json');
const sizeRelation = require('../data/size.json');
const {logger} = require('../libs/logger');
const sendMessage = (tick, sku, time) => {
if (!global.sender || !global.isProduction) {
return;
}
try {
global.sender.addMessage({
measurement: 'spider_ufo_success_report',
tags: {
type: 'ufo'
},
fields: {
tick,
sku
},
time
});
} catch (e) {
console.error(e);
}
};
module.exports = async(time) => {
spider(sknsData.map(p => p.productId))
.forEach(promise => promise.then(result => {
if (result.status !== 200 && !_.has(result, 'data.detail')) {
logger.warn(`爬取失败记录: ${JSON.stringify(result)}`);
return;
}
const {data: {detail, sizeList}} = result;
const yhProduct = sknsData.find(p => p.productId === detail.productId);
const hrtime = process.hrtime();
const timestamp = parseInt(Date.now() / 1000, 10) * 1000000000;
sizeList.forEach(s => {
if (_.isPlainObject(s.item) && s.item.price > 0) {
const find = sizeRelation[s.size];
if (!find) {
return logger.error(`爬取尺码对应关系未找到: ${JSON.stringify(s)}`);
}
report({
productId: yhProduct.yhId,
sizeId: find.sizeId,
price: s.item.price / 100,
time
}).then((data) => {
const reportTime = timestamp + process.hrtime(hrtime)[1];
logger.info(`更新sku价格成功记录, duId:${detail.productId}, yhId: ${yhProduct.yhId}, sizeId: ${find.sizeId}, time: ${time}`);
sendMessage(1, data.dataId, reportTime);
}).catch(error => {
logger.error(error.message);
});
}
});
}));
};