summaryrefslogtreecommitdiff
path: root/ATRI/plugins/rss/rss_mikanan/data_source.py
blob: 6397c709634a034a0f4a199e75178aa81671eb29 (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
95
96
97
98
99
100
101
102
103
104
105
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 RssMikananSubscriptor(Service):
    def __init__(self):
        Service.__init__(
            self,
            "rss.mikan",
            "Rss的mikan支持",
            rule=is_in_service("rss.mikan"),
            permission=SUPERUSER | GROUP_OWNER | GROUP_ADMIN,
            main_cmd="/rss.mikan",
        )

    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.mikan: 添加订阅失败")

    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.mikan: 更新订阅失败")

    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.mikan: 删除订阅失败")

    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.mikan: 获取订阅列表失败")

    async def get_all_subs(self) -> list:
        try:
            async with DB() as db:
                return await db.get_all_subs()
        except Exception:
            raise RssError("rss.mikan: 获取所有订阅失败")

    async def add_sub(self, url: str, group_id: int) -> str:
        data = await self.get_mikan_info(url)
        if not data:
            return "该链接不含mikan内容"

        rss_link = data["link"]

        query_result = await self.get_sub_list(
            {"rss_link": rss_link, "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": rss_link, "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_mikan_info(self, url: str) -> dict:
        if "mikanani.me" not in url:
            return dict()

        try:
            resp = await request.get(url)
        except Exception:
            RssError("rss.mikan: 请求链接失败")

        xml_data = resp.read()
        data = xmltodict.parse(xml_data)
        return data["rss"]["@version"]["channel"]