price-task.js
2.46 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
const dayjs = require('dayjs');
const nodeLockup = require('node-lockup');
const spider = require('../libs/spider');
const {logger} = require('../libs/logger');
const fs = require('fs');
const _ = require('lodash');
const REG_HOUR = /^[1,2]小时/;
const REG_MINUES = /^\d+分钟/;
const fetchBuys = async(productId, lsId = 0) => {
let lastList = [];
const result = await spider.spiderFetch(productId, 'https://du.hupu.com/mapi/product/lastSoldList', {
lastId: lsId
});
if (result.status === 200) {
const {list, lastId} = result.data;
lastList = list.filter(info => {
return REG_HOUR.test(info.formatTime) || REG_MINUES.test(info.formatTime) || info.formatTime.indexOf('刚刚') >= 0;
});
if (lastList.length) {
const nextList = await fetchBuys(productId, lastId);
lastList = lastList.concat(nextList);
}
}
return lastList;
};
const processBuys = async(data) => {
const allList = await fetchBuys(data.productId);
data.sizes.forEach(size => {
const buys = allList.filter(info => {
return info.item && info.item.size === size.size;
});
size.soldNum += buys.length;
});
return data;
};
const task = async(id) => {
const result = await spider.spiderFetch(id);
if (result.status === 200) {
const {detail, sizeList} = result.data;
const productId = detail.productId;
const soldNum = detail.soldNum;
const articleNumber = detail.articleNumber;
// const sizes = sizeList.map(sizeItem => {
// return {
// size: sizeItem.size,
// price: _.isEmpty(sizeItem.item) ? 0 : sizeItem.item.price,
// soldNum: 0
// };
// });
// const dest = await processBuys({
// productId,
// soldNum,
// sizes
// });
return {
productId,
soldNum,
articleNumber
};
}
};
module.exports = async() => {
const now = dayjs().format('YYYY-MM-DD_HH');
const fw = fs.createWriteStream(`/Data/logs/node/prices/${now}.log`, {
flags: 'a'
});
const lockTask = nodeLockup(task, 1);
const ids = Array.from(new Array(22080)).map((v, i) => i + 1);
try {
ids.forEach((id, inx) => {
lockTask(id).then(async result => {
if (result) {
logger.info(`time:${now}; productId:${id}; soldNum:${result.soldNum};`);
fw.write(`${JSON.stringify(result)}\n`);
}
if (inx >= ids.length - 1) {
fw.end('');
}
});
});
} catch (error) {
fw.end(error.toString());
}
};