2022-07-31 08:00:52 +00:00
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
2022-12-22 15:09:01 +00:00
|
|
|
|
from genshin import GenshinException, DataNotPublic, InvalidCookies
|
2022-10-12 09:35:59 +00:00
|
|
|
|
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
|
2022-07-31 08:00:52 +00:00
|
|
|
|
from telegram.constants import ChatAction
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters
|
2022-11-28 08:38:49 +00:00
|
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-31 08:00:52 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.baseplugin import BasePlugin
|
|
|
|
|
from core.cookies.error import CookiesNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.cookies.services import CookiesService
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.plugin import Plugin, handler
|
2022-10-22 07:03:59 +00:00
|
|
|
|
from core.template.services import RenderResult, TemplateService
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.user.error import UserNotFoundError
|
2022-08-06 12:37:41 +00:00
|
|
|
|
from core.user.services import UserService
|
2022-12-01 02:27:27 +00:00
|
|
|
|
from utils.bot import get_args
|
2022-07-31 08:00:52 +00:00
|
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
|
from utils.decorators.restricts import restricts
|
|
|
|
|
from utils.helpers import get_genshin_client
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-31 08:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-30 16:26:16 +00:00
|
|
|
|
def get_now() -> datetime:
|
|
|
|
|
now = datetime.now()
|
|
|
|
|
return (now - timedelta(days=1)) if now.day == 1 and now.hour <= 4 else now
|
|
|
|
|
|
|
|
|
|
|
2022-07-31 08:00:52 +00:00
|
|
|
|
def check_ledger_month(context: CallbackContext) -> int:
|
2022-09-30 16:26:16 +00:00
|
|
|
|
now_time = get_now()
|
|
|
|
|
month = now_time.month
|
2022-12-01 02:27:27 +00:00
|
|
|
|
args = get_args(context)
|
2022-07-31 08:00:52 +00:00
|
|
|
|
if len(args) >= 1:
|
2022-10-12 16:41:40 +00:00
|
|
|
|
month = args[0].replace("月", "")
|
|
|
|
|
if re_data := re.findall(r"\d+", str(month)):
|
2022-07-31 08:00:52 +00:00
|
|
|
|
month = int(re_data[0])
|
|
|
|
|
else:
|
2022-10-10 11:07:28 +00:00
|
|
|
|
num_dict = {"一": 1, "二": 2, "三": 3, "四": 4, "五": 5, "六": 6, "七": 7, "八": 8, "九": 9, "十": 10}
|
2022-08-06 06:22:37 +00:00
|
|
|
|
month = sum(num_dict.get(i, 0) for i in str(month))
|
2022-07-31 08:00:52 +00:00
|
|
|
|
# check right
|
2022-09-30 16:26:16 +00:00
|
|
|
|
allow_month = [now_time.month]
|
2022-07-31 08:00:52 +00:00
|
|
|
|
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 in allow_month:
|
|
|
|
|
return month
|
|
|
|
|
elif isinstance(month, int):
|
|
|
|
|
raise IndexError
|
|
|
|
|
return now_time.month
|
|
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
class Ledger(Plugin, BasePlugin):
|
2022-07-31 08:00:52 +00:00
|
|
|
|
"""旅行札记"""
|
|
|
|
|
|
2022-10-10 11:07:28 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
user_service: UserService = None,
|
|
|
|
|
cookies_service: CookiesService = None,
|
|
|
|
|
template_service: TemplateService = None,
|
|
|
|
|
):
|
2022-07-31 08:00:52 +00:00
|
|
|
|
self.template_service = template_service
|
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
|
self.user_service = user_service
|
|
|
|
|
self.current_dir = os.getcwd()
|
|
|
|
|
|
2022-10-22 07:03:59 +00:00
|
|
|
|
async def _start_get_ledger(self, client, month=None) -> RenderResult:
|
2022-07-31 08:00:52 +00:00
|
|
|
|
try:
|
|
|
|
|
diary_info = await client.get_diary(client.uid, month=month)
|
|
|
|
|
except GenshinException as error:
|
|
|
|
|
raise error
|
|
|
|
|
color = ["#73a9c6", "#d56565", "#70b2b4", "#bd9a5a", "#739970", "#7a6da7", "#597ea0"]
|
2022-10-10 11:07:28 +00:00
|
|
|
|
categories = [
|
|
|
|
|
{
|
|
|
|
|
"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
|
|
|
|
|
]
|
2022-07-31 08:00:52 +00:00
|
|
|
|
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 = {
|
|
|
|
|
"uid": client.uid,
|
|
|
|
|
"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(
|
2022-10-12 13:39:47 +00:00
|
|
|
|
"genshin/ledger/ledger.html", ledger_data, {"width": 580, "height": 610}
|
2022-10-10 11:07:28 +00:00
|
|
|
|
)
|
2022-10-22 07:03:59 +00:00
|
|
|
|
return render_result
|
2022-07-31 08:00:52 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@handler(CommandHandler, command="ledger", block=False)
|
2022-09-11 10:47:16 +00:00
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^旅行札记查询(.*)"), block=False)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@restricts()
|
2022-07-31 08:00:52 +00:00
|
|
|
|
@error_callable
|
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
|
|
|
|
user = update.effective_user
|
2022-09-11 10:47:16 +00:00
|
|
|
|
message = update.effective_message
|
2022-07-31 08:00:52 +00:00
|
|
|
|
try:
|
|
|
|
|
month = check_ledger_month(context)
|
|
|
|
|
except IndexError:
|
|
|
|
|
reply_message = await message.reply_text("仅可查询最新三月的数据,请重新输入")
|
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
|
|
|
|
return
|
2022-12-23 13:35:26 +00:00
|
|
|
|
logger.info("用户 %s[%s] 查询旅行札记", user.full_name, user.id)
|
2022-09-11 10:47:16 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-07-31 08:00:52 +00:00
|
|
|
|
try:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
client = await get_genshin_client(user.id)
|
2022-12-22 15:09:01 +00:00
|
|
|
|
try:
|
|
|
|
|
render_result = await self._start_get_ledger(client, month)
|
2022-12-23 13:35:26 +00:00
|
|
|
|
except InvalidCookies as exc: # 如果抛出InvalidCookies 判断是否真的玄学过期(或权限不足?)
|
2022-12-22 15:09:01 +00:00
|
|
|
|
await client.get_genshin_user(client.uid)
|
2022-12-23 13:35:26 +00:00
|
|
|
|
logger.warning(
|
|
|
|
|
"用户 %s[%s] 无法请求旅行札记数据 API返回信息为 [%s]%s", user.full_name, user.id, exc.retcode, exc.original
|
|
|
|
|
)
|
2023-01-07 07:27:00 +00:00
|
|
|
|
reply_message = await message.reply_text("出错了呜呜呜 ~ 当前访问令牌无法请求角色数数据,请尝试重新获取Cookie。")
|
2022-12-22 15:09:01 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
|
|
|
|
return
|
2022-09-08 01:08:37 +00:00
|
|
|
|
except (UserNotFoundError, CookiesNotFoundError):
|
2022-11-28 08:38:49 +00:00
|
|
|
|
buttons = [[InlineKeyboardButton("点我绑定账号", url=create_deep_linked_url(context.bot.username, "set_cookie"))]]
|
2022-07-31 08:00:52 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2022-10-12 09:35:59 +00:00
|
|
|
|
reply_message = await message.reply_text(
|
|
|
|
|
"未查询到您所绑定的账号信息,请先私聊派蒙绑定账号", reply_markup=InlineKeyboardMarkup(buttons)
|
|
|
|
|
)
|
2022-07-31 08:00:52 +00:00
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
2022-10-12 09:35:59 +00:00
|
|
|
|
|
2022-07-31 08:00:52 +00:00
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
2022-10-12 09:35:59 +00:00
|
|
|
|
else:
|
2022-10-22 13:54:04 +00:00
|
|
|
|
await message.reply_text("未查询到您所绑定的账号信息,请先绑定账号", reply_markup=InlineKeyboardMarkup(buttons))
|
2022-07-31 08:00:52 +00:00
|
|
|
|
return
|
|
|
|
|
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):
|
|
|
|
|
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id, 30)
|
|
|
|
|
self._add_delete_message_job(context, message.chat_id, message.message_id, 30)
|
|
|
|
|
return
|
2022-09-11 10:47:16 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2022-10-22 07:03:59 +00:00
|
|
|
|
await render_result.reply_photo(message, filename=f"{client.uid}.png", allow_sending_without_reply=True)
|