From 8dcd3b62be4b1087346ac64794ae51089fd92acf Mon Sep 17 00:00:00 2001 From: KT Date: Sat, 3 Jun 2023 11:14:58 +0800 Subject: [PATCH] docs: add examples and update README.md --- README.md | 50 +++++++++++++++++++++++++++++++++--- examples/basic.py | 27 +++++++++++++++++++ examples/data_persistence.py | 25 ++++++++++++++++++ examples/merge_data.py | 19 ++++++++++++++ mihomo/client.py | 5 +++- 5 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 examples/basic.py create mode 100644 examples/data_persistence.py create mode 100644 examples/merge_data.py diff --git a/README.md b/README.md index 5d3b8c21..77345dc0 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ pip install -U git+https://github.com/KT-Yeh/mihomo.git ``` ## Usage + +### Basic An example for https://api.mihomo.me/sr_info_parsed/800333171?lang=en ```py @@ -30,11 +32,53 @@ async def main(): for character in data.characters: print("-----------") print(f"Name: {character.name}") - print(f"rarity: {character.rarity}") + print(f"Rarity: {character.rarity}") print(f"Level: {character.level}") print(f"Avatar url: {client.get_icon_url(character.icon)}") print(f"Preview url: {client.get_icon_url(character.preview)}") - print(f"portrait url: {client.get_icon_url(character.portrait)}") + print(f"Portrait url: {client.get_icon_url(character.portrait)}") asyncio.run(main()) -``` \ No newline at end of file +``` + +### Tools +`from mihomo import tools` +#### Remove Duplicate Character +```py + data = await client.fetch_user(800333171) + data = tools.remove_duplicate_character(data) +``` + +#### Merge Character Data +```py + old_data = await client.fetch_user(800333171) + + # Change characters in game and wait for the API to refresh + # ... + + new_data = await client.fetch_user(800333171) + data = tools.merge_character_data(new_data, old_data) +``` + +### Data Persistence +Take pickle and json as an example +```py +import pickle +import zlib +from mihomo import MihomoAPI, Language, StarrailInfoParsed + +client = MihomoAPI(language=Language.EN) +data = await client.fetch_user(800333171) + +# Save +pickle_data = zlib.compress(pickle.dumps(data)) +print(len(pickle_data)) +json_data = data.json(by_alias=True, ensure_ascii=False) +print(len(json_data)) + +# Load +data_from_pickle = pickle.loads(zlib.decompress(pickle_data)) +data_from_json = StarrailInfoParsed.parse_raw(json_data) +print(type(data_from_pickle)) +print(type(data_from_json)) +``` diff --git a/examples/basic.py b/examples/basic.py new file mode 100644 index 00000000..04bae385 --- /dev/null +++ b/examples/basic.py @@ -0,0 +1,27 @@ +import asyncio + +from mihomo import Language, MihomoAPI + +client = MihomoAPI(language=Language.EN) + + +async def main(): + data = await client.fetch_user(800333171) + + print(f"Name: {data.player.name}") + print(f"Level: {data.player.level}") + print(f"Signature: {data.player.signature}") + print(f"Achievements: {data.player_details.achievements}") + print(f"Characters count: {data.player_details.characters}") + print(f"Profile picture url: {client.get_icon_url(data.player.icon)}") + for character in data.characters: + print("-----------") + print(f"Name: {character.name}") + print(f"Rarity: {character.rarity}") + print(f"Level: {character.level}") + print(f"Avatar url: {client.get_icon_url(character.icon)}") + print(f"Preview url: {client.get_icon_url(character.preview)}") + print(f"Portrait url: {client.get_icon_url(character.portrait)}") + + +asyncio.run(main()) diff --git a/examples/data_persistence.py b/examples/data_persistence.py new file mode 100644 index 00000000..7eb933b0 --- /dev/null +++ b/examples/data_persistence.py @@ -0,0 +1,25 @@ +import asyncio +import pickle +import zlib + +from mihomo import Language, MihomoAPI, StarrailInfoParsed + + +async def main(): + client = MihomoAPI(language=Language.EN) + data = await client.fetch_user(800333171) + + # Save + pickle_data = zlib.compress(pickle.dumps(data)) + print(len(pickle_data)) + json_data = data.json(by_alias=True, ensure_ascii=False) + print(len(json_data)) + + # Load + data_from_pickle = pickle.loads(zlib.decompress(pickle_data)) + data_from_json = StarrailInfoParsed.parse_raw(json_data) + print(type(data_from_pickle)) + print(type(data_from_json)) + + +asyncio.run(main()) diff --git a/examples/merge_data.py b/examples/merge_data.py new file mode 100644 index 00000000..c30d8b6b --- /dev/null +++ b/examples/merge_data.py @@ -0,0 +1,19 @@ +import asyncio + +from mihomo import Language, MihomoAPI, tools + + +async def main(): + client = MihomoAPI(language=Language.EN) + old_data = await client.fetch_user(800333171) + + # Change characters in game and wait for the API to refresh + # ... + + new_data = await client.fetch_user(800333171) + data = tools.merge_character_data(new_data, old_data) + + print(data) + + +asyncio.run(main()) diff --git a/mihomo/client.py b/mihomo/client.py index 0d5d6db5..1243f1cf 100644 --- a/mihomo/client.py +++ b/mihomo/client.py @@ -10,6 +10,7 @@ from .tools import remove_empty_dict, replace_trailblazer_name class Language(Enum): CHT = "cht" + CHS = "chs" DE = "de" EN = "en" ES = "es" @@ -63,7 +64,9 @@ class MihomoAPI: """ url = self.BASE_URL + "/" + str(uid) - params = {"lang": language.value} + params = {} + if language != Language.CHS: + params.update({"lang": language.value}) async with aiohttp.ClientSession() as session: async with session.get(url, params=params) as response: if response.status == 200: