mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 03:32:33 +08:00
feat: Add data merging functionality to the tools
Added the following functions: - remove_duplicate_character - merge_character_data
This commit is contained in:
parent
dcd09ee97e
commit
c433b5d845
|
@ -1,11 +1,20 @@
|
||||||
from typing import TypeVar
|
from typing import TypeVar
|
||||||
|
|
||||||
from .models import StarrailInfoParsed
|
from .models import Character, StarrailInfoParsed
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
def remove_empty_dict(data: T) -> T:
|
def remove_empty_dict(data: T) -> T:
|
||||||
|
"""
|
||||||
|
Recursively removes empty dictionaries from the given raw data.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- data (`T`): The input data.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- `T`: The data with empty dictionaries removed.
|
||||||
|
"""
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
for key in data.keys():
|
for key in data.keys():
|
||||||
data[key] = None if (data[key] == {}) else remove_empty_dict(data[key])
|
data[key] = None if (data[key] == {}) else remove_empty_dict(data[key])
|
||||||
|
@ -16,7 +25,56 @@ def remove_empty_dict(data: T) -> T:
|
||||||
|
|
||||||
|
|
||||||
def replace_trailblazer_name(data: StarrailInfoParsed) -> StarrailInfoParsed:
|
def replace_trailblazer_name(data: StarrailInfoParsed) -> StarrailInfoParsed:
|
||||||
|
"""
|
||||||
|
Replaces the trailblazer name with the player's name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- data (`StarrailInfoParsed`): The input StarrailInfoParsed data.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- `StarrailInfoParsed`: The updated StarrailInfoParsed data.
|
||||||
|
"""
|
||||||
for i in range(len(data.characters)):
|
for i in range(len(data.characters)):
|
||||||
if data.characters[i].name == r"{NICKNAME}":
|
if data.characters[i].name == r"{NICKNAME}":
|
||||||
data.characters[i].name = data.player.name
|
data.characters[i].name = data.player.name
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def remove_duplicate_character(data: StarrailInfoParsed) -> StarrailInfoParsed:
|
||||||
|
"""
|
||||||
|
Removes duplicate characters from the given StarrailInfoParsed data.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- data (`StarrailInfoParsed`): The input StarrailInfoParsed data.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- `StarrailInfoParsed`: The updated StarrailInfoParsed data without duplicate characters.
|
||||||
|
"""
|
||||||
|
new_characters: list[Character] = []
|
||||||
|
characters_ids: set[str] = set()
|
||||||
|
for character in data.characters:
|
||||||
|
if character.id not in characters_ids:
|
||||||
|
new_characters.append(character)
|
||||||
|
characters_ids.add(character.id)
|
||||||
|
data.characters = new_characters
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def merge_character_data(
|
||||||
|
new_data: StarrailInfoParsed, old_data: StarrailInfoParsed
|
||||||
|
) -> StarrailInfoParsed:
|
||||||
|
"""
|
||||||
|
Append the old data characters to the list of new data characters.
|
||||||
|
The player's info from the old data will be omitted/discarded.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- new_data (`StarrailInfoParsed`): The new data to be merged.
|
||||||
|
- old_data (`StarrailInfoParsed`): The old data to merge into.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- `StarrailInfoParsed`: The merged new data.
|
||||||
|
"""
|
||||||
|
for character in old_data.characters:
|
||||||
|
new_data.characters.append(character)
|
||||||
|
new_data = remove_duplicate_character(new_data)
|
||||||
|
return new_data
|
||||||
|
|
Loading…
Reference in New Issue
Block a user