Authored by 陈峰

commit

  1 +const schedule = require('node-schedule');
  2 +const spiderBuyers = require('./other/spider-buyers');
  3 +
  4 +schedule.scheduleJob('0 0 2 * * *', () => {
  5 + spiderBuyers();
  6 +});
@@ -31,7 +31,8 @@ const task = async(options, tick = 1) => { @@ -31,7 +31,8 @@ const task = async(options, tick = 1) => {
31 Referer: 'http://du.hupu.com/mdu/product/detail.html?id=1&source=boutiqueRecommend', 31 Referer: 'http://du.hupu.com/mdu/product/detail.html?id=1&source=boutiqueRecommend',
32 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 Safari/604.1', 32 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 Safari/604.1',
33 'X-Requested-With': 'XMLHttpRequest' 33 'X-Requested-With': 'XMLHttpRequest'
34 - } 34 + },
  35 + timeout: 2000
35 }); 36 });
36 37
37 return result; 38 return result;
1 const dayjs = require('dayjs'); 1 const dayjs = require('dayjs');
  2 +const lockup = require('node-lockup');
  3 +const isBetween = require('dayjs/plugin/isBetween');
2 const _ = require('lodash'); 4 const _ = require('lodash');
3 5
4 require('../libs/logger'); 6 require('../libs/logger');
@@ -6,6 +8,8 @@ const config = require('../config/index'); @@ -6,6 +8,8 @@ const config = require('../config/index');
6 const MysqlAdapter = require('../libs/mysql'); 8 const MysqlAdapter = require('../libs/mysql');
7 const spider = require('../libs/spider'); 9 const spider = require('../libs/spider');
8 10
  11 +dayjs.extend(isBetween)
  12 +
