From 79eef585c97b0a753ad1d67ceb5a884300e2fe38 Mon Sep 17 00:00:00 2001 From: CMHopeSunshine <277073121@qq.com> Date: Thu, 8 Dec 2022 18:03:32 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=9B=BE=E6=A0=87=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LittlePaimon/utils/files.py | 4 ++-- LittlePaimon/utils/requests.py | 41 +++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/LittlePaimon/utils/files.py b/LittlePaimon/utils/files.py index b7ded99..591f13c 100644 --- a/LittlePaimon/utils/files.py +++ b/LittlePaimon/utils/files.py @@ -49,8 +49,8 @@ async def load_image( img = Image.open(path) elif path.name.startswith(('UI_', 'Skill_')): img = await aiorequests.download_icon(path.name, headers=headers, save_path=path, follow_redirects=True) - if img is None: - return Image.new('RGBA', size=size, color=(0, 0, 0, 0)) + if img is None or isinstance(img, str): + return Image.new('RGBA', size=size or (50, 50), color=(0, 0, 0, 0)) else: raise FileNotFoundError(f'{path} not found') cache_image[str(path)] = img diff --git a/LittlePaimon/utils/requests.py b/LittlePaimon/utils/requests.py index caa573d..a51ad63 100644 --- a/LittlePaimon/utils/requests.py +++ b/LittlePaimon/utils/requests.py @@ -101,9 +101,9 @@ class aiorequests: if resp.headers.get('etag') == 'W/"6363798a-13c7"' or resp.headers.get( 'content-md5') == 'JeG5b/z8SpViMmO/E9eayA==': save_path = False - resp = resp.read() - if b'NoSuchKey' in resp or b'character not exists' in resp: + if resp.headers.get('Content-Type') not in ['image/png', 'image/jpeg']: return 'No Such File' + resp = resp.read() img = Image.open(BytesIO(resp)) except SSLCertVerificationError: async with httpx.AsyncClient() as client: @@ -115,9 +115,9 @@ class aiorequests: if resp.headers.get('etag') == 'W/"6363798a-13c7"' or resp.headers.get( 'content-md5') == 'JeG5b/z8SpViMmO/E9eayA==': save_path = False - resp = resp.read() - if b'error' in resp: + if resp.headers.get('Content-Type') not in ['image/png', 'image/jpeg']: return 'No Such File' + resp = resp.read() img = Image.open(BytesIO(resp)) if size: if isinstance(size, float): @@ -169,24 +169,29 @@ class aiorequests: :param headers: 请求头 :param save_path: 保存路径 """ - url = None - if name.startswith(('UI_EquipIcon', 'UI_RelicIcon')): - url = f'https://upload-bbs.mihoyo.com/game_record/genshin/equip/{name}' - elif name.startswith('UI_Talent'): - url = f'https://upload-bbs.mihoyo.com/game_record/genshin/constellation_icon/{name}' - elif name.startswith('UI_AvatarIcon'): - if name.endswith('UI_AvatarIcon_Side'): - url = f'https://upload-bbs.mihoyo.com/game_record/genshin/character_side_icon/{name}' - elif name.endswith('Card.png'): - url = f'https://upload-bbs.mihoyo.com/game_record/genshin/character_card_icon/{name}' - else: - url = f'https://upload-bbs.mihoyo.com/game_record/genshin/character_icon/{name}' urls = [ - url, - f'https://file.microgg.cn/KimigaiiWuyi/resource/icon/{name}' + f'https://file.microgg.cn/KimigaiiWuyi/resource/icon/{name}', f'https://api.ambr.top/assets/UI/{name}', f'https://enka.network/ui/{name}', ] + if name.startswith(('UI_EquipIcon', 'UI_RelicIcon')): + urls.insert(0, f'https://upload-bbs.mihoyo.com/game_record/genshin/equip/{name}') + elif name.startswith('UI_Talent'): + urls.insert(0, f'https://upload-bbs.mihoyo.com/game_record/genshin/constellation_icon/{name}') + urls.insert(1, f'https://ghproxy.com/https://raw.githubusercontent.com/CMHopeSunshine/GenshinWikiMap/master/resources/constellation/{name}') + elif name.startswith('UI_AvatarIcon'): + if name.endswith('UI_AvatarIcon_Side'): + urls.insert(0, f'https://upload-bbs.mihoyo.com/game_record/genshin/character_side_icon/{name}') + elif name.endswith('Card.png'): + urls.insert(0, f'https://upload-bbs.mihoyo.com/game_record/genshin/character_card_icon/{name}') + else: + urls.insert(0, f'https://upload-bbs.mihoyo.com/game_record/genshin/character_icon/{name}') + elif name.startswith('UI_Gacha_AvatarImg_'): + urls.insert(0, f'https://ghproxy.com/https://raw.githubusercontent.com/CMHopeSunshine/GenshinWikiMap/master/resources/gacha_img/{name}') + elif name.startswith('Skill_'): + urls.insert(0, f'https://ghproxy.com/https://raw.githubusercontent.com/CMHopeSunshine/GenshinWikiMap/master/resources/talent/{name}') + elif name.startswith('UI_ItemIcon_'): + urls.insert(0, f'https://ghproxy.com/https://raw.githubusercontent.com/CMHopeSunshine/GenshinWikiMap/master/resources/materials/{name}') for url in urls: with contextlib.suppress(Exception): if url is not None: