Merge pull request #71 from ElapsingDreams/main

暂时使用nonebot_plugin_htmlrender以解决issue#68
This commit is contained in:
远野千束 2024-09-01 08:14:05 +08:00 committed by GitHub
commit 5253d0e515
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,17 +1,26 @@
import os import os.path
# import time # import time
from os import getcwd
# from typing import Literal # from typing import Literal
import aiofiles import aiofiles
import nonebot import nonebot
"""
from src.utils.htmlrender import ( from src.utils.htmlrender import (
template_to_html, template_to_html,
template_to_pic, template_to_pic,
# get_new_page, # get_new_page,
) )
"""
from nonebot_plugin_htmlrender import *
from .tools import random_hex_string from .tools import random_hex_string
async def html2image(
html: str,
wait: int = 0,
):
pass
async def template2html( async def template2html(
template: str, template: str,
@ -32,7 +41,10 @@ async def template2html(
async def template2image( async def template2image(
template: str, template: str,
templates: dict, templates: dict,
wait: int = 1, ###
pages=None,
###
wait: int = 0,
scale_factor: float = 1, scale_factor: float = 1,
debug: bool = False, debug: bool = False,
) -> bytes: ) -> bytes:
@ -48,6 +60,18 @@ async def template2image(
Returns: Returns:
图片二进制数据 图片二进制数据
""" """
###
if pages is None:
pages = {
"viewport": {
"width" : 1080,
"height": 10
},
"base_url": f"file://{getcwd()}",
}
###
template_path = os.path.dirname(template) template_path = os.path.dirname(template)
template_name = os.path.basename(template) template_name = os.path.basename(template)
@ -65,14 +89,50 @@ async def template2image(
await f.write(raw_html) await f.write(raw_html)
nonebot.logger.info("Debug HTML: %s" % f"{random_file_name}") nonebot.logger.info("Debug HTML: %s" % f"{random_file_name}")
"""
viewport={
"width": 1080,
"height": 10,
"deviceScaleFactor": scale_factor,
},"""
return await template_to_pic( return await template_to_pic(
template_name=template_name, template_name=template_name,
template_path=template_path, template_path=template_path,
templates=templates, templates=templates,
wait=wait, wait=wait,
viewport={
"width": 1080, ###
"height": 10, pages=pages,
"deviceScaleFactor": scale_factor, device_scale_factor=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
)