price-task.js 1.02 KB
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());
  }
};