diff --git a/genshinuid_gachalog/export_and_import.py b/genshinuid_gachalog/export_and_import.py new file mode 100644 index 00000000..7eced7f8 --- /dev/null +++ b/genshinuid_gachalog/export_and_import.py @@ -0,0 +1,62 @@ +import json +from pathlib import Path +from datetime import datetime + +from .get_gachalogs import save_gachalogs + +PLAYER_PATH = Path(__file__).parents[1] / 'player' +INT_TO_TYPE = { + '100': '新手祈愿', + '200': '常驻祈愿', + '301': '角色祈愿', + '400': '角色祈愿', + '302': '武器祈愿', +} + + +async def import_gachalogs(history_data: dict, uid: int): + raw_data = history_data['list'] + result = {'新手祈愿': [], '常驻祈愿': [], '角色祈愿': [], '武器祈愿': []} + for item in raw_data: + result[INT_TO_TYPE['gacha_type']].append(item) + im = await save_gachalogs(str(uid), result) + return im + + +async def export_gachalogs(uid: int): + path = PLAYER_PATH / str(uid) + if not path.exists(): + path.mkdir(parents=True, exist_ok=True) + + # 获取当前时间 + now = datetime.now() + current_time = now.strftime('%Y-%m-%d %H:%M:%S') + + # 抽卡记录json路径 + gachalogs_path = path / 'gacha_logs.json' + if gachalogs_path.exists(): + with open(gachalogs_path, "r", encoding='UTF-8') as f: + raw_data = json.load(f) + result = { + 'info': { + 'uid': str(uid), + 'lang': 'zh-cn', + 'export_time': current_time, + 'export_app': 'GenshinUID', + 'export_app_version': '3.1', + 'uigf_version': '2.1', + }, + 'list': [], + } + for i in ['新手祈愿', '常驻祈愿', '角色祈愿', '武器祈愿']: + for item in raw_data['data'][i]: + item['uigf_gacha_type'] = item['gacha_type'] + result['list'].append(item) + # 保存文件 + with open(path / f'UIGF_{str(uid)}', 'w', encoding='UTF-8') as file: + json.dump(result, file, ensure_ascii=False) + im = '导出成功!' + else: + im = '你还没有抽卡记录可以导出!' + + return im diff --git a/genshinuid_gachalog/get_gachalogs.py b/genshinuid_gachalog/get_gachalogs.py index 57edc92a..381cdfd0 100644 --- a/genshinuid_gachalog/get_gachalogs.py +++ b/genshinuid_gachalog/get_gachalogs.py @@ -1,5 +1,6 @@ import json from pathlib import Path +from typing import Optional from datetime import datetime from ..utils.mhy_api.get_mhy_data import get_gacha_log_by_authkey @@ -7,9 +8,10 @@ from ..utils.mhy_api.get_mhy_data import get_gacha_log_by_authkey PLAYER_PATH = Path(__file__).parents[1] / 'player' -async def save_gachalogs(uid: str): +async def save_gachalogs(uid: str, raw_data: Optional[dict] = None): path = PLAYER_PATH / str(uid) - path.mkdir(parents=True, exist_ok=True) + if not path.exists(): + path.mkdir(parents=True, exist_ok=True) # 获取当前时间 now = datetime.now() @@ -33,7 +35,17 @@ async def save_gachalogs(uid: str): old_weapon_gacha_num = len(gachalogs_history['武器祈愿']) # 获取新抽卡记录 - raw_data = await get_gacha_log_by_authkey(uid, gachalogs_history) + if raw_data is None: + raw_data = await get_gacha_log_by_authkey(uid, gachalogs_history) + else: + new_data = {'新手祈愿': [], '常驻祈愿': [], '角色祈愿': [], '武器祈愿': []} + if gachalogs_history: + for i in ['新手祈愿', '常驻祈愿', '角色祈愿', '武器祈愿']: + for item in raw_data[i]: + if item not in gachalogs_history[i]: + new_data[i].append(item) + raw_data = new_data + if raw_data == {}: return '你还没有绑定过Stoken噢~' if not raw_data: