update code

This commit is contained in:
HibiKier
2021-09-05 02:21:38 +08:00
parent 21a5feda43
commit 7afb099c12
84 changed files with 3377 additions and 1404 deletions
+11 -8
View File
@@ -4,6 +4,7 @@ from playwright.async_api import Browser, async_playwright
import nonebot
from nonebot import Driver
from services.log import logger
import platform
driver: Driver = nonebot.get_driver()
@@ -13,14 +14,16 @@ _browser: Optional[Browser] = None
async def init(**kwargs) -> Optional[Browser]:
# try:
global _browser
browser = await async_playwright().start()
_browser = await browser.chromium.launch(**kwargs)
return _browser
# except NotImplementedError:
# logger.warning("win环境下 初始化playwright失败....请替换环境至linux")
# return None
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失败....请替换环境至linux")
return None
async def get_browser(**kwargs) -> Browser:
+115 -23
View File
@@ -45,7 +45,9 @@ def get_img_hash(image_file: str) -> ImageHash:
return hash_value
def compressed_image(in_file: Union[str, Path], out_file: Union[str, Path] = None, ratio: float = 0.9):
def compressed_image(
in_file: Union[str, Path], out_file: Union[str, Path] = None, ratio: float = 0.9
):
"""
说明:
压缩图片
@@ -56,15 +58,19 @@ def compressed_image(in_file: Union[str, Path], out_file: Union[str, Path] = Non
"""
in_file = Path(IMAGE_PATH) / in_file if isinstance(in_file, str) else in_file
if out_file:
out_file = Path(IMAGE_PATH) / out_file if isinstance(out_file, str) else out_file
out_file = (
Path(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
img = cv2.resize(cv2.imread(str(in_file.absolute())), (int(w * ratio), int(h * ratio)))
img = cv2.resize(
cv2.imread(str(in_file.absolute())), (int(w * ratio), int(h * ratio))
)
cv2.imwrite(str(out_file.absolute()), img)
def alpha2white_PIL(pic: Image) -> Image:
def alpha2white_pil(pic: Image) -> Image:
"""
说明:
将图片透明背景转化为白色
@@ -131,16 +137,19 @@ class CreateImg:
def __init__(
self,
w,
h,
paste_image_width=0,
paste_image_height=0,
color="white",
image_type="RGBA",
font_size=10,
background="",
ttf="yz.ttf",
ratio=1,
w: int,
h: int,
paste_image_width: int = 0,
paste_image_height: int = 0,
color: Union[str, Tuple[int, int, int], Tuple[int, int, int, int]] = "white",
image_mode: str = "RGBA",
font_size: int = 10,
background: Union[Optional[str], BytesIO] = None,
ttf: str = "yz.ttf",
ratio: float = 1,
is_alpha: bool = False,
plain_text: Optional[str] = None,
font_color: Optional[Tuple[int, int, int]] = None,
):
"""
参数:
@@ -149,11 +158,13 @@ class CreateImg:
:param paste_image_width: 当图片做为背景图时,设置贴图的宽度,用于贴图自动换行
:param paste_image_height: 当图片做为背景图时,设置贴图的高度,用于贴图自动换行
:param color: 生成图片的颜色
:param image_type: 图片的类型
:param image_mode: 图片的类型
:param font_size: 文字大小
:param background: 打开图片的路径
:param ttf: 字体,默认在 resource/ttf/ 路径下
:param ratio: 倍率压缩
:param is_alpha: 是否背景透明
:param plain_text: 纯文字文本
"""
self.w = int(w)
self.h = int(h)
@@ -161,9 +172,14 @@ class CreateImg:
self.paste_image_height = int(paste_image_height)
self.current_w = 0
self.current_h = 0
self.ttfont = ImageFont.truetype(TTF_PATH + ttf, int(font_size))
self.font = ImageFont.truetype(TTF_PATH + ttf, int(font_size))
if not background:
self.markImg = Image.new(image_type, (self.w, self.h), color)
if plain_text:
ttf_w, ttf_h = self.getsize(plain_text)
self.w = self.w if self.w > ttf_w else ttf_w
self.h = self.h if self.h > ttf_h else ttf_h
self.markImg = Image.new(image_mode, (self.w, self.h), color)
self.markImg.convert(image_mode)
else:
if not w and not h:
self.markImg = Image.open(background)
@@ -181,14 +197,26 @@ class CreateImg:
self.markImg = Image.open(background).resize(
(self.w, self.h), Image.ANTIALIAS
)
if is_alpha:
array = self.markImg.load()
for i in range(w):
for j in range(h):
pos = array[i, j]
is_edit = (sum([1 for x in pos[0:3] if x > 240]) == 3)
if is_edit:
array[i, j] = (255, 255, 255, 0)
self.draw = ImageDraw.Draw(self.markImg)
self.size = self.w, self.h
if plain_text:
fill = font_color if font_color else (0, 0, 0)
self.text((0, 0), plain_text, fill)
def paste(
self,
img: "CreateImg" or Image,
pos: Tuple[int, int] = None,
alpha: bool = False,
center_type: Optional[str] = None,
):
"""
说明:
@@ -197,7 +225,26 @@ class CreateImg:
:param img: 已打开的图片文件,可以为 CreateImg 或 Image
:param pos: 贴图位置(左上角)
:param alpha: 图片背景是否为透明
:param center_type: 居中类型,可能的值 center: 完全居中,by_width: 水平居中,by_height: 垂直居中
"""
if center_type:
if center_type not in ["center", "by_height", "by_width"]:
raise ValueError(
"center_type must be 'center', 'by_width' or 'by_height'"
)
width, height = 0, 0
if not pos:
pos = (0, 0)
if center_type == "center":
width = int((self.w - img.w) / 2)
height = int((self.h - img.h) / 2)
elif center_type == "by_width":
width = int((self.w - img.w) / 2)
height = pos[1]
elif center_type == "by_height":
width = pos[0]
height = int((self.h - img.h) / 2)
pos = (width, height)
if isinstance(img, CreateImg):
img = img.markImg
if self.current_w == self.w:
@@ -222,10 +269,38 @@ class CreateImg:
参数:
:param msg: 文字内容
"""
return self.ttfont.getsize(msg)
return self.font.getsize(msg)
def point(self, pos: Tuple[int, int], fill: Optional[Tuple[int, int, int]] = None):
"""
说明:
绘制多个或单独的像素
参数:
:param pos: 坐标
:param fill: 填错颜色
"""
self.draw.point(pos, fill=fill)
def ellipse(
self,
pos: Tuple[int, int, int, int],
fill: Optional[Tuple[int, int, int]] = None,
outline: Optional[Tuple[int, int, int]] = None,
width: int = 1,
):
"""
说明:
绘制圆
参数:
:param pos: 坐标范围
:param fill: 填充颜色
:param outline: 描线颜色
:param width: 描线宽度
"""
self.draw.ellipse(pos, fill, outline, width)
def text(
self, pos: Tuple[int, int], text: str, fill: Tuple[int, int, int] = (0, 0, 0)
self, pos: Tuple[int, int], text: str, fill: Tuple[int, int, int] = (0, 0, 0), center_type: Optional[str] = None
):
"""
说明:
@@ -234,8 +309,24 @@ class CreateImg:
:param pos: 文字位置
:param text: 文字内容
:param fill: 文字颜色
:param center_type: 居中类型,可能的值 center: 完全居中,by_width: 水平居中,by_height: 垂直居中
"""
self.draw.text(pos, text, fill=fill, font=self.ttfont)
if center_type:
if center_type not in ["center", "by_height", "by_width"]:
raise ValueError(
"center_type must be 'center', 'by_width' or 'by_height'"
)
w, h = self.w, self.h
ttf_w, ttf_h = self.getsize(text)
if center_type == 'center':
w = int((w - ttf_w) / 2)
h = int((h - ttf_h) / 2)
elif center_type == 'by_width':
w = int((w - ttf_w) / 2)
elif center_type == 'by_height':
h = int((h - ttf_h) / 2)
pos = (w, h)
self.draw.text(pos, text, fill=fill, font=self.font)
def save(self, path: str):
"""
@@ -291,13 +382,14 @@ class CreateImg:
参数:
:param word: 文本内容
"""
return self.ttfont.getsize(word)[0] > self.w
return self.font.getsize(word)[0] > self.w
def transparent(self, n: int = 0):
def transparent(self, alpha_ratio: float = 1, n: int = 0):
"""
说明:
图片透明化
参数:
:param alpha_ratio: 透明化程度
:param n: 透明化大小内边距
"""
self.markImg = self.markImg.convert("RGBA")
@@ -305,7 +397,7 @@ class CreateImg:
for i in range(n, x - n):
for k in range(n, y - n):
color = self.markImg.getpixel((i, k))
color = color[:-1] + (100,)
color = color[:-1] + (int(100 * alpha_ratio),)
self.markImg.putpixel((i, k), color)
def pic2bs4(self) -> str:
+4 -3
View File
@@ -1,14 +1,13 @@
from configs.path_config import IMAGE_PATH, VOICE_PATH
from nonebot.adapters.cqhttp.message import MessageSegment
from services.log import logger
from typing import Optional
from typing import Union
from pathlib import Path
import os
import ujson
def image(
img_name: str = None, path: str = None, abspath: str = None, b64: str = None
img_name: Union[str, Path] = None, path: str = None, abspath: str = None, b64: str = None
) -> MessageSegment or str:
"""
说明:
@@ -29,6 +28,8 @@ def image(
elif b64:
return MessageSegment.image(b64 if "base64://" in b64 else "base64://" + b64)
else:
# if isinstance(img_name, Path):
# return MessageSegment.image(img_name)
if "http" in img_name:
return MessageSegment.image(img_name)
if len(img_name.split(".")) == 1:
+4 -1
View File
@@ -2,10 +2,13 @@ from typing import Optional
from .group_manager import GroupManager
from pathlib import Path
from .data_source import init
from .data_class import StaticData
# 群权限
# 群管理
group_manager: Optional[GroupManager] = GroupManager(
Path() / "data" / "manager" / "group_manager.json"
)
withdraw_message_id_manager: Optional[StaticData] = StaticData(None)
init(group_manager)
+7 -6
View File
@@ -1,4 +1,4 @@
from typing import Union
from typing import Union, Optional
from pathlib import Path
import ujson as json
@@ -8,12 +8,13 @@ class StaticData:
静态数据共享类
"""
def __init__(self, file: Path):
file.parent.mkdir(exist_ok=True, parents=True)
self.file = file
def __init__(self, file: Optional[Path]):
self.data = {}
if file.exists():
self.data: dict = json.load(open(file, "r", encoding="utf8"))
if file:
file.parent.mkdir(exist_ok=True, parents=True)
self.file = file
if file.exists():
self.data: dict = json.load(open(file, "r", encoding="utf8"))
def set(self, key, value):
self.data[key] = value
+12 -2
View File
@@ -1,4 +1,5 @@
from typing import Optional
from configs.config import DEFAULT_GROUP_LEVEL
from typing import Optional, List
from pathlib import Path
from .data_class import StaticData
@@ -132,6 +133,13 @@ class GroupManager(StaticData):
if group_id in self.data['super']['white_group_list']:
self.data['super']['white_group_list'].remove(group_id)
def get_group_white_list(self) -> List[str]:
"""
说明:
获取所有群白名单
"""
return self.data['super']['white_group_list']
def _set_plugin_status(
self,
plugin_cmd: str,
@@ -183,4 +191,6 @@ class GroupManager(StaticData):
参数:
:param group_id: 群号
"""
self.data["group_manager"][group_id] = {"level": 5, "close_plugins": []}
self.data["group_manager"][group_id] = {"level": DEFAULT_GROUP_LEVEL, "close_plugins": []}
+27 -6
View File
@@ -3,7 +3,7 @@ from collections import defaultdict
from nonebot import require
from configs.path_config import TXT_PATH
from configs.config import SYSTEM_PROXY
from typing import List, Union
from typing import List, Union, Optional
from nonebot.adapters import Bot
import nonebot
import pytz
@@ -48,16 +48,16 @@ class UserExistLimiter:
self.flag_data = defaultdict(bool)
self.time = time.time()
def set_True(self, key: Union[str, int, float]):
def set_true(self, key: Union[str, int, float]):
self.time = time.time()
self.flag_data[key] = True
def set_False(self, key: Union[str, int, float]):
def set_false(self, key: Union[str, int, float]):
self.flag_data[key] = False
def check(self, key: Union[str, int, float]) -> bool:
if time.time() - self.time > 30:
self.set_False(key)
self.set_false(key)
return False
return self.flag_data[key]
@@ -169,12 +169,15 @@ def is_number(s: str) -> bool:
return False
def get_bot() -> Bot:
def get_bot() -> Optional[Bot]:
"""
说明:
获取 bot 对象
"""
return list(nonebot.get_bots().values())[0]
try:
return list(nonebot.get_bots().values())[0]
except IndexError:
return None
def get_message_at(data: str) -> List[int]:
@@ -322,3 +325,21 @@ def cn2py(word: str) -> str:
for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
temp += "".join(i)
return temp
def change_picture_links(url: str, mode: str):
"""
说明:
根据配置改变图片大小
参数:
:param url: 图片原图链接
:param mode: 模式
"""
if mode == 'master':
img_sp = url.rsplit('.', maxsplit=1)
url = img_sp[0]
img_type = img_sp[1]
url = url.replace('original', 'master') + f'_master1200.{img_type}'
return url