🧐内置页面渲染组件,弃用nonebot-plugin-htmlrender,避免playwright对多平台的不友好绑架

This commit is contained in:
2024-08-23 17:58:36 +08:00
parent a9c6ea0452
commit b6c8fcbccd
32 changed files with 1956 additions and 169 deletions

View File

@ -1,23 +1,21 @@
import os.path
import time
from os import getcwd
import os
# import time
# from typing import Literal
import aiofiles
import nonebot
from nonebot_plugin_htmlrender import *
from src.utils.htmlrender import (
template_to_html,
template_to_pic,
# get_new_page,
)
from .tools import random_hex_string
async def html2image(
html: str,
wait: int = 0,
):
pass
async def template2html(
template: str,
templates: dict,
template: str,
templates: dict,
) -> str:
"""
Args:
@ -32,12 +30,11 @@ async def template2html(
async def template2image(
template: str,
templates: dict,
pages=None,
wait: int = 0,
scale_factor: float = 1,
debug: bool = False,
template: str,
templates: dict,
wait: int = 1,
scale_factor: float = 1,
debug: bool = False,
) -> bytes:
"""
template -> html -> image
@ -51,14 +48,6 @@ async def template2image(
Returns:
图片二进制数据
"""
if pages is None:
pages = {
"viewport": {
"width" : 1080,
"height": 10
},
"base_url": f"file://{getcwd()}",
}
template_path = os.path.dirname(template)
template_name = os.path.basename(template)
@ -70,7 +59,9 @@ async def template2image(
**templates,
)
random_file_name = f"debug-{random_hex_string(6)}.html"
async with aiofiles.open(os.path.join(template_path, random_file_name), "w", encoding="utf-8") as f:
async with aiofiles.open(
os.path.join(template_path, random_file_name), "w", encoding="utf-8"
) as f:
await f.write(raw_html)
nonebot.logger.info("Debug HTML: %s" % f"{random_file_name}")
@ -78,36 +69,10 @@ async def template2image(
template_name=template_name,
template_path=template_path,
templates=templates,
pages=pages,
wait=wait,
device_scale_factor=scale_factor,
viewport={
"width": 1080,
"height": 10,
"deviceScaleFactor": scale_factor,
},
)
async def url2image(
url: str,
wait: int = 0,
scale_factor: float = 1,
type: str = "png",
quality: int = 100,
**kwargs
) -> bytes:
"""
Args:
quality:
type:
url: str: URL
wait: int: 等待时间
scale_factor: float: 缩放因子
**kwargs: page 参数
Returns:
图片二进制数据
"""
async with get_new_page(scale_factor) as page:
await page.goto(url)
await page.wait_for_timeout(wait)
return await page.screenshot(
full_page=True,
type=type,
quality=quality
)

View File

@ -11,11 +11,11 @@ from nonebot import require
from nonebot.adapters import satori
from nonebot.adapters.onebot import v11
from src.utils.htmlrender import md_to_pic
from .. import load_from_yaml
from ..base.ly_typing import T_Bot, T_Message, T_MessageEvent
require("nonebot_plugin_htmlrender")
from nonebot_plugin_htmlrender import md_to_pic
config = load_from_yaml("config.yml")