mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
update v0.1.5.0
This commit is contained in:
+6
-2
@@ -14,16 +14,20 @@ _browser: Optional[Browser] = None
|
||||
|
||||
|
||||
async def init(**kwargs) -> Optional[Browser]:
|
||||
global _browser
|
||||
if platform.system() == "Windows":
|
||||
return None
|
||||
try:
|
||||
global _browser
|
||||
browser = await async_playwright().start()
|
||||
_browser = await browser.chromium.launch(**kwargs)
|
||||
return _browser
|
||||
except NotImplementedError:
|
||||
logger.warning("win环境下 初始化playwright失败,相关功能将被限制....")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.warning(f"启动chromium发生错误 {type(e)}:{e}")
|
||||
if _browser:
|
||||
await _browser.close()
|
||||
return None
|
||||
|
||||
|
||||
async def get_browser(**kwargs) -> Browser:
|
||||
|
||||
+18
-16
@@ -1,17 +1,18 @@
|
||||
import asyncio
|
||||
from configs.path_config import IMAGE_PATH, FONT_PATH
|
||||
from PIL import Image, ImageFile, ImageDraw, ImageFont, ImageFilter
|
||||
from imagehash import ImageHash
|
||||
from io import BytesIO
|
||||
from matplotlib import pyplot as plt
|
||||
from typing import Tuple, Optional, Union, List, Literal
|
||||
from pathlib import Path
|
||||
from math import ceil
|
||||
import random
|
||||
import cv2
|
||||
import base64
|
||||
import imagehash
|
||||
import random
|
||||
import re
|
||||
from io import BytesIO
|
||||
from math import ceil
|
||||
from pathlib import Path
|
||||
from typing import List, Literal, Optional, Tuple, Union
|
||||
|
||||
import cv2
|
||||
import imagehash
|
||||
from configs.path_config import FONT_PATH, IMAGE_PATH
|
||||
from imagehash import ImageHash
|
||||
from matplotlib import pyplot as plt
|
||||
from PIL import Image, ImageDraw, ImageFile, ImageFilter, ImageFont
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
Image.MAX_IMAGE_PIXELS = None
|
||||
@@ -65,9 +66,7 @@ def compressed_image(
|
||||
"""
|
||||
in_file = IMAGE_PATH / in_file if isinstance(in_file, str) else in_file
|
||||
if out_file:
|
||||
out_file = (
|
||||
IMAGE_PATH / out_file if isinstance(out_file, str) else out_file
|
||||
)
|
||||
out_file = IMAGE_PATH / out_file if isinstance(out_file, str) else out_file
|
||||
else:
|
||||
out_file = in_file
|
||||
h, w, d = cv2.imread(str(in_file.absolute())).shape
|
||||
@@ -673,9 +672,11 @@ class BuildImage:
|
||||
ellipse_box = [0, 0, r2 - 2, r2 - 2]
|
||||
mask = Image.new(
|
||||
size=[int(dim * antialias) for dim in self.markImg.size],
|
||||
mode='L', color='black')
|
||||
mode="L",
|
||||
color="black",
|
||||
)
|
||||
draw = ImageDraw.Draw(mask)
|
||||
for offset, fill in (width / -2.0, 'black'), (width / 2.0, 'white'):
|
||||
for offset, fill in (width / -2.0, "black"), (width / 2.0, "white"):
|
||||
left, top = [(value + offset) * antialias for value in ellipse_box[:2]]
|
||||
right, bottom = [(value - offset) * antialias for value in ellipse_box[2:]]
|
||||
draw.ellipse([left, top, right, bottom], fill=fill)
|
||||
@@ -1490,6 +1491,7 @@ async def text2image(
|
||||
height = 0
|
||||
_tmp = BuildImage(0, 0, font_size=font_size)
|
||||
for x in text.split("\n"):
|
||||
x = x if x.strip() else "正"
|
||||
w, h = _tmp.getsize(x)
|
||||
height += h
|
||||
width = width if width > w else w
|
||||
|
||||
+46
-12
@@ -1,16 +1,16 @@
|
||||
from configs.path_config import IMAGE_PATH, RECORD_PATH
|
||||
from nonebot.adapters.onebot.v11.message import MessageSegment
|
||||
from configs.config import NICKNAME
|
||||
from services.log import logger
|
||||
from typing import Union, List
|
||||
from pathlib import Path
|
||||
import os
|
||||
from typing import List, Union
|
||||
|
||||
from configs.config import NICKNAME
|
||||
from configs.path_config import IMAGE_PATH, RECORD_PATH
|
||||
from nonebot.adapters.onebot.v11.message import MessageSegment, Message
|
||||
from services.log import logger
|
||||
|
||||
|
||||
def image(
|
||||
file: Union[str, Path, bytes] = None,
|
||||
path: str = None,
|
||||
b64: str = None,
|
||||
file: Union[str, Path, bytes] = None,
|
||||
path: str = None,
|
||||
b64: str = None,
|
||||
) -> Union[MessageSegment, str]:
|
||||
"""
|
||||
说明:
|
||||
@@ -63,7 +63,9 @@ def record(voice_name: str, path: str = None) -> MessageSegment or str:
|
||||
if len(voice_name.split(".")) == 1:
|
||||
voice_name += ".mp3"
|
||||
file = (
|
||||
Path(RECORD_PATH) / path / voice_name if path else Path(RECORD_PATH) / voice_name
|
||||
Path(RECORD_PATH) / path / voice_name
|
||||
if path
|
||||
else Path(RECORD_PATH) / voice_name
|
||||
)
|
||||
if "http" in voice_name:
|
||||
return MessageSegment.record(voice_name)
|
||||
@@ -96,7 +98,7 @@ def contact_user(qq: int) -> MessageSegment:
|
||||
|
||||
|
||||
def share(
|
||||
url: str, title: str, content: str = None, image_url: str = None
|
||||
url: str, title: str, content: str = None, image_url: str = None
|
||||
) -> MessageSegment:
|
||||
"""
|
||||
说明:
|
||||
@@ -155,7 +157,7 @@ def music(type_: str, id_: int) -> MessageSegment:
|
||||
|
||||
|
||||
def custom_forward_msg(
|
||||
msg_list: List[str], uin: Union[int, str], name: str = f"这里是{NICKNAME}"
|
||||
msg_list: List[str], uin: Union[int, str], name: str = f"这里是{NICKNAME}"
|
||||
) -> List[dict]:
|
||||
"""
|
||||
生成自定义合并消息
|
||||
@@ -176,3 +178,35 @@ def custom_forward_msg(
|
||||
}
|
||||
mes_list.append(data)
|
||||
return mes_list
|
||||
|
||||
|
||||
class MessageBuilder:
|
||||
"""
|
||||
MessageSegment构建工具
|
||||
"""
|
||||
|
||||
def __init__(self, msg: Union[str, MessageSegment, Message]):
|
||||
if msg:
|
||||
if isinstance(msg, str):
|
||||
self._msg = text(msg)
|
||||
else:
|
||||
self._msg = msg
|
||||
else:
|
||||
self._msg = text("")
|
||||
|
||||
def text(self, msg: str):
|
||||
return MessageBuilder(self._msg + text(msg))
|
||||
|
||||
def image(
|
||||
self,
|
||||
file: Union[str, Path, bytes] = None,
|
||||
path: str = None,
|
||||
b64: str = None,
|
||||
):
|
||||
return MessageBuilder(self._msg + image(file, path, b64))
|
||||
|
||||
def at(self, qq: int):
|
||||
return MessageBuilder(self._msg + at(qq))
|
||||
|
||||
def face(self, id_: int):
|
||||
return MessageBuilder(self._msg + face(id_))
|
||||
|
||||
Reference in New Issue
Block a user