mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-16 03:55:26 +00:00
✨ Add Refresh History Job
This commit is contained in:
parent
b61a87d0f0
commit
ae775d8d6b
163
plugins/jobs/refresh_history.py
Normal file
163
plugins/jobs/refresh_history.py
Normal file
@ -0,0 +1,163 @@
|
||||
import datetime
|
||||
from asyncio import sleep
|
||||
from typing import TYPE_CHECKING, Dict
|
||||
|
||||
from simnet.errors import (
|
||||
TimedOut as SimnetTimedOut,
|
||||
BadRequest as SimnetBadRequest,
|
||||
InvalidCookies,
|
||||
)
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.error import BadRequest, Forbidden
|
||||
|
||||
from core.plugin import Plugin, job
|
||||
from core.services.history_data.services import (
|
||||
HistoryDataAbyssServices,
|
||||
HistoryDataLedgerServices,
|
||||
HistoryDataChallengeStoryServices,
|
||||
)
|
||||
from gram_core.basemodel import RegionEnum
|
||||
from gram_core.plugin import handler
|
||||
from gram_core.services.cookies import CookiesService
|
||||
from gram_core.services.cookies.models import CookiesStatusEnum
|
||||
from plugins.starrail.challenge import ChallengePlugin
|
||||
from plugins.starrail.challenge_story import ChallengeStoryPlugin
|
||||
from plugins.starrail.ledger import LedgerPlugin
|
||||
from plugins.tools.genshin import GenshinHelper, PlayerNotFoundError, CookiesNotFoundError
|
||||
from utils.log import logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
from simnet import StarRailClient
|
||||
|
||||
REGION = [RegionEnum.HYPERION, RegionEnum.HOYOLAB]
|
||||
NOTICE_TEXT = """#### %s更新 ####
|
||||
时间:%s (UTC+8)
|
||||
UID: %s
|
||||
结果: 新的%s已保存,可通过命令回顾"""
|
||||
|
||||
|
||||
class RefreshHistoryJob(Plugin):
|
||||
"""历史记录定时刷新"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
cookies: CookiesService,
|
||||
genshin_helper: GenshinHelper,
|
||||
history_abyss: HistoryDataAbyssServices,
|
||||
history_data_abyss_story: HistoryDataChallengeStoryServices,
|
||||
history_ledger: HistoryDataLedgerServices,
|
||||
):
|
||||
self.cookies = cookies
|
||||
self.genshin_helper = genshin_helper
|
||||
self.history_data_abyss = history_abyss
|
||||
self.history_data_abyss_story = history_data_abyss_story
|
||||
self.history_data_ledger = history_ledger
|
||||
|
||||
@staticmethod
|
||||
async def send_notice(context: "ContextTypes.DEFAULT_TYPE", user_id: int, notice_text: str):
|
||||
try:
|
||||
await context.bot.send_message(user_id, notice_text, parse_mode=ParseMode.HTML)
|
||||
except (BadRequest, Forbidden) as exc:
|
||||
logger.error("执行自动刷新历史记录时发生错误 user_id[%s] Message[%s]", user_id, exc.message)
|
||||
except Exception as exc:
|
||||
logger.error("执行自动刷新历史记录时发生错误 user_id[%s]", user_id, exc_info=exc)
|
||||
|
||||
async def save_abyss_data(self, client: "StarRailClient") -> bool:
|
||||
uid = client.player_id
|
||||
abyss_data = await client.get_starrail_challenge(uid, previous=False, lang="zh-cn")
|
||||
if abyss_data.has_data:
|
||||
return await ChallengePlugin.save_abyss_data(self.history_data_abyss, uid, abyss_data)
|
||||
return False
|
||||
|
||||
async def send_abyss_notice(self, context: "ContextTypes.DEFAULT_TYPE", user_id: int, uid: int):
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
notice_text = NOTICE_TEXT % ("混沌回忆历史记录", now, uid, "挑战记录")
|
||||
await self.send_notice(context, user_id, notice_text)
|
||||
|
||||
async def save_abyss_story_data(self, client: "StarRailClient") -> bool:
|
||||
uid = client.player_id
|
||||
abyss_data = await client.get_starrail_challenge_story(uid, previous=False, lang="zh-cn")
|
||||
if abyss_data.has_data and abyss_data.groups:
|
||||
group = abyss_data.groups[0]
|
||||
return await ChallengeStoryPlugin.save_abyss_data(self.history_data_abyss_story, uid, abyss_data, group)
|
||||
return False
|
||||
|
||||
async def send_abyss_story_notice(self, context: "ContextTypes.DEFAULT_TYPE", user_id: int, uid: int):
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
notice_text = NOTICE_TEXT % ("虚构叙事历史记录", now, uid, "挑战记录")
|
||||
await self.send_notice(context, user_id, notice_text)
|
||||
|
||||
async def _save_ledger_data(self, client: "StarRailClient", year: int, month: int) -> bool:
|
||||
req_month = f"{year}0{month}" if month < 10 else f"{year}{month}"
|
||||
diary_info = await client.get_starrail_diary(client.player_id, month=req_month)
|
||||
return await LedgerPlugin.save_ledger_data(self.history_data_ledger, client.player_id, diary_info)
|
||||
|
||||
@staticmethod
|
||||
def get_ledger_months() -> Dict[int, int]:
|
||||
now = datetime.datetime.now()
|
||||
now_time = (now - datetime.timedelta(days=1)) if now.day == 1 and now.hour <= 4 else now
|
||||
months = {}
|
||||
last_month = now_time.replace(day=1) - datetime.timedelta(days=1)
|
||||
months[last_month.month] = last_month.year
|
||||
|
||||
last_month = last_month.replace(day=1) - datetime.timedelta(days=1)
|
||||
months[last_month.month] = last_month.year
|
||||
return months
|
||||
|
||||
async def save_ledger_data(self, client: "StarRailClient") -> bool:
|
||||
months = self.get_ledger_months()
|
||||
ok = False
|
||||
for month, year in months.items():
|
||||
if await self._save_ledger_data(client, year, month):
|
||||
ok = True
|
||||
return ok
|
||||
|
||||
async def send_ledger_notice(self, context: "ContextTypes.DEFAULT_TYPE", user_id: int, uid: int):
|
||||
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
notice_text = NOTICE_TEXT % ("旅行札记历史记录", now, uid, "旅行札记历史记录")
|
||||
await self.send_notice(context, user_id, notice_text)
|
||||
|
||||
@handler.command(command="refresh_all_history", block=False, admin=True)
|
||||
async def refresh_all_history(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
|
||||
user = update.effective_user
|
||||
logger.info("用户 %s[%s] refresh_all_history 命令请求", user.full_name, user.id)
|
||||
message = update.effective_message
|
||||
reply = await message.reply_text("正在执行刷新历史记录任务,请稍后...")
|
||||
await self.daily_refresh_history(context)
|
||||
await reply.edit_text("全部账号刷新历史记录任务完成")
|
||||
|
||||
@job.run_daily(time=datetime.time(hour=6, minute=1, second=0), name="RefreshHistoryJob")
|
||||
async def daily_refresh_history(self, context: "ContextTypes.DEFAULT_TYPE"):
|
||||
logger.info("正在执行每日刷新历史记录任务")
|
||||
for database_region in REGION:
|
||||
for cookie_model in await self.cookies.get_all(
|
||||
region=database_region, status=CookiesStatusEnum.STATUS_SUCCESS
|
||||
):
|
||||
user_id = cookie_model.user_id
|
||||
try:
|
||||
async with self.genshin_helper.genshin(user_id) as client:
|
||||
if await self.save_abyss_data(client):
|
||||
await self.send_abyss_notice(context, user_id, client.player_id)
|
||||
if await self.save_abyss_story_data(client):
|
||||
await self.send_abyss_story_notice(context, user_id, client.player_id)
|
||||
if await self.save_ledger_data(client):
|
||||
await self.send_ledger_notice(context, user_id, client.player_id)
|
||||
except (InvalidCookies, PlayerNotFoundError, CookiesNotFoundError):
|
||||
continue
|
||||
except SimnetBadRequest as exc:
|
||||
logger.warning(
|
||||
"用户 user_id[%s] 请求历史记录失败 [%s]%s", user_id, exc.ret_code, exc.original or exc.message
|
||||
)
|
||||
continue
|
||||
except SimnetTimedOut:
|
||||
logger.info("用户 user_id[%s] 请求历史记录超时", user_id)
|
||||
continue
|
||||
except Exception as exc:
|
||||
logger.error("执行自动刷新历史记录时发生错误 user_id[%s]", user_id, exc_info=exc)
|
||||
continue
|
||||
await sleep(1)
|
||||
|
||||
logger.success("执行每日刷新历史记录任务完成")
|
@ -218,7 +218,7 @@ class ChallengePlugin(Plugin):
|
||||
async def get_rendered_pic_data(self, client: "StarRailClient", uid: int, previous: bool) -> "StarRailChallenge":
|
||||
abyss_data = await client.get_starrail_challenge(uid, previous=previous, lang="zh-cn")
|
||||
if abyss_data.has_data:
|
||||
await self.save_abyss_data(uid, abyss_data)
|
||||
await self.save_abyss_data(self.history_data_abyss, uid, abyss_data)
|
||||
return abyss_data
|
||||
|
||||
async def get_rendered_pic( # skipcq: PY-R1000 #
|
||||
@ -330,12 +330,17 @@ class ChallengePlugin(Plugin):
|
||||
)
|
||||
]
|
||||
|
||||
async def save_abyss_data(self, uid: int, abyss_data: "StarRailChallenge"):
|
||||
model = self.history_data_abyss.create(uid, abyss_data)
|
||||
old_data = await self.history_data_abyss.get_by_user_id_data_id(uid, model.data_id)
|
||||
exists = self.history_data_abyss.exists_data(model, old_data)
|
||||
@staticmethod
|
||||
async def save_abyss_data(
|
||||
history_data_abyss: "HistoryDataAbyssServices", uid: int, abyss_data: "StarRailChallenge"
|
||||
) -> bool:
|
||||
model = history_data_abyss.create(uid, abyss_data)
|
||||
old_data = await history_data_abyss.get_by_user_id_data_id(uid, model.data_id)
|
||||
exists = history_data_abyss.exists_data(model, old_data)
|
||||
if not exists:
|
||||
await self.history_data_abyss.add(model)
|
||||
await history_data_abyss.add(model)
|
||||
return True
|
||||
return False
|
||||
|
||||
async def get_abyss_data(self, uid: int):
|
||||
return await self.history_data_abyss.get_by_user_id(uid)
|
||||
|
@ -221,7 +221,7 @@ class ChallengeStoryPlugin(Plugin):
|
||||
group = None
|
||||
if abyss_data.has_data and abyss_data.groups:
|
||||
group = abyss_data.groups[1] if previous else abyss_data.groups[0]
|
||||
await self.save_abyss_data(uid, abyss_data, group)
|
||||
await self.save_abyss_data(self.history_data_abyss, uid, abyss_data, group)
|
||||
return abyss_data, group
|
||||
|
||||
async def get_rendered_pic(
|
||||
@ -343,14 +343,20 @@ class ChallengeStoryPlugin(Plugin):
|
||||
)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
async def save_abyss_data(
|
||||
self, uid: int, abyss_data: "StarRailChallengeStory", group: "StarRailChallengeStoryGroup"
|
||||
):
|
||||
model = self.history_data_abyss.create(uid, abyss_data, group)
|
||||
old_data = await self.history_data_abyss.get_by_user_id_data_id(uid, model.data_id)
|
||||
exists = self.history_data_abyss.exists_data(model, old_data)
|
||||
history_data_abyss: "HistoryDataChallengeStoryServices",
|
||||
uid: int,
|
||||
abyss_data: "StarRailChallengeStory",
|
||||
group: "StarRailChallengeStoryGroup",
|
||||
) -> bool:
|
||||
model = history_data_abyss.create(uid, abyss_data, group)
|
||||
old_data = await history_data_abyss.get_by_user_id_data_id(uid, model.data_id)
|
||||
exists = history_data_abyss.exists_data(model, old_data)
|
||||
if not exists:
|
||||
await self.history_data_abyss.add(model)
|
||||
await history_data_abyss.add(model)
|
||||
return True
|
||||
return False
|
||||
|
||||
async def get_abyss_data(self, uid: int):
|
||||
return await self.history_data_abyss.get_by_user_id(uid)
|
||||
|
@ -53,7 +53,7 @@ class LedgerPlugin(Plugin):
|
||||
async def _start_get_ledger(self, client: "StarRailClient", year, month) -> RenderResult:
|
||||
req_month = f"{year}0{month}" if month < 10 else f"{year}{month}"
|
||||
diary_info: StarRailDiary = await client.get_starrail_diary(client.player_id, month=req_month)
|
||||
await self.save_ledger_data(client.player_id, diary_info)
|
||||
await self.save_ledger_data(self.history_data_ledger, client.player_id, diary_info)
|
||||
return await self._start_get_ledger_render(client.player_id, diary_info)
|
||||
|
||||
async def _start_get_ledger_render(self, uid: int, diary_info: "StarRailDiary") -> RenderResult:
|
||||
@ -148,13 +148,18 @@ class LedgerPlugin(Plugin):
|
||||
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
||||
await render_result.reply_photo(message, filename=f"{client.player_id}.png", allow_sending_without_reply=True)
|
||||
|
||||
async def save_ledger_data(self, uid: int, ledger_data: "StarRailDiary"):
|
||||
@staticmethod
|
||||
async def save_ledger_data(
|
||||
history_data_ledger: "HistoryDataLedgerServices", uid: int, ledger_data: "StarRailDiary"
|
||||
) -> bool:
|
||||
if int(ledger_data.current_month) == ledger_data.month:
|
||||
return
|
||||
model = self.history_data_ledger.create(uid, ledger_data)
|
||||
old_data = await self.history_data_ledger.get_by_user_id_data_id(uid, model.data_id)
|
||||
return False
|
||||
model = history_data_ledger.create(uid, ledger_data)
|
||||
old_data = await history_data_ledger.get_by_user_id_data_id(uid, model.data_id)
|
||||
if not old_data:
|
||||
await self.history_data_ledger.add(model)
|
||||
await history_data_ledger.add(model)
|
||||
return True
|
||||
return False
|
||||
|
||||
async def get_ledger_data(self, uid: int):
|
||||
return await self.history_data_ledger.get_by_user_id(uid)
|
||||
|
Loading…
Reference in New Issue
Block a user