-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (130 loc) · 4.85 KB
/
index.js
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
const { logger } = require('./lib/logger');
const SzfcClient = require('./lib/szfc');
const _ = require('lodash');
const { table } = require('table');
const async = require('async');
const process = require('process');
const RETRY_MAP = {};
let totalHouseList = [];
let startTimeStamp = 0;
let unsellCount = 0;
let sellingCount = 0;
let soldCount = 0;
let disableCount = 0;
let projectInterval = 1000;
let buildingInterval = 2000;
const projectQueue = async.queue(function(project, callback) {
const buildingQueue = async.queue(function({
szfcClient,
building
}, callback) {
setTimeout(async () => {
try {
let houseList = await szfcClient.getHouseList(building.project.id, building.id);
logger.info(`正在获取 [${building.project.district}] [${building.project.name}] [${building.name}] 的网签记录, 获取数量 ${houseList.length}`);
callback(null, houseList);
} catch (e) {
callback(e, []);
}
}, buildingInterval);
}, 1);
buildingQueue.error(function(err, {szfcClient, building}) {
let retryCount = RETRY_MAP[building.project.id + building.id] != null ? RETRY_MAP[building.project.id + building.id] : 0;
// buildingQueue.push({szfcClient, building}, saveHouseList);
projectQueue.push(building.project);
RETRY_MAP[building.project.id + building.id] = ++retryCount;
logger.error(`重试 [${building.project.name}] [${building.name}] 第 ${++retryCount} 次`);
});
const saveHouseList = function(err, houseList) {
totalHouseList = _.uniqBy(_.concat(totalHouseList, houseList), 'identifier');
}
setTimeout(async ()=> {
let szfcClient = await new SzfcClient();
try {
let buildingList = await szfcClient.getBuildingList(project);
logger.info(`${project.name} 下取得 ${buildingList.length} 条楼栋`);
_(buildingList).forEach(async function(building) {
buildingQueue.push({
szfcClient,
building
}, saveHouseList);
});
callback();
} catch (e) {
callback(e);
}
}, projectInterval);
}, 50);
projectQueue.error(function(err, project) {
let retryCount = RETRY_MAP[project.id] != null ? RETRY_MAP[project.id] : 0;
projectQueue.push(project);
RETRY_MAP[project.id] = ++retryCount;
logger.error(`重试 [${project.name}] 第 ${retryCount + 1} 次`);
});
const calcResult = function() {
let endTimeStamp = new Date().getTime();
logger.info(`Project queue: ${projectQueue.length()}`);
logger.info(`花费时间: ${(endTimeStamp - startTimeStamp)/1000} 秒`);
logger.info(`取得数据: ${totalHouseList.length} 条`);
unsellCount += _.filter(totalHouseList, {houseStatus: 0}).length;
sellingCount += _.filter(totalHouseList, {houseStatus: 1}).length;
soldCount += _.filter(totalHouseList, {houseStatus: 2}).length;
disableCount += _.filter(totalHouseList, {houseStatus: 3}).length;
let countTableData = [];
countTableData.push(['可售出', unsellCount]);
countTableData.push(['签约中', sellingCount]);
countTableData.push(['已售出', soldCount]);
countTableData.push(['锁定中', disableCount]);
countTableData.push(['合计', totalHouseList.length]);
logger.info("\n" + table(countTableData));
}
process.on('SIGINT', function() {
process.exit();
});
process.on('exit', function() {
calcResult();
});
startTimeStamp = new Date().getTime();
(async function () {
let szfcClient = await new SzfcClient();
let districtList = await szfcClient.getDistrictList();
logger.debug(`取得 ${districtList.length} 个地区`);
_(districtList).forEach(async function(district) {
let pageSize = await szfcClient.getProjectCount(district);
logger.info(`${district} 共有 ${pageSize} 页数据`);
// Loop project list page
for (let i=1; i<= pageSize; i++) {
let projectList = await szfcClient.getProjectList(district, i);
logger.debug(`从 ${district} 第 ${i} 页取得 ${projectList.length} 条数据`);
// Loop building under project
_(projectList).forEach(async function(project) {
await projectQueue.push(project);
});
}
});
}());
// (async function () {
// await szfc.getSaleInfoProListIndex();
// projectQueue.push([{
// id: 'b040951a-3028-4b95-b6a2-439e019d013e',
// name: '春风南岸花园一期',
// district: '高新区'
// }, {
// id: '4e5947bb-75a9-4a09-8167-291c7d16a3e2',
// name: '春风南岸花园二期',
// district: '高新区'
// }]);
// await Promise.all([projectQueue.drain(), buildingQueue.drain()]);
// }());
exports.main_handler = async (event, context) => {
await projectQueue.push([{
id: 'b040951a-3028-4b95-b6a2-439e019d013e',
name: '春风南岸花园一期',
district: '高新区'
}, {
id: '4e5947bb-75a9-4a09-8167-291c7d16a3e2',
name: '春风南岸花园二期',
district: '高新区'
}]);
};
// exports.main_handler({}, {}, function() {});