summaryrefslogtreecommitdiff
path: root/ATRI/plugins/rss/rss_rsshub/data_source.py
blob: 9ca1c04e8af13d492fd7038a5e51d5cded02c934 (plain)
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
import xmltodict

from ATRI.exceptions import RssError
from ATRI.utils import request, gen_random_str

from .db import DB


class RssHubSubscriptor:
    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:
        data = await self.get_rsshub_info(url)
        if not data:
            return "该链接不含RSSHub内容"

        check_url = data["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["title"]
        disc = data["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"]