forked from bot/app
🐛 [utils]: htmlrender load error
This commit is contained in:
parent
6c1fc62ef1
commit
967f1a0e5b
@ -1,20 +1,19 @@
|
|||||||
import os.path
|
import os.path
|
||||||
# import time
|
|
||||||
from os import getcwd
|
from os import getcwd
|
||||||
# from typing import Literal
|
|
||||||
|
|
||||||
import aiofiles
|
import aiofiles # type: ignore
|
||||||
import nonebot
|
import nonebot
|
||||||
"""
|
from nonebot import require
|
||||||
from src.utils.htmlrender import (
|
|
||||||
|
require("nonebot_plugin_htmlrender")
|
||||||
|
|
||||||
|
from nonebot_plugin_htmlrender import (
|
||||||
template_to_html,
|
template_to_html,
|
||||||
template_to_pic,
|
template_to_pic,
|
||||||
# get_new_page,
|
|
||||||
)
|
)
|
||||||
"""
|
|
||||||
from nonebot_plugin_htmlrender import *
|
|
||||||
from .tools import random_hex_string
|
from .tools import random_hex_string
|
||||||
|
|
||||||
|
|
||||||
async def html2image(
|
async def html2image(
|
||||||
html: str,
|
html: str,
|
||||||
wait: int = 0,
|
wait: int = 0,
|
||||||
@ -39,14 +38,12 @@ async def template2html(
|
|||||||
|
|
||||||
|
|
||||||
async def template2image(
|
async def template2image(
|
||||||
template: str,
|
template: str,
|
||||||
templates: dict,
|
templates: dict,
|
||||||
###
|
pages=None,
|
||||||
pages=None,
|
wait: int = 0,
|
||||||
###
|
scale_factor: float = 1,
|
||||||
wait: int = 0,
|
debug: bool = False,
|
||||||
scale_factor: float = 1,
|
|
||||||
debug: bool = False,
|
|
||||||
) -> bytes:
|
) -> bytes:
|
||||||
"""
|
"""
|
||||||
template -> html -> image
|
template -> html -> image
|
||||||
@ -66,11 +63,9 @@ async def template2image(
|
|||||||
pages = {
|
pages = {
|
||||||
"viewport": {
|
"viewport": {
|
||||||
"width" : 1080,
|
"width" : 1080,
|
||||||
"height": 10
|
|
||||||
},
|
},
|
||||||
"base_url": f"file://{getcwd()}",
|
"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)
|
||||||
@ -84,18 +79,10 @@ async def template2image(
|
|||||||
)
|
)
|
||||||
random_file_name = f"debug-{random_hex_string(6)}.html"
|
random_file_name = f"debug-{random_hex_string(6)}.html"
|
||||||
async with aiofiles.open(
|
async with aiofiles.open(
|
||||||
os.path.join(template_path, random_file_name), "w", encoding="utf-8"
|
os.path.join(template_path, random_file_name), "w", encoding="utf-8"
|
||||||
) as f:
|
) as f:
|
||||||
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,
|
||||||
@ -107,32 +94,3 @@ async def template2image(
|
|||||||
device_scale_factor=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
|
|
||||||
)
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user