mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 03:32:33 +08:00
docs: add examples and update README.md
This commit is contained in:
parent
2894966b6c
commit
8dcd3b62be
50
README.md
50
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())
|
||||
```
|
||||
```
|
||||
|
||||
### 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))
|
||||
```
|
||||
|
|
27
examples/basic.py
Normal file
27
examples/basic.py
Normal file
|
@ -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())
|
25
examples/data_persistence.py
Normal file
25
examples/data_persistence.py
Normal file
|
@ -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())
|
19
examples/merge_data.py
Normal file
19
examples/merge_data.py
Normal file
|
@ -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())
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user