From 03a8b6b3bd8a6ff540d8eaba24006d82332e234d Mon Sep 17 00:00:00 2001 From: MingxuanGame Date: Wed, 3 May 2023 09:05:42 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E7=BC=93=E5=AD=98=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StarRailUID/starrailuid_roleinfo/utils.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/StarRailUID/starrailuid_roleinfo/utils.py b/StarRailUID/starrailuid_roleinfo/utils.py index ef34197..9c09912 100644 --- a/StarRailUID/starrailuid_roleinfo/utils.py +++ b/StarRailUID/starrailuid_roleinfo/utils.py @@ -3,9 +3,13 @@ from typing import List, TypeVar, Generator from PIL import Image from aiohttp import ClientSession +from gsuid_core.data_store import get_res_path T = TypeVar("T") +ROLEINFO_PATH = get_res_path() / 'StarRailUID' / 'roleinfo' +ROLEINFO_PATH.mkdir(parents=True, exist_ok=True) + def wrap_list(lst: List[T], n: int) -> Generator[List[T], None, None]: for i in range(0, len(lst), n): @@ -13,6 +17,14 @@ def wrap_list(lst: List[T], n: int) -> Generator[List[T], None, None]: async def get_icon(url: str) -> Image.Image: - async with ClientSession() as client: - async with client.get(url) as resp: - return Image.open(BytesIO(await resp.read())).convert("RGBA") + name = url.split('/')[-1] + path = ROLEINFO_PATH / name + if (path).exists(): + content = path.read_bytes() + else: + async with ClientSession() as client: + async with client.get(url) as resp: + content = await resp.read() + with open(path, "wb") as f: + f.write(content) + return Image.open(BytesIO(content)).convert("RGBA")