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.
This commit is contained in:
KT 2023-06-10 10:36:34 +08:00
parent 4a892d213b
commit 2fd4d7e2e0
3 changed files with 57 additions and 14 deletions

View File

@ -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:

View File

@ -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.

View File

@ -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" },
]