-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (58 loc) · 1.81 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
import ptt_crawler from '@waynechang65/ptt-crawler/lib/ptt_crawler.js';
import { searchOption } from './data.js';
import { FilterOption, TransformToObject, PushMessageAPI, sleep } from './function.js';
// 取得參數
let args = process.argv.slice(2);
if (!args[0]) {
console.log('Need LINE_MESSAGE_API_TOKEN !');
process.exit(1); // 終止程式,返回錯誤碼
}
if (!args[1]) {
console.log('Need YOU USER ID !');
process.exit(1); // 終止程式,返回錯誤碼
}
main();
async function main() {
// *** Initialize ***
await ptt_crawler.initialize();
let content = '';
// split content
let contentArray = [];
for (let item of searchOption) {
const ptt = await ptt_crawler.getResults({
board: item.boardName,
pages: item.pages,
skipPBs: true,
});
// filter data
const filteredData = FilterOption(TransformToObject(ptt), item.option)
// generate log content
content += filteredData.length > 0 ? `<${item.lineTitle}>\n` : ``;
for (let item of filteredData) {
// LINE MESSAGE Length must be between 0 and 5000
if (content.length < 4500) {
content += item.approval + ' 推 - ' + '日期:' + item.date + ' - ' + item.title + ' - ' + item.author + ' - ' + item.url + '\n';
} else {
contentArray.push(content)
content = item.approval + ' 推 - ' + '日期:' + item.date + ' - ' + item.title + ' - ' + item.author + ' - ' + item.url + '\n';
}
}
}
contentArray.push(content)
// *** Close ***
await ptt_crawler.close();
// Line message api
if (contentArray.length === 0) {
console.log('no message');
return;
}
if (contentArray.length > 5) {
console.log('最多一次傳5則訊息');
return;
}
for (let message of contentArray) {
await PushMessageAPI(args[0], args[1], message)
if(contentArray[contentArray.length - 1] === message) return;
await sleep(0.3)
}
}