Azide e2a97a9e56
适配小刻食堂平台 (#379)
* 🐛 插入新的Schedulable时应传入use_batch参数

*  适配ceobecanteen平台

Co-authored-by: phidiaLam <2957035701@qq.com>

*   明日方舟公告与官网采用截图分享 (#480)

*  明日方舟公告与官网采用截图分享

* 💄 auto fix by pre-commit hooks

* 🐛 修复缺少的导入,优化逻辑

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Azide <rukuy@qq.com>

* 🐛 优化截图图片效果

* 🐛 修复错误将转发内图片视作头图的问题

* 🍱 使用正式 Bison Logo

* 💄 auto fix by pre-commit hooks

* 🐛 请求小刻API时不在headers里添加过多字段

* 🐛 get_comb_id方法删除无用的targets参数

* 💡 get_comb_id方法更新注释

* 🔥 移除发送部分的更改

*  在命名中明确表示cond_func意图

* ♻️ 拆分get_comb_id功能

* ♻️ 调整缓存逻辑

*  使用uri在theme中调用platform截图

* ♻️ 重构截图逻辑

*  添加模糊匹配提示

*  适配新版Site

* 💄 auto fix by pre-commit hooks

* 🐛 去掉不必要的排序

* 🐛 修正不应出现的驼峰变量名

* ♻️ 按review意见修改

* ♻️ 调整截图函数逻辑

* 🔊 调低日志等级

* ✏️ 修复一些拼写和格式

---------

Co-authored-by: phidiaLam <2957035701@qq.com>
Co-authored-by: 洛梧藤 <67498817+phidiaLam@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-07-13 01:06:42 +08:00

82 lines
2.4 KiB
Python

from io import BytesIO
from pathlib import Path
from dataclasses import dataclass
from typing import TYPE_CHECKING, Literal
from nonebot_plugin_saa import Text, Image, MessageSegmentFactory
from nonebot_bison.utils import text_fletten
from nonebot_bison.theme.utils import web_embed_image
from nonebot_bison.theme import Theme, ThemeRenderError, ThemeRenderUnsupportError
if TYPE_CHECKING:
from nonebot_bison.post import Post
@dataclass
class ArkData:
announce_title: str
content: str
banner_image_url: str | Path | None
class ArknightsTheme(Theme):
"""Arknights 公告风格主题
需要安装`nonebot_plugin_htmlrender`插件
"""
name: Literal["arknights"] = "arknights"
need_browser: bool = True
template_path: Path = Path(__file__).parent / "templates"
template_name: str = "announce.html.jinja"
async def render(self, post: "Post"):
from nonebot_plugin_htmlrender import template_to_pic
if not post.title:
raise ThemeRenderUnsupportError("标题为空")
banner = post.images[0] if post.images else None
match banner:
case bytes() | BytesIO():
banner = web_embed_image(banner)
case str() | Path() | None:
pass
case _:
raise ThemeRenderUnsupportError(
f"图片类型错误, 期望 str | Path | bytes | BytesIO | None, 实际为 {type(banner)}"
)
ark_data = ArkData(
announce_title=text_fletten(post.title),
content=post.content,
banner_image_url=banner,
)
try:
announce_pic = await template_to_pic(
template_path=self.template_path.as_posix(),
template_name=self.template_name,
templates={
"data": ark_data,
},
pages={
"viewport": {"width": 600, "height": 100},
"base_url": self.template_path.as_uri(),
},
)
except Exception as e:
raise ThemeRenderError(f"渲染文本失败: {e}")
msgs: list[MessageSegmentFactory] = []
msgs.append(Image(announce_pic))
if post.url:
msgs.append(Text(f"前往:{post.url}"))
if post.images:
msgs.extend(Image(img) for img in post.images[1:])
return msgs