forked from lanthora/rssbot-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rssbot.py
287 lines (258 loc) · 11.8 KB
/
rssbot.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Copyright (C) 2019-2020 lanthora #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import configparser
import logging
import signal
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from telegram import Bot
from telegram.error import BadRequest, Unauthorized
from telegram.ext import CommandHandler, Job, Updater
from rssdata import NoSQLDB, RecentlyUsedElements, RegularExpression, Settings
from rssdatabase import RSSdatabase
from rssfetcher import ParseError, RSSFethcer
from util import md5sum, regular_match
class RSSBot(object):
def __init__(self):
self.bot = Bot(Settings().get_token())
self.updater = Updater(token=Settings().get_token(), use_context=True)
self.dp = self.updater.dispatcher
self.jq = self.updater.job_queue
self.interval = Settings().get_interval()
self.fether = RSSFethcer()
self.database = RSSdatabase()
self.et = NoSQLDB().get_error_times_db()
self.el = Settings().get_error_limit()
self.recently_used_elements = RecentlyUsedElements()
self.executor = ThreadPoolExecutor()
self.exit = False
self.re_db = RegularExpression()
signal.signal(signal.SIGINT, self.sig_handler)
signal.signal(signal.SIGTERM, self.sig_handler)
def __send_html(self, chat_id, text):
self.bot.send_message(
chat_id, text,
parse_mode='HTML',
disable_web_page_preview=True
)
def __can_sub(self, chat_id, username):
admin = Settings().get_admin()
sublimit = Settings().get_sublimit()
current_sub = len(self.database.get_sub_by_chat_id(chat_id))
return (username == admin or current_sub < sublimit)
def __error(self, update, context):
try:
raise context.error
except BaseException as e:
logging.error(e)
def __refresh(self, context):
rss_list = self.database.get_rss_list()
if len(rss_list) == 0:
return
delta = self.interval/len(rss_list)
for rss in rss_list:
if self.exit:
break
self.executor.submit(self.__update, rss.url)
time.sleep(delta)
def __update(self, url):
try:
chats = self.database.get_chats_by_url(url)
mark = self.database.get_mark(url)
_rssitems = self.fether.update_rss(url)
self.database.set_mark(url, _rssitems[0].mark)
rssitems = []
normal = False
continue_times = 0
for rssitem in _rssitems:
iid = md5sum(rssitem.mark)
if rssitem.mark == mark:
normal = True
break
# 出现三篇最近推送过的文章,即使mark不匹配,也是视为正常匹配
# 这个策略可以用来解决最近文章被替换的问题
elif self.recently_used_elements.has_element(iid, url):
continue_times += 1
if continue_times < 3:
continue
else:
normal = (len(rssitems) != 0)
break
else:
rssitems.append(rssitem)
if not normal:
rssitems.clear()
logging.info("更新异常 清理推送 {}".format(url))
self.et[url] = 0
if len(rssitems) > 0:
self.__send(rssitems, chats)
except (ParseError, IndexError):
self.et[url] = self.et.setdefault(url, 0) + 1
if self.et[url] >= int(self.el):
self.database.set_active(url, False)
title = self.database.get_rss_by_url(url).title
text = '<a href="{}">{} </a>'.format(url, title)
text += '更新时出现错误,已停止推送,请检查无误后重新订阅'
logging.info("连续错误 停止推送{}".format(url))
for chat_id in chats:
self.__send_html(chat_id, text)
def __send(self, rssitems, chats):
_text = ''
_url = rssitems[0].url
while len(rssitems):
rssitem = rssitems.pop()
_text += '\n<a href="{}">{}</a>'.format(rssitem.link, rssitem.name)
for chat_id in chats:
_text = regular_match(_text, self.re_db.get_re(chat_id, _url))
if(_text == "\n"):
continue
nickname = self.database.get_nickname(_url, chat_id)
_title = '<b>{}</b>'.format(nickname)
text = _title + _text
try:
self.__send_html(chat_id, text)
except (BadRequest, Unauthorized):
self.database.del_sub('', chat_id)
except BaseException as base_exception:
logging.error(base_exception)
def start(self, update, context):
chat_id = update.message.chat_id
text = Settings().get_start_msg()
self.__send_html(chat_id, text)
def subscribe(self, update, context):
chat_id = update.message.chat_id
username = update.effective_user.username
if not self.__can_sub(chat_id, username):
sublimit = Settings().get_sublimit()
text = '订阅上限为 <i>{}</i> , 您已达到订阅上限\n'.format(sublimit)
text += '请根据 /start 中的指引自行构建'
self.__send_html(chat_id, text)
logging.info("达到上限 {}".format(chat_id))
return
try:
url = update.message.text.split(' ')[1]
rss = self.fether.check_url(url)
if rss.active:
self.database.add_rss(rss)
self.database.add_sub(rss.url, chat_id)
_text = '已订阅: <a href="{}">{}</a>'
text = _text.format(rss.url, rss.title)
logging.info("成功订阅 {} -> {}".format(chat_id, url))
else:
issue = "https://github.com/lanthora/rssbot-beta/issues"
text = '暂不支持此RSS,请<a href="{}">上报</a>'.format(issue)
except IndexError:
text = '请输入正确的格式:\n/sub url'
finally:
self.__send_html(chat_id, text)
def unsubscribe(self, update, context):
chat_id = update.message.chat_id
try:
url = update.message.text.split(' ')[1]
name = self.database.get_rss_by_url(url).title
self.database.del_sub(url, chat_id)
text = '已退订: <a href="{}">{}</a>'.format(url, name)
logging.info("取消订阅 {} -> {}".format(chat_id, url))
if(self.database.get_sub_num_by_url(url) == 0):
self.recently_used_elements.remove_cache(url)
except IndexError:
text = '请输入正确的格式:\n/unsub url'
except (TypeError, AttributeError):
text = '无此订阅'
finally:
self.__send_html(chat_id, text)
def rss(self, update, context):
chat_id = update.message.chat_id
sub_list = self.database.get_sub_by_chat_id(chat_id)
text = '<b>你的订阅:</b>\n'
active_rss = '▫️<a href="{}">{}</a>\n'
inactive_rss = '▪️<a href="{}">{}</a>\n'
if len(sub_list) == 0:
text = '暂无订阅'
else:
for rss in sub_list:
if bool(rss.active) == True:
text += active_rss.format(rss.url, rss.title)
else:
text += inactive_rss.format(rss.url, rss.title)
self.__send_html(chat_id, text)
def rename(self, update, context):
chat_id = update.message.chat_id
try:
url = update.message.text.split(' ')[1]
nickname = ' '.join(update.message.text.split(' ')[2:]).strip()
self.database.set_nickname(url, chat_id, nickname)
text = '别名已更新为: <a href="{}">{}</a>'.format(url, nickname)
except IndexError:
text = '请输入正确的格式:\n/rename url nickname'
except BaseException as base_exception:
logging.error(base_exception)
issue = "https://github.com/lanthora/rssbot-beta/issues"
text = '发生未知错误,请<a href="{}">上报</a>'.format(issue)
finally:
self.__send_html(chat_id, text)
def add_re(self, update, context):
chat_id = update.message.chat_id
try:
_url = update.message.text.split(' ')[1]
regular = ' '.join(update.message.text.split(' ')[2:]).strip()
self.re_db.set_re(chat_id, _url, regular)
text = '成功添加正则表达式 <a href="{}">{}</a>'.format(_url, regular)
re_add_log = "添加正则表达式 {} , {} -> {}"
logging.info(re_add_log.format(chat_id, _url, regular))
except IndexError:
text = '请输入正确的格式:\n/addre url regular'
except:
text = '发生未知错误'
finally:
self.__send_html(chat_id, text)
def del_re(self, update, context):
chat_id = update.message.chat_id
try:
_url = update.message.text.split(' ')[1]
rm_re_view = '成功移除正则表达式 <a href="{}">{}</a>'
text = rm_re_view.format(_url, self.re_db.get_re(chat_id, _url))
self.re_db.rm_re(chat_id, _url)
re_rm_log = "移除正则表达式 {} , {}"
logging.info(re_rm_log.format(chat_id, _url))
except IndexError:
text = '请输入正确的格式:\n/delre url'
except:
text = '发生未知错误'
finally:
self.__send_html(chat_id, text)
def run(self):
self.dp.add_handler(CommandHandler('start', self.start))
self.dp.add_handler(CommandHandler('sub', self.subscribe))
self.dp.add_handler(CommandHandler('unsub', self.unsubscribe))
self.dp.add_handler(CommandHandler('rss', self.rss))
self.dp.add_handler(CommandHandler('rename', self.rename))
self.dp.add_handler(CommandHandler('addre', self.add_re))
self.dp.add_handler(CommandHandler('delre', self.del_re))
self.dp.add_error_handler(self.__error)
self.jq.run_repeating(self.__refresh, self.interval, first=5)
self.jq.start()
self.updater.start_polling()
self.updater.idle()
def sig_handler(self, signal, frame):
NoSQLDB().dump()
self.exit = True
self.updater.stop()
self.jq.stop()