price-task.js
1.02 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
const dayjs = require('dayjs');
const spider = require('../libs/spider');
const fs = require('fs');
const _ = require('lodash');
module.exports = async() => {
const now = dayjs().format('YYYY-MM-DD_HH');
const fw = fs.createWriteStream(`/Data/logs/node/prices/${now}.log`, {
flags: 'a'
});
const ids = Array.from(new Array(22080)).map((v, i) => i + 1);
try {
spider(ids, 'http://du.hupu.com/mapi/product/detail', {}, 1).forEach((promise, inx) => promise.then(result => {
if (result.status === 200) {
const {detail, sizeList} = result.data;
const productId = detail.productId;
const sizes = sizeList.map(sizeItem => {
return {
size: sizeItem.size,
price: _.isEmpty(sizeItem.item) ? 0 : sizeItem.item.price
};
});
fw.write(`${JSON.stringify({
productId,
sizes
})}\n`);
}
if (inx >= ids.length - 1) {
fw.end('');
}
}));
} catch (error) {
fw.end(error.toString());
}
};