summaryrefslogtreecommitdiff
path: root/ATRI/plugins/applet/data_source.py
blob: 296c337d4990a58d5d51f3ac3c0c459ba9262c2a (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
import re

from ATRI.utils import request


URL = "https://api.kyomotoi.moe/api/bilibili/v3/video_info?aid="

table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"
tr = dict()
for i in range(58):
    tr[table[i]] = i
s = [11, 10, 3, 8, 4, 6]
xor = 177451812
add = 8728348608


class Applet:
    @staticmethod
    def _bv_dec(x) -> str:
        r = 0
        for i in range(6):
            r += tr[x[s[i]]] * 58**i
        return str((r - add) ^ xor)

    @staticmethod
    def _bv_enc(x) -> str:
        x = (x ^ xor) + add
        r = list("BV1  4 1 7  ")
        for i in range(6):
            r[s[i]] = table[x // 58**i % 58]
        return "".join(r)

    @staticmethod
    async def bili_request(url: str) -> str:
        req = await request.get(url)
        return req.headers.get("location")

    @staticmethod
    def bili_video_code_catcher(text: str) -> str:
        pattern = re.compile(r"BV[0-9A-Za-z]{10}")
        result = pattern.findall(text)
        return result[0] if result else ""

    @classmethod
    async def msg_builder(cls, text: str) -> tuple:
        bv = cls.bili_video_code_catcher(text)
        if not bv:
            pattern = r"https://b23.tv/[a-z0-9A-z]{6,7}"
            burl = re.findall(pattern, text)
            u = burl[0] if burl else str()
            if not u:
                return None, False

            rep = await cls.bili_request(u)
            bv = cls.bili_video_code_catcher(rep)
            av = cls._bv_dec(bv)

        else:
            av = cls._bv_dec(bv)

        url = URL + av
        req = await request.get(url)
        res_data = req.json()
        data = res_data["data"]

        result = (
            f"{data['bvid']} INFO:\n"
            f"Title: {data['title']}\n"
            f"Link: {data['short_link']}"
        )
        return result, True