mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
✨ 适配明日方舟新版公告栏API (#305)
* 🔨 适配明日方舟新版公告栏API * ✅ 更新测试 * 💄 auto fix by pre-commit hooks --------- Co-authored-by: GuGuMur <222153315@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import json
|
||||
from typing import Any
|
||||
from pathlib import Path
|
||||
|
||||
from httpx import AsyncClient
|
||||
from nonebot.plugin import require
|
||||
@@ -32,36 +32,45 @@ class Arknights(NewMessage):
|
||||
return "明日方舟游戏信息"
|
||||
|
||||
async def get_sub_list(self, _) -> list[RawPost]:
|
||||
raw_data = await self.client.get(
|
||||
"https://ak-conf.hypergryph.com/config/prod/announce_meta/IOS/announcement.meta.json"
|
||||
)
|
||||
return json.loads(raw_data.text)["announceList"]
|
||||
raw_data = await self.client.get("https://ak-webview.hypergryph.com/api/game/bulletinList?target=IOS")
|
||||
return raw_data.json()["data"]["list"]
|
||||
|
||||
def get_id(self, post: RawPost) -> Any:
|
||||
return post["announceId"]
|
||||
return post["cid"]
|
||||
|
||||
def get_date(self, _: RawPost) -> None:
|
||||
def get_date(self, _: RawPost) -> Any:
|
||||
return None
|
||||
|
||||
def get_category(self, _) -> Category:
|
||||
return Category(1)
|
||||
|
||||
async def parse(self, raw_post: RawPost) -> Post:
|
||||
announce_url = raw_post["webUrl"]
|
||||
text = ""
|
||||
raw_html = await self.client.get(announce_url)
|
||||
soup = bs(raw_html.text, "html.parser")
|
||||
pics = []
|
||||
if soup.find("div", class_="standerd-container"):
|
||||
# 图文
|
||||
require("nonebot_plugin_htmlrender")
|
||||
from nonebot_plugin_htmlrender import capture_element
|
||||
raw_data = await self.client.get(
|
||||
f"https://ak-webview.hypergryph.com/api/game/bulletin/{self.get_id(post=raw_post)}"
|
||||
)
|
||||
raw_data = raw_data.json()["data"]
|
||||
|
||||
pic_data = await capture_element(
|
||||
announce_url,
|
||||
"div.main",
|
||||
viewport={"width": 320, "height": 6400},
|
||||
device_scale_factor=3,
|
||||
announce_title = raw_data.get("header") if raw_data.get("header") != "" else raw_data.get("title")
|
||||
# text = "游戏公告更新:" + announce_title.replace('\n','')
|
||||
text = ""
|
||||
|
||||
pics = []
|
||||
if "content" in raw_data:
|
||||
require("nonebot_plugin_htmlrender")
|
||||
from nonebot_plugin_htmlrender import template_to_pic
|
||||
|
||||
template_path = str(Path(__file__).parent.parent / "post/templates/ark_announce")
|
||||
pic_data = await template_to_pic(
|
||||
template_path=template_path,
|
||||
template_name="index.html",
|
||||
templates={
|
||||
"announce_title": announce_title,
|
||||
"content": raw_data["content"],
|
||||
},
|
||||
pages={
|
||||
"viewport": {"width": 500, "height": 6400},
|
||||
"base_url": f"file://{template_path}",
|
||||
},
|
||||
)
|
||||
# render = Render()
|
||||
# viewport = {"width": 320, "height": 6400, "deviceScaleFactor": 3}
|
||||
@@ -72,8 +81,8 @@ class Arknights(NewMessage):
|
||||
pics.append(pic_data)
|
||||
else:
|
||||
text = "图片渲染失败"
|
||||
elif pic := soup.find("img", class_="banner-image"):
|
||||
pics.append(pic["src"]) # type: ignore
|
||||
elif "bannerImageUrl" in raw_data:
|
||||
pics.append(raw_post["bannerImageUrl"]) # type: ignore
|
||||
else:
|
||||
raise CategoryNotRecognize("未找到可渲染部分")
|
||||
return Post(
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1, minimum-scale=1"
|
||||
/>
|
||||
<link rel="icon" href="data:;base64,=" />
|
||||
<title>公告</title>
|
||||
<link rel="stylesheet" href="style.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="main">
|
||||
<div class="container">
|
||||
<div class="standerd-container">
|
||||
<div class="head-title-container">
|
||||
<span class="head-title">{{ announce_title }}</span>
|
||||
</div>
|
||||
<div class="content">{{ content }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
引用自 https://ak.hycdn.cn/announce/assets/css/announcement.v_0_1_2.css
|
||||
**/
|
||||
@media screen and (max-device-width: 480px) {
|
||||
body {
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
}
|
||||
html {
|
||||
height: 100%;
|
||||
}
|
||||
body,
|
||||
head {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
body {
|
||||
background-color: #313131;
|
||||
min-height: 100%;
|
||||
background-color: #d0d0cf;
|
||||
}
|
||||
.main {
|
||||
max-width: 980px;
|
||||
font-family: "Microsoft Yahei";
|
||||
width: 100%;
|
||||
margin: auto;
|
||||
font-size: 1rem;
|
||||
min-height: 100%;
|
||||
}
|
||||
.main .container {
|
||||
min-height: 100%;
|
||||
}
|
||||
.main .container .standerd-container {
|
||||
padding: 2.72727273%;
|
||||
width: 94.54545455%;
|
||||
margin: auto;
|
||||
}
|
||||
.main .container .standerd-container .banner-image-container {
|
||||
margin-bottom: 0.8rem;
|
||||
}
|
||||
.main .container .standerd-container .banner-image-container .banner-image {
|
||||
display: block;
|
||||
width: 100%;
|
||||
}
|
||||
.main .container .standerd-container .head-title-container {
|
||||
margin: 0;
|
||||
background-image: url(
|
||||
https://ak.hycdn.cn/announce/assets/images/announcement/header.jpg);
|
||||
background-size: cover;
|
||||
position: relative;
|
||||
margin-bottom: 0.6rem;
|
||||
}
|
||||
.main .container .standerd-container .head-title-container::before {
|
||||
content: "";
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding-top: 6.02564103%;
|
||||
}
|
||||
.main .container .standerd-container .head-title-container .head-title {
|
||||
padding-left: 0.25rem;
|
||||
color: #fff;
|
||||
font-weight: 500;
|
||||
overflow: hidden;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
box-sizing: border-box;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
flex-direction: column;
|
||||
font-size: 1rem;
|
||||
}
|
||||
.main .container .standerd-container .content {
|
||||
line-height: 0.8rem;
|
||||
font-size: 0.6rem;
|
||||
}
|
||||
.main .container .standerd-container .content h4 {
|
||||
font-size: 110%;
|
||||
margin-block-start: 0.5rem;
|
||||
margin-block-end: 0.5rem;
|
||||
}
|
||||
.main .container .standerd-container .content p {
|
||||
margin-block-start: 0.25rem;
|
||||
margin-block-end: 0.25rem;
|
||||
min-height: 0.8rem;
|
||||
}
|
||||
.main .container .standerd-container .content img {
|
||||
max-width: 100%;
|
||||
margin: auto;
|
||||
display: block;
|
||||
}
|
||||
.main .container .banner-image-container.cover {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
position: absolute;
|
||||
overflow: hidden;
|
||||
}
|
||||
.main .container .banner-image-container.cover .cover-jumper {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: block;
|
||||
}
|
||||
.main .container .banner-image-container.cover .banner-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
Reference in New Issue
Block a user