From 1e41f8453856cf1b5a2163c196a6814ef97e9f2e Mon Sep 17 00:00:00 2001 From: Kyomotoi Date: Mon, 14 Feb 2022 19:19:16 +0800 Subject: =?UTF-8?q?=F0=9F=9A=9A=20=E5=AF=B9=E6=8F=92=E4=BB=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9B=B8=E5=85=B3=E8=BF=9B=E8=A1=8C=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATRI/plugins/applet/__init__.py | 21 +++++++++ ATRI/plugins/applet/data_source.py | 88 ++++++++++++++++++++++++++++++++++++++ ATRI/plugins/rich/__init__.py | 21 --------- ATRI/plugins/rich/data_source.py | 88 -------------------------------------- test/test_plugin_applet.py | 28 ++++++++++++ test/test_plugin_rich.py | 28 ------------ 6 files changed, 137 insertions(+), 137 deletions(-) create mode 100644 ATRI/plugins/applet/__init__.py create mode 100644 ATRI/plugins/applet/data_source.py delete mode 100644 ATRI/plugins/rich/__init__.py delete mode 100644 ATRI/plugins/rich/data_source.py create mode 100644 test/test_plugin_applet.py delete mode 100644 test/test_plugin_rich.py diff --git a/ATRI/plugins/applet/__init__.py b/ATRI/plugins/applet/__init__.py new file mode 100644 index 0000000..d6e2ab6 --- /dev/null +++ b/ATRI/plugins/applet/__init__.py @@ -0,0 +1,21 @@ +from nonebot.adapters.onebot.v11 import MessageEvent +from nonebot.adapters.onebot.v11.helpers import Cooldown + +from ATRI.log import logger as log +from .data_source import Applet + + +bili_applet = Applet().on_message("小程序检测", "小程序爪巴", block=False) + + +@bili_applet.handle([Cooldown(3)]) +async def _fk_bili(event: MessageEvent): + msg = str(event.message) + try: + result, is_ok = await Applet().fk_bili(msg) + except Exception: + return + log.debug(result, is_ok) + if not is_ok: + return + await bili_applet.finish(result) diff --git a/ATRI/plugins/applet/data_source.py b/ATRI/plugins/applet/data_source.py new file mode 100644 index 0000000..fb7dc79 --- /dev/null +++ b/ATRI/plugins/applet/data_source.py @@ -0,0 +1,88 @@ +import re + +from ATRI.service import Service +from ATRI.utils import request +from ATRI.rule import is_in_service +from ATRI.exceptions import RequestError + + +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 + +__doc__ = """啥b腾讯小程序给👴爪巴 +目前只整了b站的 +""" + + +class Applet(Service): + def __init__(self): + Service.__init__(self, "小程序处理", __doc__, rule=is_in_service("小程序处理")) + + @staticmethod + def _bv_dec(x) -> str: + r = 0 + for i in range(6): + r += tr[x[s[i]]] * 58 ** i + result = "av" + str((r - add) ^ xor) + return result + + @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) + + @classmethod + async def fk_bili(cls, text: str) -> tuple: + msg = text.replace("\\", "") + bv = False + if "https://b23" in msg: + pattern = r"https://b23\.tv/[a-zA-Z0-9]+" + burl = re.findall(pattern, msg) + u = burl[0] + + try: + res = await request.get(u) + except: + return "Request failed!", False + + bv_pattern = r"video/BV[a-zA-Z0-9]+" + try: + tu = str(res.url) + t_bv = re.findall(bv_pattern, tu) + bv = t_bv[0].replace("video/", "") + except: + return "Deal bv code failed!", False + av = cls._bv_dec(bv).replace("av", "") + + else: + pattern = r"[bB][vV][a-zA-Z0-9]+" + try: + bv = re.findall(pattern, msg)[0] + except: + return "Deal bv code failed!", False + av = cls._bv_dec(bv).replace("av", "") + + url = URL + av + try: + res = await request.get(url) + except RequestError: + return "Request failed!", False + res_data = res.json() + data = res_data["data"] + + result = ( + f"{data['bvid']} INFO:\n" + f"Title: {data['title']}\n" + f"Link: {data['short_link']}" + ) + return result, True diff --git a/ATRI/plugins/rich/__init__.py b/ATRI/plugins/rich/__init__.py deleted file mode 100644 index 2d8f8c6..0000000 --- a/ATRI/plugins/rich/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -from nonebot.adapters.onebot.v11 import MessageEvent -from nonebot.adapters.onebot.v11.helpers import Cooldown - -from ATRI.log import logger as log -from .data_source import Rich - - -bili_rich = Rich().on_message("小程序检测", "小程序爪巴", block=False) - - -@bili_rich.handle([Cooldown(3)]) -async def _fk_bili(event: MessageEvent): - msg = str(event.message) - try: - result, is_ok = await Rich().fk_bili(msg) - except Exception: - return - log.debug(result, is_ok) - if not is_ok: - return - await bili_rich.finish(result) diff --git a/ATRI/plugins/rich/data_source.py b/ATRI/plugins/rich/data_source.py deleted file mode 100644 index 042fd8b..0000000 --- a/ATRI/plugins/rich/data_source.py +++ /dev/null @@ -1,88 +0,0 @@ -import re - -from ATRI.service import Service -from ATRI.utils import request -from ATRI.rule import is_in_service -from ATRI.exceptions import RequestError - - -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 - -__doc__ = """啥b腾讯小程序给👴爪巴 -目前只整了b站的 -""" - - -class Rich(Service): - def __init__(self): - Service.__init__(self, "小程序处理", __doc__, rule=is_in_service("小程序处理")) - - @staticmethod - def _bv_dec(x) -> str: - r = 0 - for i in range(6): - r += tr[x[s[i]]] * 58 ** i - result = "av" + str((r - add) ^ xor) - return result - - @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) - - @classmethod - async def fk_bili(cls, text: str) -> tuple: - msg = text.replace("\\", "") - bv = False - if "https://b23" in msg: - pattern = r"https://b23\.tv/[a-zA-Z0-9]+" - burl = re.findall(pattern, msg) - u = burl[0] - - try: - res = await request.get(u) - except: - return "Request failed!", False - - bv_pattern = r"video/BV[a-zA-Z0-9]+" - try: - tu = str(res.url) - t_bv = re.findall(bv_pattern, tu) - bv = t_bv[0].replace("video/", "") - except: - return "Deal bv code failed!", False - av = cls._bv_dec(bv).replace("av", "") - - else: - pattern = r"[bB][vV][a-zA-Z0-9]+" - try: - bv = re.findall(pattern, msg)[0] - except: - return "Deal bv code failed!", False - av = cls._bv_dec(bv).replace("av", "") - - url = URL + av - try: - res = await request.get(url) - except RequestError: - return "Request failed!", False - res_data = res.json() - data = res_data["data"] - - result = ( - f"{data['bvid']} INFO:\n" - f"Title: {data['title']}\n" - f"Link: {data['short_link']}" - ) - return result, True diff --git a/test/test_plugin_applet.py b/test/test_plugin_applet.py new file mode 100644 index 0000000..d2c0654 --- /dev/null +++ b/test/test_plugin_applet.py @@ -0,0 +1,28 @@ +import pytest +from nonebug import App + +from .utils import make_fake_message, make_fake_event + + +@pytest.mark.asyncio +async def test_bili_rich(app: App): + from ATRI.plugins.applet import bili_rich + + Message = make_fake_message() + + async with app.test_matcher(bili_rich) as ctx: + bot = ctx.create_bot() + + msg = Message("BV1Ff4y1C7YR") + event = make_fake_event(_message=msg)() + + ctx.receive_event(bot, event) + ctx.should_call_send( + event, + """ + BV1Ff4y1C7YR INFO: + Title: 【8K30fps】这可能是画质最高的Rick Roll (doge) + Link: https://b23.tv/BV1Ff4y1C7YR + """, + True, + ) diff --git a/test/test_plugin_rich.py b/test/test_plugin_rich.py deleted file mode 100644 index 3e63f4a..0000000 --- a/test/test_plugin_rich.py +++ /dev/null @@ -1,28 +0,0 @@ -import pytest -from nonebug import App - -from .utils import make_fake_message, make_fake_event - - -@pytest.mark.asyncio -async def test_bili_rich(app: App): - from ATRI.plugins.rich import bili_rich - - Message = make_fake_message() - - async with app.test_matcher(bili_rich) as ctx: - bot = ctx.create_bot() - - msg = Message("BV1Ff4y1C7YR") - event = make_fake_event(_message=msg)() - - ctx.receive_event(bot, event) - ctx.should_call_send( - event, - """ - BV1Ff4y1C7YR INFO: - Title: 【8K30fps】这可能是画质最高的Rick Roll (doge) - Link: https://b23.tv/BV1Ff4y1C7YR - """, - True, - ) -- cgit v1.2.3