mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-22 23:57:26 +08:00
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:
parent
4a892d213b
commit
2fd4d7e2e0
@ -3,10 +3,10 @@ from enum import Enum
|
|||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
|
from . import tools
|
||||||
from .errors import HttpRequestError, InvalidParams, UserNotFound
|
from .errors import HttpRequestError, InvalidParams, UserNotFound
|
||||||
from .models import StarrailInfoParsed
|
from .models import StarrailInfoParsed
|
||||||
from .models.v1 import StarrailInfoParsedV1
|
from .models.v1 import StarrailInfoParsedV1
|
||||||
from .tools import remove_empty_dict, replace_trailblazer_name
|
|
||||||
|
|
||||||
|
|
||||||
class Language(Enum):
|
class Language(Enum):
|
||||||
@ -91,36 +91,52 @@ class MihomoAPI:
|
|||||||
case _:
|
case _:
|
||||||
raise HttpRequestError(response.status, str(response.reason))
|
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.
|
Fetches user data from the API.
|
||||||
|
|
||||||
Args:
|
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:
|
Returns:
|
||||||
StarrailInfoParsed: The parsed user data from mihomo API.
|
StarrailInfoParsed: The parsed user data from mihomo API.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
data = await self.request(uid, self.lang)
|
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)
|
data = StarrailInfoParsed.parse_obj(data)
|
||||||
return 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:
|
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:
|
Returns:
|
||||||
StarrailInfoParsedV1: The parsed user data from the Mihomo API (version 1).
|
StarrailInfoParsedV1: The parsed user data from the Mihomo API (version 1).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
data = await self.request(uid, self.lang, params={"version": "v1"})
|
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 = StarrailInfoParsedV1.parse_obj(data)
|
||||||
data = replace_trailblazer_name(data)
|
data = tools.replace_trailblazer_name(data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get_icon_url(self, icon: str) -> str:
|
def get_icon_url(self, icon: str) -> str:
|
||||||
|
@ -1,21 +1,23 @@
|
|||||||
from typing import TypeVar
|
from typing import Final, TypeVar
|
||||||
|
|
||||||
from .models import Character, StarrailInfoParsed
|
from .models import Character, StarrailInfoParsed
|
||||||
from .models.v1 import Character, StarrailInfoParsedV1
|
from .models.v1 import Character, StarrailInfoParsedV1
|
||||||
|
|
||||||
T = TypeVar("T")
|
RawData = TypeVar("RawData")
|
||||||
ParsedData = TypeVar("ParsedData", StarrailInfoParsed, StarrailInfoParsedV1)
|
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.
|
Recursively removes empty dictionaries from the given raw data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- data (`T`): The input data.
|
- data (`RawData`): The input raw data.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
- `T`: The data with empty dictionaries removed.
|
- `RawData`: The data with empty dictionaries removed.
|
||||||
"""
|
"""
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
for key in data.keys():
|
for key in data.keys():
|
||||||
@ -26,6 +28,31 @@ def remove_empty_dict(data: T) -> T:
|
|||||||
return data
|
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:
|
def replace_trailblazer_name(data: StarrailInfoParsedV1) -> StarrailInfoParsedV1:
|
||||||
"""
|
"""
|
||||||
Replaces the trailblazer name with the player's name.
|
Replaces the trailblazer name with the player's name.
|
||||||
|
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "mihomo"
|
name = "mihomo"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="KT", email="xns77477@gmail.com" },
|
{ name="KT", email="xns77477@gmail.com" },
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user