From c65209ec65b02025a73c3ee66b34941fbc89e7ba Mon Sep 17 00:00:00 2001 From: lenlino <65199014+lenlino@users.noreply.github.com> Date: Thu, 28 Mar 2024 08:51:57 +0900 Subject: [PATCH] =?UTF-8?q?weight=E3=82=92=E3=82=B5=E3=83=BC=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=8B=E3=82=89=E5=8F=96=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/CardCommand.py | 2 +- generate/utils.py | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/commands/CardCommand.py b/commands/CardCommand.py index aee84c1..fb78755 100644 --- a/commands/CardCommand.py +++ b/commands/CardCommand.py @@ -118,7 +118,7 @@ async def button_callback(interaction): # 重み weight_text = "" avatar_id = json_parsed["characters"][select_number]['id'] - weight_dict = generate.utils.get_weight(avatar_id) + weight_dict = await generate.utils.get_weight(avatar_id) for k, v in weight_dict.items(): if v == 0: diff --git a/generate/utils.py b/generate/utils.py index 2cc4510..270e070 100644 --- a/generate/utils.py +++ b/generate/utils.py @@ -26,6 +26,15 @@ async def get_image_from_url(url: str): return f"{os.path.dirname(os.path.abspath(__file__))}/{replaced_path}" +async def get_json_from_urlpath(path: str): + async with aiohttp.ClientSession(connector_owner=False, connector=conn) as session: + async with session.get(f"https://hcs.lenlino.com{path}") as response: + if response.status == 200: + result_json = await response.json() + return result_json + return None + + async def get_json_from_url(uid: str, lang: str): result_json = {} async with aiohttp.ClientSession(connector_owner=False, connector=conn) as session: @@ -255,11 +264,18 @@ def get_mihomo_lang(discord_lang): else: return "en" +weight_dict = {} -def get_weight(chara_id): - with open(f"{os.path.dirname(os.path.abspath(__file__))}/weight.json") as f: - weight_json = json.load(f) - return weight_json[str(chara_id)]["weight"] + +async def get_weight(chara_id): + """with open(f"{os.path.dirname(os.path.abspath(__file__))}/weight.json") as f: + weight_json = json.load(f)""" + + if weight_dict[str(chara_id)]: + return weight_dict[str(chara_id)]["weight"] + else: + weight_dict[str(chara_id)] = await get_json_from_urlpath(f"/weight/{chara_id}") + return await get_weight(chara_id) def get_score_rank(chara_id, uid, score):