diff --git a/GenshinUID/genshinuid_gachalog/__init__.py b/GenshinUID/genshinuid_gachalog/__init__.py index 2af36e37..6afaac48 100644 --- a/GenshinUID/genshinuid_gachalog/__init__.py +++ b/GenshinUID/genshinuid_gachalog/__init__.py @@ -1,14 +1,15 @@ from gsuid_core.sv import SV from gsuid_core.bot import Bot from gsuid_core.models import Event +from gsuid_core.logger import logger from gsuid_core.message_models import Button from gsuid_core.segment import MessageSegment from gsuid_core.utils.error_reply import UID_HINT from gsuid_core.utils.database.models import GsBind from ..utils.convert import get_uid -from .get_gachalogs import save_gachalogs from .draw_gachalogs import draw_gachalogs_img +from .get_gachalogs import save_gachalogs, get_full_gachalog from .export_and_import import export_gachalogs, import_gachalogs from .lelaer_tools import ( get_gachaurl, @@ -41,7 +42,7 @@ async def send_import_gacha_info(bot: Bot, ev: Event): @sv_gacha_log.on_fullmatch(('抽卡记录')) async def send_gacha_log_card_info(bot: Bot, ev: Event): - await bot.logger.info('开始执行[抽卡记录]') + logger.info('开始执行[抽卡记录]') uid = await get_uid(bot, ev) if uid is None: return await bot.send(UID_HINT) @@ -54,7 +55,7 @@ async def send_gacha_log_card_info(bot: Bot, ev: Event): @sv_refresh_gacha_log.on_fullmatch(('刷新抽卡记录', '强制刷新抽卡记录')) async def send_refresh_gacha_info(bot: Bot, ev: Event): - await bot.logger.info('开始执行[刷新抽卡记录]') + logger.info('开始执行[刷新抽卡记录]') uid = await get_uid(bot, ev) if uid is None: return await bot.send(UID_HINT) @@ -69,9 +70,22 @@ async def send_refresh_gacha_info(bot: Bot, ev: Event): await bot.send_option(im, [Button('🃏抽卡记录', '抽卡记录')]) +@sv_refresh_gacha_log.on_fullmatch(('全量刷新抽卡记录')) +async def send_full_refresh_gacha_info(bot: Bot, ev: Event): + logger.info('开始执行[全量刷新抽卡记录]') + uid = await get_uid(bot, ev) + if uid is None: + return await bot.send(UID_HINT) + await bot.send( + f'UID{uid}开始执行[全量刷新抽卡记录],需要一定时间...请勿重复触发!' + ) + im = await get_full_gachalog(uid) + return await bot.send_option(im, [Button('🃏抽卡记录', '抽卡记录')]) + + @sv_export_gacha_log.on_fullmatch(('导出抽卡记录')) async def send_export_gacha_info(bot: Bot, ev: Event): - await bot.logger.info('开始执行[导出抽卡记录]') + logger.info('开始执行[导出抽卡记录]') uid = await GsBind.get_uid_by_game(ev.user_id, ev.bot_id) if uid is None: return await bot.send(UID_HINT) @@ -103,7 +117,7 @@ async def send_export_gacha_info(bot: Bot, ev: Event): @sv_import_lelaer_gachalog.on_fullmatch(('从小助手导入抽卡记录')) async def import_lelaer_gachalog(bot: Bot, ev: Event): - await bot.logger.info('开始执行[从小助手导入抽卡记录]') + logger.info('开始执行[从小助手导入抽卡记录]') uid = await GsBind.get_uid_by_game(ev.user_id, ev.bot_id) if uid is None: return await bot.send(UID_HINT) @@ -113,7 +127,7 @@ async def import_lelaer_gachalog(bot: Bot, ev: Event): @sv_export_lelaer_gachalog.on_fullmatch(('导出抽卡记录到小助手')) async def export_to_lelaer_gachalog(bot: Bot, ev: Event): - await bot.logger.info('开始执行[导出抽卡记录到小助手]') + logger.info('开始执行[导出抽卡记录到小助手]') uid = await GsBind.get_uid_by_game(ev.user_id, ev.bot_id) if uid is None: return await bot.send(UID_HINT) @@ -123,7 +137,7 @@ async def export_to_lelaer_gachalog(bot: Bot, ev: Event): @sv_export_gachalogurl.on_fullmatch(('导出抽卡记录链接', '导出抽卡记录连接')) async def export_gachalogurl(bot: Bot, ev: Event): - await bot.logger.info('开始执行[导出抽卡记录链接]') + logger.info('开始执行[导出抽卡记录链接]') uid = await GsBind.get_uid_by_game(ev.user_id, ev.bot_id) if uid is None: return await bot.send(UID_HINT) diff --git a/GenshinUID/genshinuid_gachalog/get_gachalogs.py b/GenshinUID/genshinuid_gachalog/get_gachalogs.py index 3311c38d..12ef6219 100644 --- a/GenshinUID/genshinuid_gachalog/get_gachalogs.py +++ b/GenshinUID/genshinuid_gachalog/get_gachalogs.py @@ -1,9 +1,12 @@ import json +import shutil import asyncio from copy import deepcopy -from datetime import datetime from typing import Dict, Optional +from datetime import datetime, timedelta +import aiofiles +from gsuid_core.logger import logger from gsuid_core.utils.error_reply import SK_HINT from ..utils.mys_api import mys_api @@ -33,6 +36,70 @@ '集录祈愿': ['500'], } +full_lock = [] +lock = [] + + +async def get_full_gachalog(uid: str): + if uid in full_lock: + return '当前正在全量刷新抽卡记录中, 请勿重试!请稍后再试...!' + + full_lock.append(uid) + 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(): + gacha_log_backup_path = path / f'gacha_logs_{current_time}.json' + shutil.copy(gachalogs_path, gacha_log_backup_path) + logger.info( + f'[全量刷新抽卡记录] 已备份抽卡记录到{gacha_log_backup_path}' + ) + async with aiofiles.open(gachalogs_path, "r", encoding='UTF-8') as f: + gachalogs_history: Dict = json.loads(await f.read()) + gachalogs_history = remove_gachalog(gachalogs_history) + async with aiofiles.open(gachalogs_path, "w", encoding='UTF-8') as f: + await f.write( + json.dumps( + gachalogs_history, + ensure_ascii=False, + ) + ) + im = await save_gachalogs(uid, None) + else: + im = '你还没有已缓存的抽卡记录, 请使用刷新抽卡记录!' + full_lock.remove(uid) + return im + + +def remove_gachalog(gachalog: Dict, month: int = 5): + now = datetime.now() + threshold = now - timedelta(days=month * 30) + + map_num = { + '新手祈愿': 'new_gacha_num', + '常驻祈愿': 'normal_gacha_num', + '角色祈愿': 'char_gacha_num', + '武器祈愿': 'weapon_gacha_num', + '集录祈愿': 'mix_gacha_num', + } + for gacha_name in map_num: + gachanum_name = map_num[gacha_name] + gachalog['data'][gacha_name] = [ + item + for item in gachalog['data'][gacha_name] + if datetime.strptime(item["time"], "%Y-%m-%d %H:%M:%S") + <= threshold + ] + gachalog[gachanum_name] = len(gachalog['data'][gacha_name]) + + return gachalog + async def get_new_gachalog(uid: str, full_data: Dict, is_force: bool): temp = [] @@ -41,7 +108,10 @@ async def get_new_gachalog(uid: str, full_data: Dict, is_force: bool): end_id = '0' for page in range(1, 999): data = await mys_api.get_gacha_log_by_authkey( - uid, gacha_type, page, end_id + uid, + gacha_type, + page, + end_id, ) await asyncio.sleep(0.9) if isinstance(data, int): @@ -77,6 +147,9 @@ async def get_new_gachalog(uid: str, full_data: Dict, is_force: bool): async def save_gachalogs( uid: str, raw_data: Optional[dict] = None, is_force: bool = False ) -> str: + if uid in lock: + return '当前正在刷新抽卡记录中, 请勿重试!请稍后再试...!' + lock.append(uid) path = PLAYER_PATH / str(uid) if not path.exists(): path.mkdir(parents=True, exist_ok=True) @@ -103,8 +176,8 @@ async def save_gachalogs( ) = (0, 0, 0, 0, 0) if gachalogs_path.exists(): - with open(gachalogs_path, "r", encoding='UTF-8') as f: - gachalogs_history: Dict = json.load(f) + async with aiofiles.open(gachalogs_path, "r", encoding='UTF-8') as f: + gachalogs_history: Dict = json.loads(await f.read()) gachalogs_history = gachalogs_history['data'] old_normal_gacha_num = len(gachalogs_history['常驻祈愿']) old_char_gacha_num = len(gachalogs_history['角色祈愿']) @@ -140,6 +213,7 @@ async def save_gachalogs( raw_data[i].extend(gachalogs_history[i]) if raw_data == {} or not raw_data: + lock.remove(uid) return SK_HINT if '集录祈愿' not in raw_data: @@ -190,4 +264,5 @@ async def save_gachalogs( ) if new_add > 0: im += f'\n新手祈愿{new_add}个!' + lock.remove(uid) return im