From 2fd4d7e2e0a1bb2d150bb80d98bca91b4e6ff44d Mon Sep 17 00:00:00 2001 From: KT Date: Sat, 10 Jun 2023 10:36:34 +0800 Subject: [PATCH] feat: Add replace_icon_name_with_url param Add the replace_icon_name_with_url parameter to the client's fetch_user and fetch_user_v1 methods. --- mihomo/client.py | 32 ++++++++++++++++++++++++-------- mihomo/tools.py | 37 ++++++++++++++++++++++++++++++++----- pyproject.toml | 2 +- 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/mihomo/client.py b/mihomo/client.py index 06190fb80..65efa697f 100644 --- a/mihomo/client.py +++ b/mihomo/client.py @@ -3,10 +3,10 @@ from enum import Enum import aiohttp +from . import tools from .errors import HttpRequestError, InvalidParams, UserNotFound from .models import StarrailInfoParsed from .models.v1 import StarrailInfoParsedV1 -from .tools import remove_empty_dict, replace_trailblazer_name class Language(Enum): @@ -91,36 +91,52 @@ class MihomoAPI: case _: raise HttpRequestError(response.status, str(response.reason)) - async def fetch_user(self, uid: int) -> StarrailInfoParsed: + async def fetch_user( + self, + uid: int, + *, + replace_icon_name_with_url: bool = False, + ) -> StarrailInfoParsed: """ Fetches user data from the API. Args: - uid (int): The user ID. + - uid (`int`): The user ID. + - replace_icon_name_with_url (`bool`): Whether to replace icon names with asset URLs. Returns: StarrailInfoParsed: The parsed user data from mihomo API. """ data = await self.request(uid, self.lang) + if replace_icon_name_with_url is True: + data = tools.replace_icon_name_with_url(data) data = StarrailInfoParsed.parse_obj(data) return data - async def fetch_user_v1(self, uid: int) -> StarrailInfoParsedV1: + async def fetch_user_v1( + self, + uid: int, + *, + replace_icon_name_with_url: bool = False, + ) -> StarrailInfoParsedV1: """ - Fetches user data from the API using version 1. + Fetches user data from the API using version 1 format. Args: - uid (int): The user ID. + - uid (`int`): The user ID. + - replace_icon_name_with_url (`bool`): Whether to replace icon names with asset URLs. Returns: StarrailInfoParsedV1: The parsed user data from the Mihomo API (version 1). """ data = await self.request(uid, self.lang, params={"version": "v1"}) - data = remove_empty_dict(data) + data = tools.remove_empty_dict(data) + if replace_icon_name_with_url is True: + data = tools.replace_icon_name_with_url(data) data = StarrailInfoParsedV1.parse_obj(data) - data = replace_trailblazer_name(data) + data = tools.replace_trailblazer_name(data) return data def get_icon_url(self, icon: str) -> str: diff --git a/mihomo/tools.py b/mihomo/tools.py index 25e21b8fc..182949618 100644 --- a/mihomo/tools.py +++ b/mihomo/tools.py @@ -1,21 +1,23 @@ -from typing import TypeVar +from typing import Final, TypeVar from .models import Character, StarrailInfoParsed from .models.v1 import Character, StarrailInfoParsedV1 -T = TypeVar("T") +RawData = TypeVar("RawData") ParsedData = TypeVar("ParsedData", StarrailInfoParsed, StarrailInfoParsedV1) +ASSET_URL: Final[str] = "https://raw.githubusercontent.com/Mar-7th/StarRailRes/master" -def remove_empty_dict(data: T) -> T: + +def remove_empty_dict(data: RawData) -> RawData: """ Recursively removes empty dictionaries from the given raw data. Args: - - data (`T`): The input data. + - data (`RawData`): The input raw data. Returns: - - `T`: The data with empty dictionaries removed. + - `RawData`: The data with empty dictionaries removed. """ if isinstance(data, dict): for key in data.keys(): @@ -26,6 +28,31 @@ def remove_empty_dict(data: T) -> T: return data +def replace_icon_name_with_url(data: RawData) -> RawData: + """ + Replaces icon file names with asset URLs in the given raw data. + + Example: Replace "/icon/avatar/1201.png" with + "https://raw.githubusercontent.com/Mar-7th/StarRailRes/master/icon/avatar/1201.png" + + Args: + - data (`RawData`): The input raw data. + + Returns: + - `RawData`: The data with icon file names replaced by asset URLs. + """ + if isinstance(data, dict): + for key in data.keys(): + data[key] = replace_icon_name_with_url(data[key]) + elif isinstance(data, list): + for i in range(len(data)): + data[i] = replace_icon_name_with_url(data[i]) + elif isinstance(data, str): + if ".png" in data: + data = ASSET_URL + "/" + data + return data + + def replace_trailblazer_name(data: StarrailInfoParsedV1) -> StarrailInfoParsedV1: """ Replaces the trailblazer name with the player's name. diff --git a/pyproject.toml b/pyproject.toml index 5902241dc..e7bc476b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "mihomo" -version = "1.1.0" +version = "1.1.1" authors = [ { name="KT", email="xns77477@gmail.com" }, ]