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
|
import xmltodict
from nonebot.permission import SUPERUSER
from nonebot.adapters.onebot.v11 import GROUP_OWNER, GROUP_ADMIN
from ATRI.service import Service
from ATRI.rule import is_in_service
from ATRI.exceptions import RssError
from ATRI.utils import request, gen_random_str
from .db import DB
class RssHubSubscriptor(Service):
def __init__(self):
Service.__init__(
self,
"rss.rsshub",
"Rss的Rsshub支持",
rule=is_in_service("rss.rsshub"),
permission=SUPERUSER | GROUP_OWNER | GROUP_ADMIN,
main_cmd="/rss.rsshub",
)
async def __add_sub(self, _id: str, group_id: int):
try:
async with DB() as db:
await db.add_sub(_id, group_id)
except Exception:
raise RssError("rss.rsshub: 添加订阅失败")
async def update_sub(self, _id: str, group_id: int, update_map: dict):
try:
async with DB() as db:
await db.update_sub(_id, group_id, update_map)
except Exception:
raise RssError("rss.rsshub: 更新订阅失败")
async def __del_sub(self, _id: str, group_id: int):
try:
async with DB() as db:
await db.del_sub({"_id": _id, "group_id": group_id})
except Exception:
raise RssError("rss.rsshub: 删除订阅失败")
async def get_sub_list(self, query_map: dict) -> list:
try:
async with DB() as db:
return await db.get_sub_list(query_map)
except Exception:
raise RssError("rss.rsshub: 获取订阅列表失败")
async def get_all_subs(self) -> list:
try:
async with DB() as db:
return await db.get_all_subs()
except Exception:
raise RssError("rss.rsshub: 获取所有订阅失败")
async def add_sub(self, url: str, group_id: int) -> str:
try:
resp = await request.get(url)
except Exception:
raise RssError("rss.rsshub: 请求链接失败")
if "RSSHub" not in resp.text:
return "该链接不含RSSHub内容"
xml_data = resp.read()
data = xmltodict.parse(xml_data)
check_url = data["rss"]["channel"]["link"]
query_result = await self.get_sub_list(
{"raw_link": check_url, "group_id": group_id}
)
if query_result:
_id = query_result[0]._id
return f"该链接已经订阅过啦! ID: {_id}"
_id = gen_random_str(6)
title = data["rss"]["channel"]["title"]
disc = data["rss"]["channel"]["description"]
await self.__add_sub(_id, group_id)
await self.update_sub(
_id,
group_id,
{
"title": title,
"rss_link": url,
"discription": disc,
},
)
return f"订阅成功! ID: {_id}"
async def del_sub(self, _id: str, group_id: int) -> str:
query_result = await self.get_sub_list({"_id": _id, "group_id": group_id})
if not query_result:
return "没有找到该订阅..."
await self.__del_sub(_id, group_id)
return f"成功取消ID为 {_id} 的订阅"
async def get_rsshub_info(self, url: str) -> dict:
try:
resp = await request.get(url)
except Exception:
raise RssError("rss.rsshub: 请求链接失败")
if "RSSHub" not in resp.text:
return dict()
xml_data = resp.read()
data = xmltodict.parse(xml_data)
return data["rss"]["channel"]
|