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

from nonebot.adapters.cqhttp import Bot, MessageEvent
from nonebot.plugin import on_message

from ATRI.rule import is_in_banlist, is_in_dormant
from ATRI.utils.request import get_bytes
from .data_source import dec

bilibili_rich = on_message(
    rule=is_in_banlist() & is_in_dormant()
)

@bilibili_rich.handle()
async def _bilibili_rich(bot: Bot, event: MessageEvent) -> None:
    msg = str(event.raw_message)
    bv = False
    
    if "qqdocurl" not in msg:
        if "av" in msg:
            av = re.findall(r"(av\d+)", msg)[0].replace('av', '')
        else:
            try:
                bv = re.findall(r"(BV\w+)", msg)
                av = str(dec(bv[0]))
            except:
                return
    else:
        bv_url = re.findall(r"(..........b23...\S+\=)", msg)
        bv_url = bv_url[0].replace("\\", "")
        print(bv_url)
        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
    
    try:
        URL = f"https://api.kyomotoi.moe/api/bilibili/v2/?aid={av}"
    except:
        return
    data = json.loads(await get_bytes(URL))['data']
    repo = (
        f"{av} INFO:\n"
        f"Title: {data['title']}\n"
        f"Bid: {data['bvid']}\n"
        f"View: {data['stat']['view']} Like: {data['stat']['like']}\n"
        f"Coin: {data['stat']['coin']} Share: {data['stat']['share']}\n"
        f"Link: {data['short_link']}\n"
        "にまねげぴのTencent rich!"
    )
    await bilibili_rich.finish(repo)