diff --git a/src/plugins/nonebot_bison/platform/bilibili.py b/src/plugins/nonebot_bison/platform/bilibili.py index b6c83c9..9d62045 100644 --- a/src/plugins/nonebot_bison/platform/bilibili.py +++ b/src/plugins/nonebot_bison/platform/bilibili.py @@ -42,7 +42,9 @@ class Bilibili(NewMessage): async def parse_target(self, target_text: str) -> Target: if re.match(r"\d+", target_text): return Target(target_text) - elif match := re.match(r"(?:https?://)?space.bilibili.com/(\d+)", target_text): + elif match := re.match( + r"(?:https?://)?space\.bilibili\.com/(\d+)", target_text + ): return Target(match.group(1)) else: raise Platform.ParseTargetException() diff --git a/src/plugins/nonebot_bison/platform/ncm_artist.py b/src/plugins/nonebot_bison/platform/ncm_artist.py index a30072f..9895dde 100644 --- a/src/plugins/nonebot_bison/platform/ncm_artist.py +++ b/src/plugins/nonebot_bison/platform/ncm_artist.py @@ -1,3 +1,4 @@ +import re from typing import Any, Optional import httpx @@ -18,6 +19,7 @@ class NcmArtist(NewMessage): schedule_kw = {"minutes": 1} name = "网易云-歌手" has_target = True + parse_target_promot = "请输入歌手主页(包含数字ID)的链接" async def get_target_name(self, target: Target) -> Optional[str]: async with httpx.AsyncClient() as client: @@ -30,6 +32,16 @@ class NcmArtist(NewMessage): return return res_data["artist"]["name"] + async def parse_target(self, target_text: str) -> Target: + if re.match(r"^\d+$", target_text): + return Target(target_text) + elif match := re.match( + r"(?:https?://)?music\.163\.com/#/artist\?id=(\d+)", target_text + ): + return Target(match.group(1)) + else: + raise self.ParseTargetException() + async def get_sub_list(self, target: Target) -> list[RawPost]: async with httpx.AsyncClient() as client: res = await client.get( diff --git a/src/plugins/nonebot_bison/platform/ncm_radio.py b/src/plugins/nonebot_bison/platform/ncm_radio.py index 20abb52..973cc1b 100644 --- a/src/plugins/nonebot_bison/platform/ncm_radio.py +++ b/src/plugins/nonebot_bison/platform/ncm_radio.py @@ -1,3 +1,4 @@ +import re from typing import Any, Optional import httpx @@ -18,6 +19,7 @@ class NcmRadio(NewMessage): schedule_kw = {"minutes": 10} name = "网易云-电台" has_target = True + parse_target_promot = "请输入主播电台主页(包含数字ID)的链接" async def get_target_name(self, target: Target) -> Optional[str]: async with httpx.AsyncClient() as client: @@ -31,6 +33,16 @@ class NcmRadio(NewMessage): return return res_data["programs"][0]["radio"]["name"] + async def parse_target(self, target_text: str) -> Target: + if re.match(r"^\d+$", target_text): + return Target(target_text) + elif match := re.match( + r"(?:https?://)?music\.163\.com/#/djradio\?id=(\d+)", target_text + ): + return Target(match.group(1)) + else: + raise self.ParseTargetException() + async def get_sub_list(self, target: Target) -> list[RawPost]: async with httpx.AsyncClient() as client: res = await client.post( diff --git a/src/plugins/nonebot_bison/platform/weibo.py b/src/plugins/nonebot_bison/platform/weibo.py index 7d5ef57..b2b211d 100644 --- a/src/plugins/nonebot_bison/platform/weibo.py +++ b/src/plugins/nonebot_bison/platform/weibo.py @@ -45,7 +45,7 @@ class Weibo(NewMessage): async def parse_target(self, target_text: str) -> Target: if re.match(r"\d+", target_text): return Target(target_text) - elif match := re.match(r"(?:https?://)?weibo.com/u/(\d+)", target_text): + elif match := re.match(r"(?:https?://)?weibo\.com/u/(\d+)", target_text): # 都2202年了应该不会有http了吧,不过还是防一手 return Target(match.group(1)) else: @@ -139,9 +139,9 @@ class Weibo(NewMessage): "https://m.weibo.cn/detail/{}".format(info["mid"]), headers=header ) try: - full_json_text = re.search( - r'"status": ([\s\S]+),\s+"hotScheme"', res.text - ).group(1) + match = re.search(r'"status": ([\s\S]+),\s+"hotScheme"', res.text) + assert match + full_json_text = match.group(1) info = json.loads(full_json_text) except: logger.info( diff --git a/tests/platforms/test_bilibili.py b/tests/platforms/test_bilibili.py index 5fc2b28..afcd426 100644 --- a/tests/platforms/test_bilibili.py +++ b/tests/platforms/test_bilibili.py @@ -1,3 +1,5 @@ +import typing + import pytest from httpx import Response from nonebug.app import App @@ -10,6 +12,10 @@ def bing_dy_list(): return get_json("bilibili_bing_list.json")["data"]["cards"] +if typing.TYPE_CHECKING: + from nonebot_bison.platform.bilibili import Bilibili + + @pytest.fixture def bilibili(app: App): from nonebot_bison.platform import platform_manager @@ -46,3 +52,20 @@ async def test_dynamic_forward(bilibili, bing_dy_list): + "\n--------------\n" + "#明日方舟#\n【新增服饰】\n//殿堂上的游禽 - 星极\n塞壬唱片偶像企划《闪耀阶梯》特供服饰/殿堂上的游禽。星极自费参加了这项企划,尝试着用大众能接受的方式演绎天空之上的故事。\n\n_____________\n谦逊留给观众,骄傲发自歌喉,此夜,唯我璀璨。 " ) + + +async def test_parse_target(bilibili: "Bilibili"): + from nonebot_bison.platform.platform import Platform + + res = await bilibili.parse_target( + "https://space.bilibili.com/161775300?from=search&seid=130517740606234234234&spm_id_from=333.337.0.0" + ) + assert res == "161775300" + res2 = await bilibili.parse_target( + "space.bilibili.com/161775300?from=search&seid=130517740606234234234&spm_id_from=333.337.0.0" + ) + assert res2 == "161775300" + with pytest.raises(Platform.ParseTargetException): + await bilibili.parse_target( + "https://www.bilibili.com/video/BV1qP4y1g738?spm_id_from=333.999.0.0" + ) diff --git a/tests/platforms/test_ncm_artist.py b/tests/platforms/test_ncm_artist.py index 354b2a6..8f8f4cd 100644 --- a/tests/platforms/test_ncm_artist.py +++ b/tests/platforms/test_ncm_artist.py @@ -1,4 +1,5 @@ import time +import typing import pytest import respx @@ -7,6 +8,9 @@ from nonebug.app import App from .utils import get_json +if typing.TYPE_CHECKING: + from nonebot_bison.platform.ncm_artist import NcmArtist + @pytest.fixture def ncm_artist(app: App): @@ -48,3 +52,16 @@ async def test_fetch_new(ncm_artist, ncm_artist_0, ncm_artist_1, dummy_user_subi assert post.target_type == "ncm-artist" assert post.text == "新专辑发布:Y1K" assert post.url == "https://music.163.com/#/album?id=131074504" + + +async def test_parse_target(ncm_artist: "NcmArtist"): + from nonebot_bison.platform.platform import Platform + + res = await ncm_artist.parse_target("32540734") + assert res == "32540734" + res = await ncm_artist.parse_target("https://music.163.com/#/artist?id=32540734") + assert res == "32540734" + res = await ncm_artist.parse_target("music.163.com/#/artist?id=32540734") + assert res == "32540734" + with pytest.raises(Platform.ParseTargetException): + await ncm_artist.parse_target("music.163.com/#/rad?id=32540734") diff --git a/tests/platforms/test_ncm_radio.py b/tests/platforms/test_ncm_radio.py index d191c6f..9bab7d8 100644 --- a/tests/platforms/test_ncm_radio.py +++ b/tests/platforms/test_ncm_radio.py @@ -1,4 +1,5 @@ import time +import typing import pytest import respx @@ -7,6 +8,9 @@ from nonebug.app import App from .utils import get_json +if typing.TYPE_CHECKING: + from nonebot_bison.platform.ncm_radio import NcmRadio + @pytest.fixture def ncm_radio(app: App): @@ -53,3 +57,14 @@ async def test_fetch_new(ncm_radio, ncm_radio_0, ncm_radio_1, dummy_user_subinfo "http://p1.music.126.net/H5em5xUNIYXcjJhOmeaSqQ==/109951166647436789.jpg" ] assert post.target_name == "《明日方舟》游戏原声OST" + + +async def test_parse_target(ncm_radio: "NcmRadio"): + res = await ncm_radio.parse_target("https://music.163.com/#/djradio?id=793745436") + assert res == "793745436" + res = await ncm_radio.parse_target("music.163.com/#/djradio?id=793745436") + assert res == "793745436" + res = await ncm_radio.parse_target("793745436") + assert res == "793745436" + with pytest.raises(ncm_radio.ParseTargetException): + await ncm_radio.parse_target("music.163.com/#/alm?id=793745436") diff --git a/tests/platforms/test_weibo.py b/tests/platforms/test_weibo.py index c241408..742d9a3 100644 --- a/tests/platforms/test_weibo.py +++ b/tests/platforms/test_weibo.py @@ -1,3 +1,4 @@ +import typing from datetime import datetime import feedparser @@ -9,6 +10,9 @@ from pytz import timezone from .utils import get_file, get_json +if typing.TYPE_CHECKING: + from nonebot_bison.platform.weibo import Weibo + @pytest.fixture def weibo(app: App): @@ -125,3 +129,16 @@ def test_chaohua_tag(weibo): tags = weibo.get_tags(test_post) assert "刚出生的小羊驼长啥样" in tags assert "小羊驼三三超话" in tags + + +async def test_parse_target(weibo: "Weibo"): + from nonebot_bison.platform.platform import Platform + + res = await weibo.parse_target("https://weibo.com/u/6441489862") + assert res == "6441489862" + res = await weibo.parse_target("weibo.com/u/6441489862") + assert res == "6441489862" + res = await weibo.parse_target("6441489862") + assert res == "6441489862" + with pytest.raises(Platform.ParseTargetException): + await weibo.parse_target("https://weibo.com/arknights") diff --git a/tests/test_config_manager_add.py b/tests/test_config_manager_add.py index e341929..8064f0d 100644 --- a/tests/test_config_manager_add.py +++ b/tests/test_config_manager_add.py @@ -530,105 +530,3 @@ async def test_add_with_bilibili_target_parser(app: App): assert sub["cats"] == [platform_manager["bilibili"].reverse_category["视频"]] assert sub["target_type"] == "bilibili" assert sub["target_name"] == "明日方舟" - - -@pytest.mark.asyncio -@respx.mock -async def test_add_with_weibo_target_parser(app: App): - from nonebot.adapters.onebot.v11.event import Sender - from nonebot.adapters.onebot.v11.message import Message - from nonebot_bison.config import Config - from nonebot_bison.config_manager import add_sub_matcher, common_platform - from nonebot_bison.platform import platform_manager - from nonebot_bison.platform.weibo import Weibo - - config = Config() - config.user_target.truncate() - - ak_list_router = respx.get( - "https://m.weibo.cn/api/container/getIndex?containerid=1005056279793937" - ) - ak_list_router.mock( - return_value=Response(200, json=get_json("weibo_ak_profile.json")) - ) - ak_list_bad_router = respx.get( - "https://m.weibo.cn/api/container/getIndex?containerid=100505000" - ) - ak_list_bad_router.mock( - return_value=Response(200, json=get_json("weibo_err_profile.json")) - ) - async with app.test_matcher(add_sub_matcher) as ctx: - bot = ctx.create_bot() - event_1 = fake_group_message_event( - message=Message("添加订阅"), - sender=Sender(card="", nickname="test", role="admin"), - to_me=True, - ) - ctx.receive_event(bot, event_1) - ctx.should_pass_rule() - ctx.should_call_send( - event_1, - Message( - BotReply.add_reply_on_platform( - platform_manager=platform_manager, common_platform=common_platform - ) - ), - True, - ) - event_2 = fake_group_message_event( - message=Message("weibo"), sender=fake_admin_user - ) - ctx.receive_event(bot, event_2) - ctx.should_call_send( - event_2, - Message(BotReply.add_reply_on_id(Weibo)), - True, - ) - event_3_err1 = fake_group_message_event( - message=Message( - "https://space.bilbili.com/161775300?from=search&seid=13051774060625135297&spm_id_from=333.337.0.0" - ), - sender=fake_admin_user, - ) - ctx.receive_event(bot, event_3_err1) - ctx.should_call_send( - event_3_err1, BotReply.add_reply_on_target_parse_input_error, True - ) - ctx.should_rejected() - - event_3_err2 = fake_group_message_event( - message=Message("https://weibo.com/arknights"), - sender=fake_admin_user, - ) - ctx.receive_event(bot, event_3_err2) - ctx.should_call_send( - event_3_err2, BotReply.add_reply_on_target_parse_input_error, True - ) - ctx.should_rejected() - - event_3_err3 = fake_group_message_event( - message=Message("https://weibo.com/u/ark6279793937"), - sender=fake_admin_user, - ) - ctx.receive_event(bot, event_3_err3) - ctx.should_call_send( - event_3_err3, BotReply.add_reply_on_target_parse_input_error, True - ) - ctx.should_rejected() - - event_3_ok = fake_group_message_event( - message=Message("http://weibo.com/u/6279793937"), sender=fake_admin_user - ) # 特意写成http测试正则 - ctx.receive_event(bot, event_3_ok) - ctx.should_call_send( - event_3_ok, - BotReply.add_reply_on_target_confirm( - "weibo", "明日方舟Arknights", "6279793937" - ), - True, - ) - ctx.should_call_send( - event_3_ok, - Message(BotReply.add_reply_on_cats(platform_manager, "weibo")), - True, - )