mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
✨ 适配小刻食堂平台 (#379)
* 🐛 插入新的Schedulable时应传入use_batch参数 * ✨ 适配ceobecanteen平台 Co-authored-by: phidiaLam <2957035701@qq.com> * ✨ ✨ 明日方舟公告与官网采用截图分享 (#480) * ✨ 明日方舟公告与官网采用截图分享 * 💄 auto fix by pre-commit hooks * 🐛 修复缺少的导入,优化逻辑 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Azide <rukuy@qq.com> * 🐛 优化截图图片效果 * 🐛 修复错误将转发内图片视作头图的问题 * 🍱 使用正式 Bison Logo * 💄 auto fix by pre-commit hooks * 🐛 请求小刻API时不在headers里添加过多字段 * 🐛 get_comb_id方法删除无用的targets参数 * 💡 get_comb_id方法更新注释 * 🔥 移除发送部分的更改 * ✨ 在命名中明确表示cond_func意图 * ♻️ 拆分get_comb_id功能 * ♻️ 调整缓存逻辑 * ✨ 使用uri在theme中调用platform截图 * ♻️ 重构截图逻辑 * ✨ 添加模糊匹配提示 * ✨ 适配新版Site * 💄 auto fix by pre-commit hooks * 🐛 去掉不必要的排序 * 🐛 修正不应出现的驼峰变量名 * ♻️ 按review意见修改 * ♻️ 调整截图函数逻辑 * 🔊 调低日志等级 * ✏️ 修复一些拼写和格式 --------- Co-authored-by: phidiaLam <2957035701@qq.com> Co-authored-by: 洛梧藤 <67498817+phidiaLam@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,7 @@ from .site import Site as Site
|
||||
from ..plugin_config import plugin_config
|
||||
from .image import pic_merge as pic_merge
|
||||
from .http import http_client as http_client
|
||||
from .image import capture_html as capture_html
|
||||
from .site import ClientManager as ClientManager
|
||||
from .image import text_to_image as text_to_image
|
||||
from .site import anonymous_site as anonymous_site
|
||||
@@ -108,3 +109,8 @@ def decode_unicode_escapes(s: str):
|
||||
|
||||
regex = re.compile(r"\\[rnt]|\\u[0-9a-fA-F]{4}")
|
||||
return regex.sub(decode_match, s)
|
||||
|
||||
|
||||
def text_fletten(text: str, *, banned: str = "\n\r\t", replace: str = " ") -> str:
|
||||
"""将文本中的格式化字符去除"""
|
||||
return "".join(c if c not in banned else replace for c in text)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from io import BytesIO
|
||||
from typing import TypeGuard
|
||||
from functools import partial
|
||||
from typing import Literal, TypeGuard
|
||||
|
||||
from yarl import URL
|
||||
from PIL import Image
|
||||
from httpx import AsyncClient
|
||||
from nonebot import logger, require
|
||||
@@ -96,7 +97,11 @@ async def pic_merge(pics: list[str | bytes], http_client: AsyncClient) -> list[s
|
||||
|
||||
|
||||
def is_pics_mergable(imgs: list) -> TypeGuard[list[str | bytes]]:
|
||||
return all(isinstance(img, str | bytes) for img in imgs)
|
||||
if any(not isinstance(img, str | bytes) for img in imgs):
|
||||
return False
|
||||
|
||||
url = [URL(img) for img in imgs if isinstance(img, str)]
|
||||
return all(u.scheme in ("http", "https") for u in url)
|
||||
|
||||
|
||||
async def text_to_image(saa_text: SaaText) -> SaaImage:
|
||||
@@ -108,3 +113,32 @@ async def text_to_image(saa_text: SaaText) -> SaaImage:
|
||||
|
||||
render_data = await text_to_pic(str(saa_text))
|
||||
return SaaImage(render_data)
|
||||
|
||||
|
||||
async def capture_html(
|
||||
url: str,
|
||||
selector: str,
|
||||
timeout: float = 0,
|
||||
type: Literal["jpeg", "png"] = "png",
|
||||
quality: int | None = None,
|
||||
wait_until: Literal["commit", "domcontentloaded", "load", "networkidle"] | None = None,
|
||||
viewport: dict = {"width": 1024, "height": 990},
|
||||
device_scale_factor: int = 2,
|
||||
**page_kwargs,
|
||||
) -> bytes | None:
|
||||
"""
|
||||
将给定的url网页的指定CSS选择器部分渲染成图片
|
||||
|
||||
timeout: 超时时间,单位毫秒
|
||||
"""
|
||||
require("nonebot_plugin_htmlrender")
|
||||
from nonebot_plugin_htmlrender import get_new_page
|
||||
|
||||
assert url
|
||||
async with get_new_page(device_scale_factor=device_scale_factor, viewport=viewport, **page_kwargs) as page:
|
||||
await page.goto(url, timeout=timeout, wait_until=wait_until)
|
||||
pic_data = await page.locator(selector).screenshot(
|
||||
type=type,
|
||||
quality=quality,
|
||||
)
|
||||
return pic_data
|
||||
|
||||
Reference in New Issue
Block a user