forked from aiyu666/rentHouse
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
140 lines (126 loc) · 4.29 KB
/
app.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
const http = require('http');
const { getRequest } = require('./lib/request');
const sendLineNotify = require('./lib/sendLineNotify');
const getFirstPostId = require('./lib/getFirstPostId');
const getToken = require('./lib/getToken');
const {
houseListURLs, port, requestFrquency, lineTokens, checkServiceStatus,
subwayStationFilter, communityFilter,
} = require('./lib/getEnv');
let serviceStatus = true;
let countFail = 0;
houseListURLs.forEach(async (houseListURL) => {
let originPostId = await getFirstPostId(houseListURL);
const stopIntervalId = setInterval(async () => {
const region = new URL(houseListURL).searchParams.get('region');
const headerInfo = await getToken();
const csrfToken = headerInfo[0];
let cookie = headerInfo[1];
if (region) {
cookie = `urlJumpIp=${region}; ${cookie}`;
}
if (checkServiceStatus.enable) {
serviceStatus = isServiceAvailable(checkServiceStatus.url);
if (serviceStatus === false) {
clearInterval(stopIntervalId);
}
}
try {
const resp = await getRequest({
url: houseListURL,
headers: {
'X-CSRF-TOKEN': csrfToken,
Cookie: cookie,
},
json: true,
});
if (resp.statusCode !== 200) {
// eslint-disable-next-line no-throw-literal
throw `Token 可能過期了,目前 StatusCode: ${resp.statusCode}`;
}
const { data } = resp.body.data;
const postIDList = data.map((rentDetail) => rentDetail.post_id);
if (postIDList.includes(originPostId) === false) {
[originPostId] = postIDList;
}
for (const rentDetail of data) {
const { post_id: postID, community } = rentDetail;
const {
type: surroundingType = '',
desc: destination = '',
distance = '',
} = rentDetail.surrounding;
if (postID === originPostId) break;
if (communityFilter.enable && !isFilterByCommunity(community)) continue;
if (subwayStationFilter.enable
&& surroundingType === 'subway_station'
&& isSubwayStationNearby(destination, distance) === false
) continue;
lineTokens.forEach(async (token) => {
await sendLineNotify(
`\nhttps://rent.591.com.tw/rent-detail-${postID}.html`,
token,
);
});
}
originPostId = data[0].post_id;
} catch (error) {
if (countFail > 10) {
lineTokens.forEach(async (token) => {
await sendLineNotify(
`\n好像出事了! 但是我嘗試重新拿 Token 第 ${countFail} 次了所以暫時先把程式關閉,有空可以檢查一下。\n `,
token,
);
});
clearInterval(stopIntervalId);
}
console.error(`Fetch the 591 rent fail: ${error}`);
countFail += 1;
}
}, requestFrquency);
});
const server = http.createServer((req, res) => {
if (!serviceStatus) {
console.error('Service stopping.');
res.writeHead(500, { 'Content-Type': 'text/html' });
return res.end('Service have some problem QQ plz check the log.');
}
if (req.url === '/ping') {
console.log('我還活著!');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('pong');
return res.end();
}
res.writeHead(400, { 'Content-Type': 'text/html' });
return res.end('Invalid Request!');
});
server.listen(port);
console.log(
`Node.js web server at port ${port} is running..`,
);
function isSubwayStationNearby(destination, distance) {
if (destination === '' || distance === '') return false;
destination = destination.slice(1);
distance = parseInt(distance.slice(0, -2), 10);
if (!subwayStationFilter.station.includes(destination)) return false;
if (distance > subwayStationFilter.distance) return false;
return true;
}
function isFilterByCommunity(community) {
const result = communityFilter.filter.map(
({ keyword, condition }) => {
if (condition === 'include') return community.includes(keyword);
if (condition === 'exclude') return !community.includes(keyword);
return false;
},
);
return result.every(Boolean);
}
async function isServiceAvailable(url) {
const servicePing = await getRequest(url);
if (servicePing.statusCode !== 200) {
console.error('Ping fail plz check it.');
return false;
}
return true;
}