summaryrefslogtreecommitdiff
path: root/ATRI/plugins/rich/__init__.py
blob: cd4c5bd68f543786c20fe1abbc03ee012025a9b7 (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
import re
import json
from random import choice
from aiohttp.client import ClientSession

from nonebot.adapters.cqhttp import Bot, MessageEvent
from nonebot.adapters.cqhttp.message import MessageSegment

from ATRI.service import Service as sv
from ATRI.utils.request import get_bytes
from ATRI.utils.list import count_list, del_list_aim

from .data_source import dec


temp_list = []
img_url = [
    "https://cdn.jsdelivr.net/gh/Kyomotoi/CDN@master/project/ATRI/fkrich.png",
    "https://cdn.jsdelivr.net/gh/Kyomotoi/CDN@master/project/ATRI/xixi.jpg"
]


bilibili_rich = sv.on_message()

@bilibili_rich.handle()
async def _bilibili_rich(bot: Bot, event: MessageEvent) -> None:
    global temp_list
    try:
        msg = str(event.raw_message).replace("\\", "")
        bv = False
        
        if "qqdocurl" not in msg:
            if "av" in msg:
                av = re.findall(r"(av\d+)", msg)[0].replace('av', '')
            else:
                bv = re.findall(r"(BV\w+)", msg)
                av = str(dec(bv[0]))
        else:
            patt = r"(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+"
            bv_url = re.findall(patt, msg)
            bv_url = bv_url[3]
            async with ClientSession() as session:
                async with session.get(url=bv_url) as r:
                    bv = re.findall(r"(BV\w+)", str(r.url))
                    av = dec(bv[0])
        
        if not bv:
            if "av" in msg:
                av = re.findall(r"(av\d+)", msg)[0].replace('av', '')
            else:
                return
        
        if count_list(temp_list, av) == 4:
            await bot.send(event, "你是怕别人看不到么发这么多次?")
            temp_list = del_list_aim(temp_list, av)
            return
        
        temp_list.append(av)
        
        URL = f"https://api.kyomotoi.moe/api/bilibili/v2/?aid={av}"
        data = json.loads(await get_bytes(URL))['data']
        repo = (
            f"{data['bvid']} INFO:\n"
            f"Title: {data['title']}\n"
            f"Link: {data['short_link']}\n"
            "にまねげぴのTencent rich!"
        )
        await bot.send(
            event,
            MessageSegment.image(file=choice(img_url)))
        await bilibili_rich.finish(repo)
    except BaseException:
        return