2022-07-31 08:00:52 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from datetime import datetime, timedelta
|
2023-07-18 09:29:31 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2022-07-31 08:00:52 +00:00
|
|
|
|
2023-08-21 14:42:44 +00:00
|
|
|
from simnet.errors import DataNotPublic, BadRequest as SimnetBadRequest
|
2022-07-31 08:00:52 +00:00
|
|
|
from telegram.constants import ChatAction
|
2023-07-18 09:29:31 +00:00
|
|
|
from telegram.ext import filters
|
2022-07-31 08:00:52 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.services.cookies import CookiesService
|
|
|
|
from core.services.template.models import RenderResult
|
|
|
|
from core.services.template.services import TemplateService
|
2023-08-21 14:42:44 +00:00
|
|
|
from plugins.tools.genshin import GenshinHelper
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2023-08-26 10:19:00 +00:00
|
|
|
from utils.uid import mask_number
|
2022-07-31 08:00:52 +00:00
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Update
|
|
|
|
from telegram.ext import ContextTypes
|
|
|
|
from simnet import GenshinClient
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
__all__ = ("LedgerPlugin",)
|
2022-07-31 08:00:52 +00:00
|
|
|
|
2022-09-30 16:26:16 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
class LedgerPlugin(Plugin):
|
|
|
|
"""旅行札记查询"""
|
2022-07-31 08:00:52 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-03-14 01:27:22 +00:00
|
|
|
helper: GenshinHelper,
|
|
|
|
cookies_service: CookiesService,
|
|
|
|
template_service: TemplateService,
|
2022-07-31 08:00:52 +00:00
|
|
|
):
|
|
|
|
self.template_service = template_service
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
self.current_dir = os.getcwd()
|
2023-03-14 01:27:22 +00:00
|
|
|
self.helper = helper
|
2022-07-31 08:00:52 +00:00
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
async def _start_get_ledger(self, client: "GenshinClient", month=None) -> RenderResult:
|
|
|
|
diary_info = await client.get_genshin_diary(client.player_id, month=month)
|
2022-07-31 08:00:52 +00:00
|
|
|
color = ["#73a9c6", "#d56565", "#70b2b4", "#bd9a5a", "#739970", "#7a6da7", "#597ea0"]
|
|
|
|
categories = [
|
2022-10-10 11:07:28 +00:00
|
|
|
{
|
2022-07-31 08:00:52 +00:00
|
|
|
"id": i.id,
|
|
|
|
"name": i.name,
|
|
|
|
"color": color[i.id % len(color)],
|
|
|
|
"amount": i.amount,
|
|
|
|
"percentage": i.percentage,
|
|
|
|
}
|
|
|
|
for i in diary_info.month_data.categories
|
|
|
|
]
|
|
|
|
color = [i["color"] for i in categories]
|
|
|
|
|
|
|
|
def format_amount(amount: int) -> str:
|
|
|
|
return f"{round(amount / 10000, 2)}w" if amount >= 10000 else amount
|
|
|
|
|
|
|
|
ledger_data = {
|
2023-08-26 10:19:00 +00:00
|
|
|
"uid": mask_number(client.player_id),
|
2022-07-31 08:00:52 +00:00
|
|
|
"day": diary_info.month,
|
|
|
|
"current_primogems": format_amount(diary_info.month_data.current_primogems),
|
|
|
|
"gacha": int(diary_info.month_data.current_primogems / 160),
|
|
|
|
"current_mora": format_amount(diary_info.month_data.current_mora),
|
|
|
|
"last_primogems": format_amount(diary_info.month_data.last_primogems),
|
|
|
|
"last_gacha": int(diary_info.month_data.last_primogems / 160),
|
|
|
|
"last_mora": format_amount(diary_info.month_data.last_mora),
|
|
|
|
"categories": categories,
|
2022-10-12 13:39:47 +00:00
|
|
|
"color": color,
|
2022-07-31 08:00:52 +00:00
|
|
|
}
|
2022-10-22 07:03:59 +00:00
|
|
|
render_result = await self.template_service.render(
|
2023-05-09 11:01:45 +00:00
|
|
|
"genshin/ledger/ledger.jinja2", ledger_data, {"width": 580, "height": 610}
|
2022-09-02 15:48:26 +00:00
|
|
|
)
|
2022-10-22 07:03:59 +00:00
|
|
|
return render_result
|
2022-07-31 08:00:52 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
@handler.command(command="ledger", block=False)
|
|
|
|
@handler.message(filters=filters.Regex("^旅行札记查询(.*)"), block=False)
|
2023-07-18 09:29:31 +00:00
|
|
|
async def command_start(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None:
|
2024-03-10 12:40:26 +00:00
|
|
|
user_id = await self.get_real_user_id(update)
|
2022-09-11 10:47:16 +00:00
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
now = datetime.now()
|
|
|
|
now_time = (now - timedelta(days=1)) if now.day == 1 and now.hour <= 4 else now
|
|
|
|
month = now_time.month
|
2022-07-31 08:00:52 +00:00
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
args = self.get_args(context)
|
|
|
|
if len(args) >= 1:
|
|
|
|
month = args[0].replace("月", "")
|
|
|
|
if re_data := re.findall(r"\d+", str(month)):
|
|
|
|
month = int(re_data[0])
|
|
|
|
else:
|
|
|
|
num_dict = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9, "十": 10}
|
|
|
|
month = sum(num_dict.get(i, 0) for i in str(month))
|
|
|
|
# check right
|
|
|
|
allow_month = [now_time.month]
|
|
|
|
|
|
|
|
last_month = now_time.replace(day=1) - timedelta(days=1)
|
|
|
|
allow_month.append(last_month.month)
|
|
|
|
|
|
|
|
last_month = last_month.replace(day=1) - timedelta(days=1)
|
|
|
|
allow_month.append(last_month.month)
|
|
|
|
|
|
|
|
if month not in allow_month and isinstance(month, int):
|
|
|
|
raise IndexError
|
|
|
|
month = now_time.month
|
2022-07-31 08:00:52 +00:00
|
|
|
except IndexError:
|
|
|
|
reply_message = await message.reply_text("仅可查询最新三月的数据,请重新输入")
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-07-31 08:00:52 +00:00
|
|
|
return
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "查询旅行札记")
|
2022-09-11 10:47:16 +00:00
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-07-31 08:00:52 +00:00
|
|
|
try:
|
2024-03-10 12:40:26 +00:00
|
|
|
async with self.helper.genshin(user_id) as client:
|
2023-08-21 14:42:44 +00:00
|
|
|
render_result = await self._start_get_ledger(client, month)
|
2022-07-31 08:00:52 +00:00
|
|
|
except DataNotPublic:
|
2022-10-22 13:54:04 +00:00
|
|
|
reply_message = await message.reply_text("查询失败惹,可能是旅行札记功能被禁用了?请先通过米游社或者 hoyolab 获取一次旅行札记后重试。")
|
2022-07-31 08:00:52 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-07-31 08:00:52 +00:00
|
|
|
return
|
2023-07-18 09:29:31 +00:00
|
|
|
except SimnetBadRequest as exc:
|
|
|
|
if exc.ret_code == -120:
|
2023-02-14 07:21:02 +00:00
|
|
|
await message.reply_text("当前角色冒险等阶不足,暂时无法获取信息")
|
|
|
|
return
|
|
|
|
raise exc
|
2022-09-11 10:47:16 +00:00
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2023-07-18 09:29:31 +00:00
|
|
|
await render_result.reply_photo(message, filename=f"{client.player_id}.png", allow_sending_without_reply=True)
|