diff --git a/nonebot_bison/platform/arknights.py b/nonebot_bison/platform/arknights.py index efba4e1..e931d69 100644 --- a/nonebot_bison/platform/arknights.py +++ b/nonebot_bison/platform/arknights.py @@ -1,9 +1,10 @@ from typing import Any from functools import partial +from yarl import URL from httpx import AsyncClient from bs4 import BeautifulSoup as bs -from pydantic import Field, AnyUrl, BaseModel +from pydantic import Field, BaseModel from nonebot.compat import type_validate_python from ..post import Post @@ -113,11 +114,7 @@ class Arknights(NewMessage): title=title, nickname="明日方舟游戏内公告", images=[data.banner_image_url] if data.banner_image_url else None, - url=( - url.unicode_string() - if data.jump_link and (url := AnyUrl(data.jump_link)).scheme.startswith("http") - else None - ), + url=(url.human_repr() if (url := URL(data.jump_link)).scheme.startswith("http") else None), timestamp=data.updated_at, compress=True, ) diff --git a/pyproject.toml b/pyproject.toml index a455f49..26d85a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -139,4 +139,4 @@ defineConstant = { PYDANTIC_V2 = true } [[tool.poetry.source]] name = "offical-source" url = "https://pypi.org/simple/" -priority = "primary" \ No newline at end of file +priority = "primary" diff --git a/tests/platforms/test_arknights.py b/tests/platforms/test_arknights.py index 230700b..12146f2 100644 --- a/tests/platforms/test_arknights.py +++ b/tests/platforms/test_arknights.py @@ -4,6 +4,7 @@ import respx import pytest from nonebug.app import App from httpx import Response, AsyncClient +from nonebot.compat import model_dump, type_validate_python from .utils import get_file, get_json @@ -51,18 +52,21 @@ async def test_url_parse(app: App): 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, - }) + return type_validate_python( + BulletinData, + { + "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( @@ -75,7 +79,7 @@ async def test_url_parse(app: App): ) def make_response(b: BulletinData): - return Response(200, json=ArkBulletinResponse(code=0, msg="", data=b).model_dump(by_alias=True)) + return Response(200, json=model_dump(ArkBulletinResponse(code=0, msg="", data=b), by_alias=True)) b1 = make_bulletin_obj("") assert b1.jump_link == ""