-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
262 lines (242 loc) · 8.55 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'use strict';
const cheerio = require('cheerio');
const fs = require('fs');
const util = require('util');
const dayjs = require('dayjs');
const customParseFormat = require('dayjs/plugin/customParseFormat');
const utc = require('dayjs/plugin/utc');
const schedule = require('node-schedule');
const TelegramBot = require('node-telegram-bot-api');
const request = require('request');
const data = require('./data.json');
dayjs.extend(customParseFormat);
dayjs.extend(utc);
const messageIDs = new Map();
const channels = new Map(data.channel);
const failed = [];
let username;
let results = [];
let pubDate = dayjs();
const bot = new TelegramBot(data.key, {
polling: {
interval: 0,
params: {timeout: 60}
}
});
const saveData = function() {
data.channel = [...channels];
fs.writeFile('data.json', JSON.stringify(data), () => {});
};
bot.getMe().then((me) => {
username = me.username;
bot.onText(/^\/(\w+)@?(\w*)/i, (msg, regex) => {
if (regex[2] && regex[2] !== me.username) {
return;
}
if (msg.chat.type === 'private') {
console.log('%s(%s) => %s: %s', msg.from.username, msg.from.id, me.username, msg.text);
} else {
if (regex[2] !== me.username) {
return;
}
console.log('%s(%s) => %s(%s): %s', msg.from.username, msg.from.id, msg.chat.title, msg.chat.id, msg.text);
}
switch (regex[1]) {
case 'start':
if (msg.text.match(/\w{32}$/)) {
bot.sendMessage(msg.chat.id, `<pre>magnet:?xt=urn:btih:${msg.text.replace(/\/start@?\w*\s/i, '')}</pre>`, {parse_mode: 'HTML'});
}
break;
case 'search':
search(msg);
break;
case 'subscribe':
if (channels.has(msg.chat.id)) {
if (msg.chat.type === 'private') {
bot.sendMessage(msg.chat.id, '您已經在訂閱清單中了!\n若要更改關鍵字請先輸入 /unsubscribe 取消訂閱');
} else {
bot.sendMessage(msg.chat.id, '這個聊天室已經在訂閱清單中了!\n若要更改關鍵字請先輸入 /unsubscribe 取消訂閱');
}
return;
}
if (msg.text.match(/\s(.+)/)) {
channels.set(msg.chat.id, msg.text.match(/\s(.+)/)[1]);
saveData();
} else {
channels.set(msg.chat.id);
saveData();
}
if (msg.chat.type === 'private') {
bot.sendMessage(msg.chat.id, '訂閱成功!\n當有更新時會向您發送訊息');
} else {
bot.sendMessage(msg.chat.id, '訂閱成功!\n當有更新時會向這個聊天室發送訊息');
}
break;
case 'unsubscribe':
if (channels.delete(msg.chat.id)) {
saveData();
console.log(msg.chat.id + ' remove from subscription list.');
if (msg.chat.type === 'private') {
bot.sendMessage(msg.chat.id, '已將您從訂閱清單中刪除!');
} else {
bot.sendMessage(msg.chat.id, '已將此聊天室從訂閱清單中刪除!');
}
} else {
bot.sendMessage(msg.chat.id, '尚未訂閱!\n輸入 /subscribe 來訂閱');
}
break;
}
});
bot.onText(/https?:\/\/share\.dmhy\.org\/topics\/view\/.*\.html/ig, (msg, regex) => {
request(regex[0], (error, response, body) => {
if (error || response.statusCode !== 200) {
return;
}
const $ = cheerio.load(body);
const link = $('#magnet2').text();
const torrent = $('#tabs-1 a').attr('href');
if (link && torrent) {
bot.sendMessage(msg.chat.id, util.format(`<a href="https:${torrent}">${link}</a>`), {
reply_to_message_id: msg.message_id,
parse_mode: 'HTML'
});
}
});
});
bot.on('new_chat_members', (msg) => {
if (msg.new_chat_member.username === me.username) {
console.log('join %s(%s)', msg.chat.title, msg.chat.id);
channels.set(msg.chat.id);
saveData();
}
});
bot.on('left_chat_member', (msg) => {
if (msg.left_chat_member.username === me.username) {
console.log('left %s(%s)', msg.chat.title, msg.chat.id);
channels.delete(msg.chat.id);
saveData();
}
});
bot.on('migrate_to_chat_id', (msg) => {
console.log('migrate chat from %s to %s', msg.chat.id, msg.migrate_to_chat_id);
channels.delete(msg.chat.id);
channels.set(msg.migrate_to_chat_id);
});
});
const search = function(msg) {
if (!msg.text.match(/\s(.+)/)) {
bot.sendMessage(msg.chat.id, '請輸入要搜尋的關鍵字!\n範例:/search 果 青\n僅顯示五個結果\n更多資訊請看 https://share.dmhy.org/cms/page/name/faq.html#faq3');
return;
}
const keyword = encodeURI(msg.text.match(/\s(.+)/)[1].replace(' ', '+'));
request('http://share.dmhy.org/topics/rss/rss.xml?keyword=' + keyword, (err, res, body) => {
if (err || res.statusCode !== 200) {
bot.sendMessage(msg.chat.id, '抓取結果時發生錯誤!');
return;
}
const processItem = function() {
$('item').each((i, elem) => {
if (i < 5) {
const date = dayjs($(elem).children('pubDate').text());
result.push(util.format('%s <code>%s</code>\n<a href="%s">%s</a> <a href="%s">*</a>',
date.locale('zh-tw').local().format('YYYY/M/D HH:mm'),
$(elem).children('category').text(),
$(elem).children('link').text(),
$(elem).children('title').text().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'),
`tg://resolve?domain=${username}&start=${$(elem).children('enclosure').attr('url').substr(20, 32)}`
));
}
});
bot.sendMessage(msg.chat.id, result.join('\n\n'), {
parse_mode: 'HTML',
disable_web_page_preview: true,
disable_notification: true
});
};
const result = [];
const $ = cheerio.load(body, {
xmlMode: true
});
if ($('item').index() > 0) {
processItem();
} else {
bot.sendMessage(msg.chat.id, '找不到任何結果!');
}
});
};
const getUpdate = function() {
request('https://share.dmhy.org/topics/rss/rss.xml', (err, res, body) => {
if (err || res.statusCode !== 200) {
console.log('update fetch failed!');
return;
}
let tmpTime;
const tmpData = [];
const $ = cheerio.load(body, {
xmlMode: true
});
$('item').each((i, elem) => {
const date = dayjs($(elem).children('pubDate').text());
if (pubDate.isBefore(date)) {
tmpData.push(util.format('%s <code>%s</code>\n<a href="%s">%s</a> <a href="%s">*</a>',
date.locale('zh-tw').local().format('HH:mm'),
$(elem).children('category').text(),
$(elem).children('link').text(),
$(elem).children('title').text().replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'),
`tg://resolve?domain=${username}&start=${$(elem).children('enclosure').attr('url').substr(20, 32)}`
));
if (!tmpTime) tmpTime = date;
}
});
if (tmpData.length !== 0) {
results = tmpData.concat(results);
}
if (tmpTime) {
pubDate = tmpTime;
}
if (results.length === 0) {
return;
}
let messages;
channels.forEach((filter, channel) => {
if (filter) {
messages = results.slice().filter((message) => message.substr(0, message.length - (59 + username.length)).match(new RegExp(filter, 'i'))).reverse();
if (messages.length === 0) {
return;
}
} else {
messages = results.slice().reverse();
}
if (messageIDs.get(channel)) {
bot.editMessageText(messages.join('\n\n'), {
chat_id: channel,
message_id: messageIDs.get(channel),
parse_mode: 'HTML',
disable_web_page_preview: true,
disable_notification: true
}).catch(() => {});
} else {
bot.sendMessage(channel, messages.join('\n\n'), {
parse_mode: 'HTML',
disable_web_page_preview: true,
disable_notification: true
}).then((msg) => {
messageIDs.set(msg.chat.id, msg.message_id);
}).catch((e) => {
console.log('Send Update to %s failed! Error: %s', channel, e.message);
if (failed.indexOf(channel) > -1) {
channels.delete(channel);
saveData();
} else if (e.code === 'ETELEGRAM' && e.response.body.error_code >= 400 && e.response.body.error_code < 500 ) {
failed.push(channel);
}
});
}
});
});
};
schedule.scheduleJob('0 * * * * *', getUpdate);
schedule.scheduleJob('0 * * * *', () => {
messageIDs.clear();
results = [];
});