This commit is contained in:
hibiki
2021-06-04 18:01:33 +08:00
parent 9e401e7a83
commit 8c5e0bd381
43 changed files with 1605 additions and 426 deletions
+44 -18
View File
@@ -1,19 +1,21 @@
import os
import aiohttp
import aiofiles
from asyncio.exceptions import TimeoutError
from aiohttp.client_exceptions import InvalidURL
from typing import List, Union, Set
import asyncio
from pathlib import Path
from .config import path_dict, DRAW_PATH
from .config import path_dict
from configs.path_config import IMAGE_PATH
import nonebot
import pypinyin
from util.img_utils import CreateImg
from configs.path_config import IMAGE_PATH
import platform
from services.log import logger
import random
from dataclasses import dataclass
from services.log import logger
import os
import asyncio
try:
import ujson as json
except ModuleNotFoundError:
@@ -23,6 +25,9 @@ except ModuleNotFoundError:
driver: nonebot.Driver = nonebot.get_driver()
loop = asyncio.get_event_loop()
headers = {'User-Agent': '"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"'}
@@ -43,8 +48,9 @@ class UpEvent:
async def download_img(url: str, path: str, name: str) -> bool:
path = path.split('_')[0]
codename = cn2py(name)
Path(IMAGE_PATH + f'/draw_card/{path}').mkdir(exist_ok=True, parents=True)
if not os.path.exists(IMAGE_PATH + f'/draw_card/{path}/{codename}.png'):
file = Path(IMAGE_PATH + f'/draw_card/{path}/{codename}.png')
if not file.exists():
file.parent.mkdir(exist_ok=True, parents=True)
try:
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url, timeout=7) as response:
@@ -66,7 +72,7 @@ async def download_img(url: str, path: str, name: str) -> bool:
@driver.on_startup
def _check_dir():
for dir_name in path_dict.keys():
_p = Path(DRAW_PATH + f'/draw_card/' + dir_name)
_p = Path(IMAGE_PATH + f'/draw_card/' + dir_name)
if not _p.exists():
_p.mkdir(parents=True, exist_ok=True)
@@ -74,17 +80,20 @@ def _check_dir():
async def generate_img(card_set: Union[Set[BaseData], List[BaseData]], game_name: str, star_list: list) -> str:
# try:
img_list = []
color_list = []
background_list = []
for x in card_set:
if game_name == 'prts':
if x.star == 6:
color_list.append('#FFD700')
background_list.append('#FFD700')
elif x.star == 5:
color_list.append('#DAA520')
background_list.append('#DAA520')
elif x.star == 4:
color_list.append('#9370D8')
background_list.append('#9370D8')
else:
color_list.append('white')
background_list.append('white')
if game_name == 'azur':
if os.path.exists(IMAGE_PATH + f'/draw_card/{game_name}/{x.star}_star.png'):
background_list.append(IMAGE_PATH + f'/draw_card/{game_name}/{x.star}_star.png')
pyname = cn2py(x.name)
img_list.append(IMAGE_PATH + f'/draw_card/{game_name}/{pyname}.png')
img_len = len(img_list)
@@ -96,7 +105,7 @@ async def generate_img(card_set: Union[Set[BaseData], List[BaseData]], game_name
h = 100 * int(img_len / 10)
else:
h = 100 * int(img_len / 10) + 100
card_img = await asyncio.get_event_loop().run_in_executor(None, _pst, h, img_list, game_name, color_list)
card_img = await asyncio.get_event_loop().run_in_executor(None, _pst, h, img_list, game_name, background_list)
num = 0
for n in star_list:
num += n
@@ -105,23 +114,28 @@ async def generate_img(card_set: Union[Set[BaseData], List[BaseData]], game_name
return A.pic2bs4()
def _pst(h: int, img_list: list, game_name: str, color_list: list):
def _pst(h: int, img_list: list, game_name: str, background_list: list):
card_img = CreateImg(100 * 10, h, 100, 100)
idx = 0
for img in img_list:
try:
if game_name == 'prts':
bk = CreateImg(100, 100, color=color_list[idx])
bk = CreateImg(100, 100, color=background_list[idx])
b = CreateImg(94, 94, background=img)
bk.paste(b, (3, 3))
b = bk
idx += 1
elif game_name == 'azur' and background_list:
bk = CreateImg(100, 100, background=background_list[idx])
b = CreateImg(98, 90, background=img)
bk.paste(b, (1, 5))
b = bk
else:
b = CreateImg(100, 100, background=img)
except FileNotFoundError:
logger.warning(f'{img} not exists')
print(f'{img} not exists')
b = CreateImg(100, 100, color='black')
card_img.paste(b)
idx += 1
return card_img
@@ -234,3 +248,15 @@ def check_num(num: str, max_num: int) -> 'str, bool':
else:
return str(num), True
# 移除windows和linux下特殊字符
def remove_prohibited_str(name: str):
if platform.system().lower() == 'windows':
tmp = ''
for i in name:
if i not in ['\\', '/', ':', '*', '?', '"', '<', '>', '|']:
tmp += i
name = tmp
else:
name = name.replace('/', '')
return name