From 8b4aa65495d98cfd244153824f5962ac71951cca Mon Sep 17 00:00:00 2001 From: Kyomotoi Date: Fri, 18 Feb 2022 00:44:08 +0800 Subject: =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8Db=E7=AB=99=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=97=A0=E6=B3=95=E6=8D=95=E6=8D=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATRI/plugins/applet/__init__.py | 12 +++---- ATRI/plugins/applet/data_source.py | 68 ++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/ATRI/plugins/applet/__init__.py b/ATRI/plugins/applet/__init__.py index d6e2ab6..233df3e 100644 --- a/ATRI/plugins/applet/__init__.py +++ b/ATRI/plugins/applet/__init__.py @@ -1,21 +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 = Applet().on_message("b站小程序检测", "B站小程序爪巴", block=False) @bili_applet.handle([Cooldown(3)]) -async def _fk_bili(event: MessageEvent): - msg = str(event.message) +async def _(event: MessageEvent): + msg = str(event.get_message()) try: - result, is_ok = await Applet().fk_bili(msg) + result, is_ok = await Applet().msg_builder(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 index fb7dc79..d2b5151 100644 --- a/ATRI/plugins/applet/data_source.py +++ b/ATRI/plugins/applet/data_source.py @@ -3,7 +3,6 @@ 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=" @@ -16,9 +15,7 @@ s = [11, 10, 3, 8, 4, 6] xor = 177451812 add = 8728348608 -__doc__ = """啥b腾讯小程序给👴爪巴 -目前只整了b站的 -""" +__doc__ = "啥b腾讯小程序给👴爪巴\n目前只整了b站的" class Applet(Service): @@ -30,8 +27,7 @@ class Applet(Service): r = 0 for i in range(6): r += tr[x[s[i]]] * 58 ** i - result = "av" + str((r - add) ^ xor) - return result + return str((r - add) ^ xor) @staticmethod def _bv_enc(x) -> str: @@ -41,43 +37,37 @@ class Applet(Service): 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 + @staticmethod + async def bili_request(url: str) -> str: + req = await request.get(url) + return req.headers.get("location") - 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", "") + @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: - 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", "") - + av = cls._bv_dec(bv) + url = URL + av - try: - res = await request.get(url) - except RequestError: - return "Request failed!", False - res_data = res.json() + req = await request.get(url) + res_data = req.json() data = res_data["data"] result = ( -- cgit v1.2.3