9 const mysql = new MysqlAdapter(config.database.connect, config.database.database); 13 const mysql = new MysqlAdapter(config.database.connect, config.database.database);
10 14
11 const RegNow = /刚刚/; 15 const RegNow = /刚刚/;
@@ -61,77 +65,125 @@ const getLeftTime = () => { @@ -61,77 +65,125 @@ const getLeftTime = () => {
61 return `${hour}小时${mins}分钟${parseInt(leftTime % 60)}秒`; 65 return `${hour}小时${mins}分钟${parseInt(leftTime % 60)}秒`;
62 }; 66 };
63 67
64 -const minTime = dayjs('2018-12-22');  
65 -  
66 const spiderBuyers = async(productId, lsId = 0, page = 0, trys = 0) => { 68 const spiderBuyers = async(productId, lsId = 0, page = 0, trys = 0) => {
67 const result = await spider.spiderFetch(productId, 'https://du.hupu.com/mapi/product/lastSoldList', { 69 const result = await spider.spiderFetch(productId, 'https://du.hupu.com/mapi/product/lastSoldList', {
68 lastId: lsId 70 lastId: lsId
69 }); 71 });
70 72
71 - let skipTime, lastId; 73 + let lastId;
  74 + let tryLogic = false;
  75 + let list = [];
72 if (result.status === 200) { 76 if (result.status === 200) {
73 - const {list} = result.data;  
74 - 77 + let skip = false;
75 lastId = result.data.lastId; 78 lastId = result.data.lastId;
  79 + list = result.data.list;
76 80
77 81
78 list.forEach(info => { 82 list.forEach(info => {
79 const time = parseTime(info.formatTime); 83 const time = parseTime(info.formatTime);
80 -  
81 - if (time.isBefore(minTime)) {  
82 - skipTime = true;  
83 - } else { 84 + const startTime = dayjs().subtract(1, 'day').add(1, 'second');
  85 +
  86 + if (!time.isBefore(startTime)) {
84 mysql.insert('INSERT INTO `buyers_copy` (`productId`, `nickName`, `icon`, `time`, `size`, `soldNum`, `price`) VALUES (:productId, :nickName, :icon, :time, :size, :soldNum, :price)', { 87 mysql.insert('INSERT INTO `buyers_copy` (`productId`, `nickName`, `icon`, `time`, `size`, `soldNum`, `price`) VALUES (:productId, :nickName, :icon, :time, :size, :soldNum, :price)', {
85 productId: productId, 88 productId: productId,
86 nickName: info.buyer.userName, 89 nickName: info.buyer.userName,
87 icon: info.buyer.icon, 90 icon: info.buyer.icon,
88 size: info.item.size, 91 size: info.item.size,
89 - time: parseTime(info.formatTime).format('YYYY-MM-DD'), 92 + time: time.format('YYYY-MM-DD HH:mm:ss'),
90 soldNum: products[productId].count, 93 soldNum: products[productId].count,
91 price: products[productId].price, 94 price: products[productId].price,
92 }); 95 });
  96 + } else {
  97 + if (!skip) {
  98 + console.log(time.format('YYYY-MM-DD HH:mm:ss'), startTime.format('YYYY-MM-DD HH:mm:ss'))
  99 + }
  100 + skip = true;
93 } 101 }
94 }); 102 });
  103 +
  104 + if (skip) {
  105 + return {productId};
  106 + }
95 } else { 107 } else {
96 if (trys < 3) { 108 if (trys < 3) {
97 - return spiderBuyers(productId, lsId, page, trys + 1); 109 + tryLogic = true;
98 } 110 }
99 } 111 }
100 112
101 - console.clear();  
102 - console.log(`page: ${page}, count: ${products[productId].count}, lastId: ${lastId}, 进度: ${inx}/${max}(${parseInt((page * 20 + list.length) / products[productId].count * 100)}%), 剩余: ${getLeftTime()}, productId: ${productId}`);  
103 - if (lastId && !skipTime) {  
104 - spiderBuyers(productId, lastId, page + 1); 113 + if (tryLogic) {
  114 + console.log(`tryLogic ${trys}`)
  115 + return spiderBuyers(productId, lsId, page, trys + 1);
105 } else { 116 } else {
106 - end = Date.now();  
107 - start(); 117 + if (lastId) {
  118 + console.log(`spiderBuyers ${lastId}, ${page}`)
  119 + return spiderBuyers(productId, lastId, page + 1);
  120 + }
  121 + return {productId};
108 } 122 }
109 }; 123 };
110 124
111 //2721219 125 //2721219
112 const products = {}; 126 const products = {};
113 127
114 -let max = 24200;  
115 -let inx = 8707;  
116 -const start = () => { 128 +let max = 34200;
  129 +let failNum = 0;
  130 +let inx = 1;
  131 +let over = false;
  132 +
  133 +const start = async() => {
  134 + if (over) {
  135 + return;
  136 + }
117 const productId = inx; 137 const productId = inx;
  138 + console.log(`start 1 , ${productId}`)
118 139
119 inx++; 140 inx++;
120 if (inx >= max) { 141 if (inx >= max) {
  142 + over = true;
121 console.log('over!!!!!!'); 143 console.log('over!!!!!!');
122 return; 144 return;
123 } 145 }
124 146
125 - if (products[productId]) {  
126 - spiderBuyers(productId); 147 + const result = await spider.spiderFetch(productId);
  148 + console.log(`start 2 , ${productId}`)
  149 +
  150 + if (result.status === 200) {
  151 + failNum = 0;
  152 + products[productId] = {
  153 + count: _.get(result, 'data.detail.soldNum', 0),
  154 + price: _.get(result, 'data.item.price', 0)
  155 + };
  156 + try {
  157 + const bl = await spiderBuyers(productId);
  158 + console.log(`start 3 , ${productId}`)
  159 +
  160 + if (bl) {
  161 + console.log(JSON.stringify(bl));
  162 + }
  163 + } catch (error) {
  164 + console.log('error', error);
  165 + }
  166 +
  167 +
127 } else { 168 } else {
128 - spider([productId]).forEach(promise => promise.then(result => {  
129 - products[productId] = {  
130 - count: _.get(result, 'data.detail.soldNum', 0),  
131 - price: _.get(result, 'data.item.price', 0)  
132 - };  
133 - spiderBuyers(productId);  
134 - })); 169 + console.log(productId, result.status)
  170 + if (result.status) {
  171 + failNum++;
  172 + }
  173 + }
  174 + if (failNum >= 20) {
  175 + over = true;
  176 + return console.log('over!!!!!!', JSON.stringify(result));
135 } 177 }
  178 + return true;
136 }; 179 };
137 180
  181 +const locktask = lockup(start);
  182 +
  183 +
  184 +module.exports = () => {
  185 + Array.from({length: max}).map((v, i) => {
  186 + locktask();
  187 + })
  188 +}
  189 +