From 2956dd99bcb086b6bbe9e2ba7fb052c4ce3fadae Mon Sep 17 00:00:00 2001 From: Azide Date: Thu, 7 Mar 2024 19:32:42 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=BF=87=E6=BB=A4=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E8=AE=BF=E9=97=AE=E7=9A=84url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_bison/platform/arknights.py | 8 +++- poetry.lock | 5 +-- tests/platforms/test_arknights.py | 67 +++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/nonebot_bison/platform/arknights.py b/nonebot_bison/platform/arknights.py index c0b2f563..efba4e1e 100644 --- a/nonebot_bison/platform/arknights.py +++ b/nonebot_bison/platform/arknights.py @@ -3,7 +3,7 @@ from httpx import AsyncClient from bs4 import BeautifulSoup as bs -from pydantic import Field, BaseModel +from pydantic import Field, AnyUrl, BaseModel from nonebot.compat import type_validate_python from ..post import Post @@ -113,7 +113,11 @@ def title_escape(text: str) -> str: title=title, nickname="明日方舟游戏内公告", images=[data.banner_image_url] if data.banner_image_url else None, - url=data.jump_link or None, + url=( + url.unicode_string() + if data.jump_link and (url := AnyUrl(data.jump_link)).scheme.startswith("http") + else None + ), timestamp=data.updated_at, compress=True, ) diff --git a/poetry.lock b/poetry.lock index 2c11d92c..566c625e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. [[package]] name = "aiodns" @@ -3503,7 +3503,6 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a08c6f0fe150303c1c6b71ebcd7213c2858041a7e01975da3a99aed1e7a378ef"}, {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, @@ -3837,7 +3836,7 @@ files = [ [package.dependencies] aiosqlite = {version = "*", optional = true, markers = "extra == \"aiosqlite\""} greenlet = {version = "!=0.4.17", optional = true, markers = "platform_machine == \"aarch64\" or platform_machine == \"ppc64le\" or platform_machine == \"x86_64\" or platform_machine == \"amd64\" or platform_machine == \"AMD64\" or platform_machine == \"win32\" or platform_machine == \"WIN32\" or extra == \"aiosqlite\""} -typing-extensions = {version = ">=4.6.0", optional = true, markers = "extra == \"aiosqlite\""} +typing-extensions = ">=4.6.0" [package.extras] aiomysql = ["aiomysql (>=0.2.0)", "greenlet (!=0.4.17)"] diff --git a/tests/platforms/test_arknights.py b/tests/platforms/test_arknights.py index 8afb4db2..230700b9 100644 --- a/tests/platforms/test_arknights.py +++ b/tests/platforms/test_arknights.py @@ -41,6 +41,73 @@ def monster_siren_list_1(): return get_json("monster-siren_list_1.json") +@respx.mock +async def test_url_parse(app: App): + from httpx import AsyncClient + + from nonebot_bison.utils import ProcessContext + from nonebot_bison.platform.arknights import Arknights, BulletinData, BulletinListItem, ArkBulletinResponse + + cid_router = respx.get("https://ak-webview.hypergryph.com/api/game/bulletin/1") + + def make_bulletin_obj(jump_link: str): + return BulletinData.model_validate({ + "cid": "1", + "displayType": 1, + "title": "title", + "category": 1, + "header": "header", + "content": "content", + "jumpLink": jump_link, + "bannerImageUrl": "https://www.baidu.com", + "displayTime": "2021-08-01", + "updatedAt": 1627795200, + }) + + def make_bulletin_list_item_obj(): + return BulletinListItem( + cid="1", + title="title", + category=1, + displayTime="2021-08-01", + updatedAt=1627795200, + sticky=False, + ) + + def make_response(b: BulletinData): + return Response(200, json=ArkBulletinResponse(code=0, msg="", data=b).model_dump(by_alias=True)) + + b1 = make_bulletin_obj("") + assert b1.jump_link == "" + + b2 = make_bulletin_obj("uniwebview://move?target=shop¶m1=SKINSHOP") + assert b2.jump_link == "uniwebview://move?target=shop¶m1=SKINSHOP" + + b3 = make_bulletin_obj("https://www.baidu.com") + assert b3.jump_link == "https://www.baidu.com" + + b4 = make_bulletin_obj("http://www.baidu.com") + assert b4.jump_link == "http://www.baidu.com" + + ark = Arknights(ProcessContext(), AsyncClient()) + + cid_router.mock(return_value=make_response(b1)) + p1 = await ark.parse(make_bulletin_list_item_obj()) + assert p1.url is None + + cid_router.mock(return_value=make_response(b2)) + p2 = await ark.parse(make_bulletin_list_item_obj()) + assert p2.url is None + + cid_router.mock(return_value=make_response(b3)) + p3 = await ark.parse(make_bulletin_list_item_obj()) + assert p3.url == "https://www.baidu.com/" + + cid_router.mock(return_value=make_response(b4)) + p4 = await ark.parse(make_bulletin_list_item_obj()) + assert p4.url == "http://www.baidu.com/" + + @pytest.mark.asyncio() async def test_get_date_in_bulletin(app: App): from nonebot_bison.utils import ProcessContext