mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2025-09-08 13:16:23 +00:00
✔❤大更新!
This commit is contained in:
@ -1,23 +1,143 @@
|
||||
import os.path
|
||||
import time
|
||||
|
||||
# import time
|
||||
from os import getcwd
|
||||
|
||||
import aiofiles
|
||||
import nonebot
|
||||
|
||||
from nonebot_plugin_htmlrender import *
|
||||
from .tools import random_hex_string
|
||||
|
||||
# import imgkit
|
||||
# from typing import Any, Dict, Literal, Optional, Union
|
||||
# import uuid
|
||||
|
||||
# import jinja2
|
||||
# from pathlib import Path
|
||||
|
||||
# TEMPLATES_PATH = str(Path(__file__).parent / "templates")
|
||||
# env = jinja2.Environment( # noqa: S701
|
||||
# extensions=["jinja2.ext.loopcontrols"],
|
||||
# loader=jinja2.FileSystemLoader(TEMPLATES_PATH),
|
||||
# enable_async=True,
|
||||
# )
|
||||
|
||||
|
||||
# async def template_to_html(
|
||||
# template_path: str,
|
||||
# template_name: str,
|
||||
# **kwargs,
|
||||
# ) -> str:
|
||||
# """使用jinja2模板引擎通过html生成图片
|
||||
|
||||
# Args:
|
||||
# template_path (str): 模板路径
|
||||
# template_name (str): 模板名
|
||||
# **kwargs: 模板内容
|
||||
# Returns:
|
||||
# str: html
|
||||
# """
|
||||
|
||||
# template_env = jinja2.Environment( # noqa: S701
|
||||
# loader=jinja2.FileSystemLoader(template_path),
|
||||
# enable_async=True,
|
||||
# )
|
||||
# template = template_env.get_template(template_name)
|
||||
|
||||
# return await template.render_async(**kwargs)
|
||||
|
||||
|
||||
# async def template_to_pic(
|
||||
# template_path: str,
|
||||
# template_name: str,
|
||||
# templates: Dict[Any, Any],
|
||||
# pages: Optional[Dict[Any, Any]] = None,
|
||||
# wait: int = 0,
|
||||
# type: Literal["jpeg", "png"] = "png", # noqa: A002
|
||||
# quality: Union[int, None] = None,
|
||||
# device_scale_factor: float = 2,
|
||||
# ) -> bytes:
|
||||
# """使用jinja2模板引擎通过html生成图片
|
||||
|
||||
# Args:
|
||||
# template_path (str): 模板路径
|
||||
# template_name (str): 模板名
|
||||
# templates (Dict[Any, Any]): 模板内参数 如: {"name": "abc"}
|
||||
# pages (Optional[Dict[Any, Any]]): 网页参数 Defaults to
|
||||
# {"base_url": f"file://{getcwd()}", "viewport": {"width": 500, "height": 10}}
|
||||
# wait (int, optional): 网页载入等待时间. Defaults to 0.
|
||||
# type (Literal["jpeg", "png"]): 图片类型, 默认 png
|
||||
# quality (int, optional): 图片质量 0-100 当为`png`时无效
|
||||
# device_scale_factor: 缩放比例,类型为float,值越大越清晰(真正想让图片清晰更优先请调整此选项)
|
||||
# Returns:
|
||||
# bytes: 图片 可直接发送
|
||||
# """
|
||||
# if pages is None:
|
||||
# pages = {
|
||||
# "viewport": {"width": 500, "height": 10},
|
||||
# "base_url": f"file://{getcwd()}", # noqa: PTH109
|
||||
# }
|
||||
|
||||
# template_env = jinja2.Environment( # noqa: S701
|
||||
# loader=jinja2.FileSystemLoader(template_path),
|
||||
# enable_async=True,
|
||||
# )
|
||||
# template = template_env.get_template(template_name)
|
||||
|
||||
# open(
|
||||
# filename := os.path.join(
|
||||
# template_path,
|
||||
# str(uuid.uuid4())+".html",
|
||||
# ),
|
||||
# "w",
|
||||
# ).write(await template.render_async(**templates))
|
||||
|
||||
# print(pages,filename)
|
||||
|
||||
|
||||
# img = imgkit.from_file(
|
||||
# filename,
|
||||
# output_path=False,
|
||||
# options={
|
||||
# "format": type,
|
||||
# "quality": quality if (quality and type == "jpeg") else 94,
|
||||
# "allow": pages["base_url"],
|
||||
# # "viewport-size": "{} {}".format(pages["viewport"]["width"],pages["viewport"]["height"]),
|
||||
# "zoom": device_scale_factor,
|
||||
# # "load-error-handling": "ignore",
|
||||
# "enable-local-file-access": None,
|
||||
# "no-stop-slow-scripts": None,
|
||||
# "transparent": None,
|
||||
# },
|
||||
# ) # type: ignore
|
||||
|
||||
|
||||
# # os.remove(filename)
|
||||
|
||||
# return img
|
||||
|
||||
# return await html_to_pic(
|
||||
# template_path=f"file://{template_path}",
|
||||
# html=await template.render_async(**templates),
|
||||
# wait=wait,
|
||||
# type=type,
|
||||
# quality=quality,
|
||||
# device_scale_factor=device_scale_factor,
|
||||
# **pages,
|
||||
# )
|
||||
|
||||
|
||||
async def html2image(
|
||||
html: str,
|
||||
wait: int = 0,
|
||||
html: str,
|
||||
wait: int = 0,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
async def template2html(
|
||||
template: str,
|
||||
templates: dict,
|
||||
template: str,
|
||||
templates: dict,
|
||||
) -> str:
|
||||
"""
|
||||
Args:
|
||||
@ -32,12 +152,12 @@ 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,
|
||||
pages=None,
|
||||
wait: int = 0,
|
||||
scale_factor: float = 1,
|
||||
debug: bool = False,
|
||||
) -> bytes:
|
||||
"""
|
||||
template -> html -> image
|
||||
@ -53,11 +173,8 @@ async def template2image(
|
||||
"""
|
||||
if pages is None:
|
||||
pages = {
|
||||
"viewport": {
|
||||
"width" : 1080,
|
||||
"height": 10
|
||||
},
|
||||
"base_url": f"file://{getcwd()}",
|
||||
"viewport": {"width": 1080, "height": 10},
|
||||
"base_url": f"file://{getcwd()}",
|
||||
}
|
||||
template_path = os.path.dirname(template)
|
||||
template_name = os.path.basename(template)
|
||||
@ -70,7 +187,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}")
|
||||
|
||||
@ -84,30 +203,30 @@ async def template2image(
|
||||
)
|
||||
|
||||
|
||||
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
|
||||
)
|
||||
# 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
|
||||
# )
|
||||
|
@ -75,8 +75,8 @@ class MarkdownMessage:
|
||||
group_id = event.group_id if message_type == "group" else None
|
||||
user_id = event.user.id if isinstance(event, satori.event.Event) else event.user_id
|
||||
session_id = user_id if message_type == "private" else group_id
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
try:
|
||||
raise TencentBannedMarkdownError("Tencent banned markdown")
|
||||
forward_id = await bot.call_api(
|
||||
|
Reference in New Issue
Block a user