diff --git a/Paimon_Info/draw_player_card.py b/Paimon_Info/draw_player_card.py index fcf2970..0c980a5 100644 --- a/Paimon_Info/draw_player_card.py +++ b/Paimon_Info/draw_player_card.py @@ -196,7 +196,7 @@ async def draw_player_card(data, chara_data, uid, nickname="旅行者"): bg_draw = ImageDraw.Draw(bg_img) # 头部名片 name_id = random.choice(data['avatars'][0:8])['id'] - name_card = load_image(os.path.join(res_path, 'name_card', f'{name_id}.png'), crop=(0, 40, 840, 360), size=(846, 352)) + name_card = load_image(os.path.join(res_path, 'name_card', f'{name_id}.png'), crop=(0, 40, 840, 360), size=(846, 360)) avatar = load_image(os.path.join(res_path, 'role_profile', f'{name_id}.png'), size=(240, 240)) bg_img.alpha_composite(name_card, (57, 27)) bg_img.alpha_composite(avatar, (360, 25)) diff --git a/Paimon_Wiki/__init__.py b/Paimon_Wiki/__init__.py index e945406..632d824 100644 --- a/Paimon_Wiki/__init__.py +++ b/Paimon_Wiki/__init__.py @@ -8,9 +8,9 @@ from nonebot.params import RegexDict from nonebot.typing import T_State from utils.alias_handler import get_match_alias +from utils.file_handler import load_json_from_url from utils.message_util import MessageBuild from .abyss_rate_draw import draw_rate_rank, draw_teams_rate -from .blue import get_blue_pic __usage__ = ''' 1.[xx角色攻略]查看西风驿站出品的角色一图流攻略 @@ -42,7 +42,12 @@ async def genshinAttribute(event: MessageEvent): name = event.message.extract_plain_text().replace('参考面板', '').strip() realname = get_match_alias(name) if realname: - img = await get_blue_pic(realname) + blue = await load_json_from_url('https://static.cherishmoon.fun/LittlePaimon/blue/blue.json') + if realname in blue.keys(): + img = await MessageBuild.StaticImage(url=f'LittlePaimon/blue/{blue[realname][0]}.jpg', + crop=(0, int(blue[realname][1][0]), 1080, int(blue[realname][1][1]))) + else: + img = MessageBuild.Text(f'没有找到{name}的参考面板') await attribute.finish(img) else: await attribute.finish(f'没有找到{name}的参考面板', at_sender=True) @@ -91,7 +96,7 @@ async def abyss_team_handler(event: MessageEvent, reGroup=RegexDict()): await abyss_team.finish(abyss_img) -def create_choice_command(endswith: str, type_: str, url: str, exclude: list = []): +def create_choice_command(endswith: str, type_: str, url: str, tips: str = None): command = on_endswith(endswith, priority=6, block=True) @command.handle() @@ -108,12 +113,14 @@ def create_choice_command(endswith: str, type_: str, url: str, exclude: list = [ if name == 'q': await command.finish() match_alias = get_match_alias(name, type_) - if isinstance(match_alias, str) and match_alias not in exclude: - await command.finish(await MessageBuild.StaticImage(url=url.format(match_alias))) - elif isinstance(match_alias, list) and len(match_alias) == 1 and match_alias[0] not in exclude: - await command.finish(await MessageBuild.StaticImage(url=url.format(match_alias[0]))) + if isinstance(match_alias, str): + await command.finish( + await MessageBuild.StaticImage(url=url.format(match_alias), tips=tips.format(match_alias))) + elif isinstance(match_alias, list) and len(match_alias) == 1: + await command.finish( + await MessageBuild.StaticImage(url=url.format(match_alias[0]), tips=tips.format(match_alias[0]))) else: - if not match_alias or match_alias in exclude: + if not match_alias: await command.finish(f'没有{state["name"]}的{endswith}哦~', at_sender=True) else: if isinstance(match_alias, dict): @@ -133,7 +140,8 @@ def create_choice_command(endswith: str, type_: str, url: str, exclude: list = [ await command.finish() if choice.isdigit() and (1 <= int(choice) <= len(match_alias)): await command.finish( - await MessageBuild.StaticImage(url=url.format(match_alias[int(choice) - 1]))) + await MessageBuild.StaticImage(url=url.format(match_alias[int(choice) - 1]), + tips=tips.format(match_alias[int(choice) - 1]))) if choice not in match_alias: state['times'] = state['times'] + 1 if 'times' in state else 1 if state['times'] == 1: @@ -142,12 +150,11 @@ def create_choice_command(endswith: str, type_: str, url: str, exclude: list = [ await command.reject(f'别调戏派蒙啦,快选一个吧,不想问了请回答\"q\"!', at_sender=True) elif state['times'] >= 3: await command.finish(f'看来旅行者您有点神志不清哦(,下次再问派蒙吧' + MessageSegment.face(146), at_sender=True) - await command.finish(await MessageBuild.StaticImage(url=url.format(choice))) + await command.finish(await MessageBuild.StaticImage(url=url.format(choice), tips=tips.format(choice))) -create_choice_command('原魔图鉴', 'monsters', 'LittlePaimon/MonsterMaps/{}.jpg') -create_choice_command('武器攻略', 'weapons', 'LittlePaimon/WeaponGuild/{}.png') -create_choice_command('角色攻略', 'roles', 'LittlePaimon/XFGuide/{}.jpg', ['夜兰', '久岐忍', '鹿野院平藏']) -create_choice_command('角色材料', 'roles', 'LittlePaimon/RoleMaterials/{}材料.jpg') -create_choice_command('收益曲线', 'roles', 'LittlePaimon/blue/{}.jpg', ['神里绫人', '夜兰', '久岐忍', '鹿野院平藏']) - +create_choice_command('原魔图鉴', 'monsters', 'LittlePaimon/MonsterMaps/{}.jpg', '暂时没有{}的原魔图鉴哦~') +create_choice_command('武器攻略', 'weapons', 'LittlePaimon/WeaponGuild/{}.png', '暂时没有{}的武器攻略哦~') +create_choice_command('角色攻略', 'roles', 'LittlePaimon/XFGuide/{}.jpg', '暂时没有{}的角色攻略哦~') +create_choice_command('角色材料', 'roles', 'LittlePaimon/RoleMaterials/{}材料.jpg', '暂时没有{}的角色材料哦~') +create_choice_command('收益曲线', 'roles', 'LittlePaimon/blue/{}.jpg', '暂时没有{}的收益曲线哦~') diff --git a/Paimon_Wiki/blue.py b/Paimon_Wiki/blue.py deleted file mode 100644 index 6e18b3d..0000000 --- a/Paimon_Wiki/blue.py +++ /dev/null @@ -1,63 +0,0 @@ -from utils.message_util import MessageBuild - -blue = { - '胡桃': ['火', (0, 1886)], - '托马': ['火', (1886, 2420)], - '宵宫': ['火', (2420, 2955)], - '烟绯': ['火', (2955, 3492)], - '可莉': ['火', (3492, 4029)], - '迪卢克': ['火', (4029, 4567)], - '辛焱': ['火', (4567, 5511)], - '安柏': ['火', (5511, 6047)], - '香菱': ['火', (6047, 6982)], - '班尼特': ['火', (6982, 7932)], - '神里绫人': ['水', (0, 750)], - '珊瑚宫心海': ['水', (750, 1700)], - '达达利亚': ['水', (1700, 2424)], - '行秋': ['水', (2424, 2920)], - '莫娜': ['水', (2920, 3640)], - '芭芭拉': ['水', (3640, 4396)], - '申鹤': ['冰', (0, 959)], - '神里绫华': ['冰', (959, 1496)], - '优菈': ['冰', (1496, 2231)], - '甘雨': ['冰', (2231, 3569)], - '凯亚': ['冰', (3569, 4102)], - '重云': ['冰', (4102, 4639)], - '七七': ['冰', (4639, 5374)], - '迪奥娜': ['冰', (5374, 5710)], - '罗莎莉亚': ['冰', (5710, 6044)], - '埃洛伊': ['冰', (6044, 6394)], - '八重神子': ['雷', (0, 947)], - '雷电将军': ['雷', (947, 1474)], - '九条裟罗': ['雷', (1474, 2001)], - '刻晴': ['雷', (2001, 2730)], - '雷泽': ['雷', (2730, 3258)], - '菲谢尔': ['雷', (3258, 3786)], - '丽莎': ['雷', (3786, 4310)], - '北斗': ['雷', (4310, 4838)], - '雷主': ['雷', (4838, 5177)], - '早柚': ['风', (0, 553)], - '枫原万叶': ['风', (553, 1078)], - '魈': ['风', (1078, 1803)], - '温迪': ['风', (1803, 2527)], - '琴': ['风', (2527, 3252)], - '砂糖': ['风', (3252, 3977)], - '风主': ['风', (3977, 4311)], - '空': ['风', (3977, 4311)], - '荧': ['风', (3977, 4311)], - '荒泷一斗': ['岩', (0, 741)], - '五郎': ['岩', (741, 1055)], - '阿贝多': ['岩', (1055, 1767)], - '钟离': ['岩', (1767, 2475)], - '诺艾尔': ['岩', (2475, 3188)], - '凝光': ['岩', (3188, 3900)], - '岩主': ['岩', (3900, 4608)], - '云堇': ['岩', (4608, 5325)] -} - - -async def get_blue_pic(name): - if name in blue.keys(): - return await MessageBuild.StaticImage(url=f'LittlePaimon/blue/{blue[name][0]}.jpg', - crop=(0, int(blue[name][1][0]), 1080, int(blue[name][1][1]))) - return None diff --git a/utils/aiorequests.py b/utils/aiorequests.py index 90a20ab..54d8a6f 100644 --- a/utils/aiorequests.py +++ b/utils/aiorequests.py @@ -90,7 +90,10 @@ async def get_img(url: str, params=params, timeout=timeout, **kwargs) - img = Image.open(BytesIO(resp.read())) + resp = resp.read() + if b'error' in resp: + return 'No Such File' + img = Image.open(BytesIO(resp)) if size: img = img.resize(size, Image.ANTIALIAS) if mode: @@ -110,7 +113,10 @@ async def get_img(url: str, params=params, timeout=timeout, **kwargs) - img = Image.open(BytesIO(resp.read())) + resp = resp.read() + if b'error' in resp: + return 'No Such File' + img = Image.open(BytesIO(resp)) if size: img = img.resize(size, Image.ANTIALIAS) if mode: diff --git a/utils/file_handler.py b/utils/file_handler.py index 9babe82..1276cbe 100644 --- a/utils/file_handler.py +++ b/utils/file_handler.py @@ -37,18 +37,12 @@ def load_json(file: str = None, path: Union[Path, str] = None, encoding: str = ' return json.load(path.open('r', encoding=encoding)) -async def load_json_from_url(url: str, encoding: str = 'utf-8', refresh: bool = False): - if 'static.cherishmoon.fun' in url: - url_path = Path() / 'data' / url.split('static.cherishmoon.fun/')[1] - if refresh or not url_path.exists(): - try: - resp = await aiorequests.get(url) - except SSLCertVerificationError: - resp = await aiorequests.get(url.replace('https', 'http')) - save_json(resp.json(), path=url_path, encoding=encoding) - return resp.json() - else: - return load_json(path=url_path, encoding=encoding) +async def load_json_from_url(url: str): + try: + resp = await aiorequests.get(url) + except SSLCertVerificationError: + resp = await aiorequests.get(url.replace('https', 'http')) + return resp.json() def save_json(data, file: str = None, path: Union[Path, str] = None, encoding: str = 'utf-8'): diff --git a/utils/message_util.py b/utils/message_util.py index a1b39ac..39ca5a4 100644 --- a/utils/message_util.py +++ b/utils/message_util.py @@ -1,5 +1,6 @@ import re import base64 +from time import time from PIL import Image from pathlib import Path @@ -39,14 +40,17 @@ class MessageBuild: size: Optional[Tuple[int, int]] = None, crop: Optional[Tuple[int, int, int, int]] = None, quality: Optional[int] = 100, - mode: Optional[str] = 'RGB' + mode: Optional[str] = 'RGB', + tips: Optional[str] = None ): path = Path() / 'data' / url - if not path.exists(): + if path.exists() and not check_time(path.stat().st_mtime, 3): + img = Image.open(path) + else: path.parent.mkdir(parents=True, exist_ok=True) img = await aiorequests.get_img(url='https://static.cherishmoon.fun/' + url, save_path=path) - else: - img = Image.open(path) + if img == 'No Such File': + return MessageSegment.text(tips or '缺少该静态资源') if size: img = img.resize(size) if crop: @@ -172,3 +176,14 @@ def transform_uid(msg): if uid: uid_list.append(uid.group('uid')) return uid_list if len(uid_list) > 1 else uid_list[0] if uid_list else None + + +# 检查该时间戳和当前时间戳相差是否超过n天, 超过则返回True +def check_time(time_stamp, n=1): + time_stamp = int(time_stamp) + now = int(time()) + if (now - time_stamp) / 86400 > n: + return True + else: + return False +