Skip to content

Commit

Permalink
✨ 过滤不可访问的url
Browse files Browse the repository at this point in the history
  • Loading branch information
AzideCupric committed Mar 7, 2024
1 parent 7635e3f commit 2956dd9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
8 changes: 6 additions & 2 deletions nonebot_bison/platform/arknights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down
5 changes: 2 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions tests/platforms/test_arknights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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&param1=SKINSHOP")
assert b2.jump_link == "uniwebview://move?target=shop&param1=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
Expand Down

0 comments on commit 2956dd9

Please sign in to comment.