spider-buyers-incre.js
6.32 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const dayjs = require('dayjs');
const lockup = require('node-lockup');
const isBetween = require('dayjs/plugin/isBetween');
const _ = require('lodash');
const MysqlAdapter = require('./libs/mysql');
const spider = require('./libs/spider');
const config = require('./spider-buyers.json');
const {logger} = require('./libs/logger');
dayjs.extend(isBetween);
const mysql = new MysqlAdapter(config.connect, config.connect.database);
const products = {};
const RegNow = /刚刚/;
const RegMinute = /(\d+)分钟前/;
const RegHour = /(\d+)小时前/;
const RegDay = /(\d+)天前/;
const RegDate = /(\d+)月(\d+)日/;
const RegDateFull = /\d+\.\d+\.\d+/;
let tableName;
const parseTime = (relativeTime) => {
const matchMinute = relativeTime.match(RegMinute);
if (matchMinute) {
return dayjs().add(0 - matchMinute[1], 'minute');
}
const matchHour = relativeTime.match(RegHour);
if (matchHour) {
return dayjs().add(0 - matchHour[1], 'hour');
}
const matchDay = relativeTime.match(RegDay);
if (matchDay) {
return dayjs().add(0 - matchDay[1], 'day');
}
const matchDate = relativeTime.match(RegDate);
if (matchDate) {
return dayjs(`2019/${matchDate[1]}/${matchDate[2]}`);
}
if (RegDateFull.test(relativeTime)) {
return dayjs(relativeTime);
}
if (RegNow.test(relativeTime)) {
return dayjs();
}
return dayjs('1990-01-01');
};
const spiderBuyers = async(productId, lsId = 0, page = 0, trys = 0) => {
const result = await spider.spiderFetch(productId, 'https://du.hupu.com/mapi/product/lastSoldList', {
lastId: lsId
});
let lastId;
let tryLogic = false;
let list = [];
if (result.status === 200) {
let skip = false;
lastId = result.data.lastId;
list = result.data.list;
console.log(`productId: ${productId}, json: ${JSON.stringify(list)}, time: ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`);
_.each(list, info => {
const orderTime = parseTime(info.formatTime);
const {lastTime, lastNickName, lastIcon, lastSize} = products[productId];
if (products[productId].lastTime) {
const lTime = dayjs(lastTime).startOf('minute');
const oTime = orderTime.startOf('minute');
if (oTime.isBefore(lTime)) {
skip = true;
return false;
}
if (`${info.buyer.userName}${info.buyer.icon}${info.item.size}` === `${lastNickName}${lastIcon}${lastSize}`) {
skip = true;
return false;
}
}
console.log('=======================')
console.log(`productId: ${productId}, size: ${info.item.size}, ${orderTime.format('YYYY-MM-DD HH:mm:ss')}`)
console.log(info.formatTime, orderTime.format('YYYY-MM-DD HH:mm:ss'), products[productId].lastTime, `${info.buyer.userName}${info.buyer.icon}${info.item.size}`)
console.log(`${info.buyer.userName}${info.buyer.icon}${info.item.size}`, `${lastNickName}${lastIcon}${lastSize}`)
mysql.insert('INSERT INTO `' + tableName + '` (`productId`, `nickName`, `icon`, `time`, `size`, `soldNum`, `price`, `productName`, `model`) VALUES (:productId, :nickName, :icon, :time, :size, :soldNum, :price, :productName, :model)', {
productId: productId,
nickName: info.buyer.userName,
icon: info.buyer.icon,
size: info.item.size,
time: orderTime.format('YYYY-MM-DD HH:mm:ss'),
soldNum: products[productId].count,
price: +products[productId].price / 100,
productName: products[productId].title,
model: products[productId].articleNumber
});
})
if (skip) {
return {productId};
}
} else {
if (trys < 3) {
tryLogic = true;
}
}
if (tryLogic) {
logger.info(`tryLogic ${trys}`);
return spiderBuyers(productId, lsId, page, trys + 1);
} else {
if (lastId) {
logger.info(`productId: ${productId}, nextPage: ${page}, lastId: ${lastId}`);
return spiderBuyers(productId, lastId, page + 1);
}
return {productId};
}
};
// 2721219
let max = 50000;
let failNum = 0;
let over = false;
const start = async(productId) => {
if (over) {
return;
}
if (productId >= max) {
over = true;
logger.info('over!!!!!!');
return;
}
const lastRow = await mysql.query('SELECT * FROM `' +tableName+ '` WHERE `productId`=' + productId + ' order by `time` desc, id limit 1');
const result = await spider.spiderFetch(productId);
if (result.status === 200) {
failNum = 0;
products[productId] = {
count: _.get(result, 'data.detail.soldNum', 0),
price: _.get(result, 'data.item.price', 0),
title: _.get(result, 'data.detail.title', ''),
articleNumber: _.get(result, 'data.detail.articleNumber', ''),
};
if (lastRow.length) {
products[productId].lastNickName = lastRow[0].nickName;
products[productId].lastSize = lastRow[0].size;
products[productId].lastIcon = lastRow[0].icon;
products[productId].lastTime = lastRow[0].time;
} else {
products[productId].lastTime = dayjs().add(-10, 'minute').format('YYYY-MM-DD HH:mm:ss')
}
try {
await spiderBuyers(productId);
} catch (error) {
logger.info('error', error);
}
} else {
logger.info(productId, result.status);
if (result.status) {
failNum++;
}
}
if (failNum >= 20) {
over = true;
return logger.info('over!!!!!!', JSON.stringify(result));
}
return true;
};
const createDayTable = async() => {
tableName = `buyers_incre`;
const sql = `CREATE TABLE \`${tableName}\` (\`productId\` int(11) DEFAULT NULL,\`nickName\` varchar(50) DEFAULT NULL,\`icon\` varchar(200) DEFAULT NULL,\`time\` datetime DEFAULT NULL,\`size\` varchar(50) DEFAULT NULL,\`id\` int(11) NOT NULL AUTO_INCREMENT,\`soldNum\` int(11) DEFAULT NULL,\`price\` decimal(10,0) DEFAULT NULL,\`productName\` varchar(200) DEFAULT NULL,\`model\` varchar(100) DEFAULT NULL,PRIMARY KEY (\`id\`),KEY \`index_name_icon\` (\`nickName\`,\`icon\`), KEY \`productid\` (\`productId\`) USING BTREE, KEY \`time\` (\`time\`) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8;`;
const result = await mysql.query(`SELECT table_name FROM information_schema.TABLES WHERE table_name ='${tableName}'`);
if (result.length) {
return;
}
return mysql.execute(sql);
};
const locktask = lockup(start);
// 4211292
module.exports = async() => {
await createDayTable();
const ids = [9670, 31270, 26209];
ids.forEach(id => {
locktask(id);
})
};