-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_rss.py
107 lines (90 loc) · 3.86 KB
/
cmd_rss.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
import logging
from khl import Bot, Message, MessageTypes, PublicMessage
from bot.databases.rss_queries import get_all_rss_list, get_rss_list, rss_subscribe, rss_unsubscribe
from bot.messages.card_messages_basic import exception_card_msg, help_card_msg, rss_card_msg_from_entry
from bot.utils.bot_utils import BotLogger
from bot.utils.bot_utils import BotUtils
from bot.utils.rss_utils import RssUtils
logger = logging.getLogger(__name__)
cmd_logger = BotLogger(logger)
def reg_rss_cmd(bot: Bot):
@bot.command(name='rss', case_sensitive=False)
async def cmd_rss(msg: Message, *args):
# not public message
if not isinstance(msg, PublicMessage):
return
# no permission
perm = await BotUtils.has_admin_and_manage(bot, msg.author_id, msg.guild.id)
if not perm:
return
# switch case
switch = {
"sub": sub,
"unsub": un_sub,
"unsuball": un_sub_all,
"list": list_subs,
"dump": list_all_subs,
}
try:
if args and len(args) > 0:
option = args[0]
result = switch.get(option, rss_help)
await result(msg, *args[1:])
else:
await rss_help(msg, *args)
cmd_logger.log_msg(msg)
except Exception as e:
await msg.reply(content=exception_card_msg(e), type=MessageTypes.CARD)
logger.exception(f"Failed {msg.content} for U:{msg.author_id}. {e}")
async def rss_help(msg: PublicMessage, *args):
await msg.reply(content=help_card_msg('rss'), type=MessageTypes.CARD)
async def sub(msg: PublicMessage, *args):
if len(args) == 0:
await rss_help(msg, *args)
return
sub_result = await rss_subscribe(msg.channel.id, msg.guild.id, *args)
if not sub_result.success:
return
url = args[0]
feed = sub_result.feed
# Check if the feed was parsed successfully
if feed.bozo:
logger.error(f"Feed is bozo {url}. {feed}")
if feed is None or len(feed.entries) == 0:
raise ValueError(f"Feed is None or len(feed.entries) == 0")
feed_title = RssUtils.parse_feed_title(feed)
entry = feed.entries[0]
content = rss_card_msg_from_entry(feed_title, entry)
try:
await msg.add_reaction('🆗')
await msg.reply(content=content, type=MessageTypes.CARD)
except Exception as e:
# issue, sometimes the image link leads to 403 or broken, solution remove image
logger.warning(f"Failed reply, retry compatibility mode. {e}")
content = rss_card_msg_from_entry(feed_title, entry, True)
await msg.reply(content=content, type=MessageTypes.CARD)
async def un_sub(msg: PublicMessage, *args):
if len(args) == 0:
await rss_help(msg, args)
return
wildcard = {
'*',
'all'
}
if args[0] in wildcard:
await un_sub_all(msg)
else:
result = await rss_unsubscribe(msg.channel.id, *args)
if result:
await msg.add_reaction('⭕')
async def un_sub_all(msg: PublicMessage, *args):
rss_url_list = await get_rss_list(msg.channel.id)
await un_sub(msg, *rss_url_list)
async def list_subs(msg: PublicMessage, *args):
rss_url_list = await get_rss_list(msg.channel.id)
encapsule_and_joined = '\n'.join([f'`{string}`' for string in rss_url_list])
await msg.reply(f"🔖 RSS已订阅列表:\n{encapsule_and_joined}")
async def list_all_subs(msg: PublicMessage, *args):
rss_url_list = await get_all_rss_list()
encapsule_and_joined = '\n'.join([f'`{string}`' for string in rss_url_list])
await msg.reply(f"🔖 RSS全部订阅列表:\n{encapsule_and_joined}")