mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2024-11-26 10:35:08 +08:00
🐛 [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,
|
||||||
@ -41,9 +40,7 @@ async def template2html(
|
|||||||
async def template2image(
|
async def template2image(
|
||||||
template: str,
|
template: str,
|
||||||
templates: dict,
|
templates: dict,
|
||||||
###
|
|
||||||
pages=None,
|
pages=None,
|
||||||
###
|
|
||||||
wait: int = 0,
|
wait: int = 0,
|
||||||
scale_factor: float = 1,
|
scale_factor: float = 1,
|
||||||
debug: bool = False,
|
debug: bool = False,
|
||||||
@ -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)
|
||||||
@ -88,14 +83,6 @@ async def template2image(
|
|||||||
) 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