2022-09-17 16:28:51 +00:00
|
|
|
|
import asyncio
|
2022-10-22 03:31:44 +00:00
|
|
|
|
import os
|
2022-09-17 16:28:51 +00:00
|
|
|
|
import re
|
|
|
|
|
from asyncio import Lock
|
|
|
|
|
from ctypes import c_double
|
|
|
|
|
from datetime import datetime
|
2022-10-07 05:02:49 +00:00
|
|
|
|
from functools import partial
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from multiprocessing import Value
|
|
|
|
|
from pathlib import Path
|
2022-09-18 04:19:29 +00:00
|
|
|
|
from ssl import SSLZeroReturnError
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from typing import Any, Dict, Iterable, Iterator, List, Literal, Optional, Tuple, TYPE_CHECKING
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
from aiofiles import open as async_open
|
2022-09-20 15:00:08 +00:00
|
|
|
|
from arkowrapper import ArkoWrapper
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
from httpx import AsyncClient, HTTPError
|
|
|
|
|
from pydantic import BaseModel
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from simnet.errors import InvalidCookies, BadRequest as SimnetBadRequest
|
|
|
|
|
from simnet.models.genshin.chronicle.characters import Character
|
|
|
|
|
from telegram import Message, User
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from telegram.constants import ChatAction, ParseMode
|
|
|
|
|
from telegram.error import RetryAfter, TimedOut
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from core.dependence.assets import AssetsCouldNotFound, AssetsService, AssetsServiceType
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from core.services.template.models import FileType, RenderGroupResult
|
|
|
|
|
from core.services.template.services import TemplateService
|
2022-10-07 05:02:49 +00:00
|
|
|
|
from metadata.genshin import AVATAR_DATA, HONEY_DATA
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from plugins.tools.genshin import CharacterDetails, PlayerNotFoundError, CookiesNotFoundError, GenshinHelper
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from utils.log import logger
|
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
|
try:
|
|
|
|
|
import ujson as jsonlib
|
|
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
import json as jsonlib
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from telegram import Update
|
|
|
|
|
from telegram.ext import ContextTypes
|
|
|
|
|
from simnet import GenshinClient
|
|
|
|
|
|
2022-10-07 05:02:49 +00:00
|
|
|
|
INTERVAL = 1
|
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
DATA_TYPE = Dict[str, List[List[str]]]
|
2022-10-10 11:07:28 +00:00
|
|
|
|
DATA_FILE_PATH = Path(__file__).joinpath("../daily.json").resolve()
|
2023-08-21 04:39:38 +00:00
|
|
|
|
DOMAINS = ["忘却之峡", "太山府", "菫色之庭", "昏识塔", "塞西莉亚苗圃", "震雷连山密宫", "砂流之庭", "有顶塔", "苍白的遗荣", "深潮的余响"]
|
|
|
|
|
DOMAIN_AREA_MAP = dict(zip(DOMAINS, ["蒙德", "璃月", "稻妻", "须弥", "枫丹"] * 2))
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2022-10-10 11:07:28 +00:00
|
|
|
|
WEEK_MAP = ["一", "二", "三", "四", "五", "六", "日"]
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
|
2022-10-10 11:07:28 +00:00
|
|
|
|
def sort_item(items: List["ItemData"]) -> Iterable["ItemData"]:
|
2022-09-18 04:19:29 +00:00
|
|
|
|
"""对武器和角色进行排序
|
|
|
|
|
|
2022-09-20 15:00:08 +00:00
|
|
|
|
排序规则:持有(星级 > 等级 > 命座/精炼) > 未持有(星级 > 等级 > 命座/精炼)
|
2022-09-18 04:19:29 +00:00
|
|
|
|
"""
|
2022-09-20 15:00:08 +00:00
|
|
|
|
return (
|
|
|
|
|
ArkoWrapper(items)
|
|
|
|
|
.sort(lambda x: x.level or -1, reverse=True)
|
|
|
|
|
.groupby(lambda x: x.level is None) # 根据持有与未持有进行分组并排序
|
|
|
|
|
.map(
|
|
|
|
|
lambda x: (
|
|
|
|
|
ArkoWrapper(x[1])
|
|
|
|
|
.sort(lambda y: y.rarity, reverse=True)
|
|
|
|
|
.groupby(lambda y: y.rarity) # 根据星级分组并排序
|
2022-10-10 11:07:28 +00:00
|
|
|
|
.map(
|
|
|
|
|
lambda y: (
|
|
|
|
|
ArkoWrapper(y[1])
|
|
|
|
|
.sort(lambda z: z.refinement or z.constellation or -1, reverse=True)
|
|
|
|
|
.groupby(lambda z: z.refinement or z.constellation or -1) # 根据命座/精炼进行分组并排序
|
|
|
|
|
.map(lambda i: ArkoWrapper(i[1]).sort(lambda j: j.id))
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-09-20 15:00:08 +00:00
|
|
|
|
)
|
2022-10-10 11:07:28 +00:00
|
|
|
|
)
|
|
|
|
|
.flat(3)
|
2022-09-20 15:00:08 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_material_serial_name(names: Iterable[str]) -> str:
|
|
|
|
|
"""获取材料的系列名"""
|
|
|
|
|
|
|
|
|
|
def all_substrings(string: str) -> Iterator[str]:
|
|
|
|
|
"""获取字符串的所有连续字串"""
|
|
|
|
|
length = len(string)
|
|
|
|
|
for i in range(length):
|
|
|
|
|
for j in range(i + 1, length + 1):
|
|
|
|
|
yield string[i:j]
|
|
|
|
|
|
|
|
|
|
result = []
|
|
|
|
|
for name_a, name_b in ArkoWrapper(names).repeat(1).group(2).unique(list):
|
|
|
|
|
for sub_string in all_substrings(name_a):
|
|
|
|
|
if sub_string in ArkoWrapper(all_substrings(name_b)):
|
|
|
|
|
result.append(sub_string)
|
2022-09-22 03:11:23 +00:00
|
|
|
|
result = ArkoWrapper(result).sort(len, reverse=True)[0]
|
2022-10-10 11:07:28 +00:00
|
|
|
|
chars = {"的": 0, "之": 0}
|
2022-09-22 03:11:23 +00:00
|
|
|
|
for char, k in chars.items():
|
|
|
|
|
result = result.split(char)[k]
|
|
|
|
|
return result
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
class DailyMaterial(Plugin):
|
2022-09-17 16:28:51 +00:00
|
|
|
|
"""每日素材表"""
|
2022-10-10 11:07:28 +00:00
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
data: DATA_TYPE
|
|
|
|
|
locks: Tuple[Lock] = (Lock(), Lock())
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
assets: AssetsService,
|
|
|
|
|
template_service: TemplateService,
|
|
|
|
|
helper: GenshinHelper,
|
|
|
|
|
character_details: CharacterDetails,
|
|
|
|
|
):
|
2022-09-17 16:28:51 +00:00
|
|
|
|
self.assets_service = assets
|
2022-09-17 17:33:11 +00:00
|
|
|
|
self.template_service = template_service
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.helper = helper
|
|
|
|
|
self.character_details = character_details
|
2022-09-17 16:28:51 +00:00
|
|
|
|
self.client = AsyncClient()
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
async def initialize(self):
|
2022-09-18 04:19:29 +00:00
|
|
|
|
"""插件在初始化时,会检查一下本地是否缓存了每日素材的数据"""
|
2022-09-17 16:28:51 +00:00
|
|
|
|
data = None
|
2022-09-18 04:19:29 +00:00
|
|
|
|
|
|
|
|
|
async def task_daily():
|
|
|
|
|
async with self.locks[0]:
|
|
|
|
|
logger.info("正在开始获取每日素材缓存")
|
|
|
|
|
self.data = await self._refresh_data()
|
|
|
|
|
|
2022-10-22 03:31:44 +00:00
|
|
|
|
if (not DATA_FILE_PATH.exists()) or ( # 若缓存不存在
|
|
|
|
|
(datetime.today() - datetime.fromtimestamp(os.stat(DATA_FILE_PATH).st_mtime)).days > 3 # 若缓存过期,超过了3天
|
|
|
|
|
):
|
2022-12-25 13:26:08 +00:00
|
|
|
|
asyncio.create_task(task_daily()) # 创建后台任务
|
2022-09-18 04:19:29 +00:00
|
|
|
|
if not data and DATA_FILE_PATH.exists(): # 若存在,则读取至内存中
|
2022-09-17 16:28:51 +00:00
|
|
|
|
async with async_open(DATA_FILE_PATH) as file:
|
2023-07-18 09:29:31 +00:00
|
|
|
|
data = jsonlib.loads(await file.read())
|
2022-09-18 04:19:29 +00:00
|
|
|
|
self.data = data
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def _get_skills_data(self, client: "GenshinClient", character: Character) -> Optional[List[int]]:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
detail = await self.character_details.get_character_details(client, character)
|
|
|
|
|
if detail is None:
|
2022-10-28 07:58:07 +00:00
|
|
|
|
return None
|
|
|
|
|
talents = [t for t in detail.talents if t.type in ["attack", "skill", "burst"]]
|
|
|
|
|
return [t.level for t in talents]
|
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def _get_data_from_user(self, user: User) -> Tuple[Optional["GenshinClient"], Dict[str, List[Any]]]:
|
2022-09-18 04:19:29 +00:00
|
|
|
|
"""获取已经绑定的账号的角色、武器信息"""
|
2022-10-10 11:07:28 +00:00
|
|
|
|
user_data = {"avatar": [], "weapon": []}
|
2022-09-17 16:28:51 +00:00
|
|
|
|
try:
|
|
|
|
|
logger.debug("尝试获取已绑定的原神账号")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
client = await self.helper.get_genshin_client(user.id)
|
2023-07-18 09:29:31 +00:00
|
|
|
|
logger.debug("获取账号数据成功: UID=%s", client.player_id)
|
|
|
|
|
characters = await client.get_genshin_characters(client.player_id)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
for character in characters:
|
2022-10-10 11:07:28 +00:00
|
|
|
|
if character.name == "旅行者": # 跳过主角
|
2022-10-07 05:02:49 +00:00
|
|
|
|
continue
|
2022-10-10 11:07:28 +00:00
|
|
|
|
cid = AVATAR_DATA[str(character.id)]["id"]
|
2022-09-17 16:28:51 +00:00
|
|
|
|
weapon = character.weapon
|
2022-10-10 11:07:28 +00:00
|
|
|
|
user_data["avatar"].append(
|
2022-09-17 16:28:51 +00:00
|
|
|
|
ItemData(
|
2022-10-10 11:07:28 +00:00
|
|
|
|
id=cid,
|
|
|
|
|
name=character.name,
|
2023-07-18 09:29:31 +00:00
|
|
|
|
rarity=int(character.rarity),
|
2022-10-10 11:07:28 +00:00
|
|
|
|
level=character.level,
|
2022-09-17 16:28:51 +00:00
|
|
|
|
constellation=character.constellation,
|
2022-10-28 07:58:07 +00:00
|
|
|
|
gid=character.id,
|
2022-10-10 11:07:28 +00:00
|
|
|
|
icon=(await self.assets_service.avatar(cid).icon()).as_uri(),
|
2022-11-22 10:36:52 +00:00
|
|
|
|
origin=character,
|
2022-09-17 16:28:51 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
2022-10-10 11:07:28 +00:00
|
|
|
|
user_data["weapon"].append(
|
2022-09-17 16:28:51 +00:00
|
|
|
|
ItemData(
|
2022-10-10 11:07:28 +00:00
|
|
|
|
id=str(weapon.id),
|
|
|
|
|
name=weapon.name,
|
|
|
|
|
level=weapon.level,
|
|
|
|
|
rarity=weapon.rarity,
|
2022-09-17 16:28:51 +00:00
|
|
|
|
refinement=weapon.refinement,
|
2022-10-10 11:07:28 +00:00
|
|
|
|
icon=(
|
|
|
|
|
await getattr( # 判定武器的突破次数是否大于 2 ;若是, 则将图标替换为 awakened (觉醒) 的图标
|
|
|
|
|
self.assets_service.weapon(weapon.id), "icon" if weapon.ascension < 2 else "awaken"
|
|
|
|
|
)()
|
|
|
|
|
).as_uri(),
|
|
|
|
|
c_path=(await self.assets_service.avatar(cid).side()).as_uri(),
|
2022-09-17 16:28:51 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
except (PlayerNotFoundError, CookiesNotFoundError):
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("未查询到用户 %s[%s] 所绑定的账号信息", user.full_name, user.id)
|
2022-10-22 13:54:04 +00:00
|
|
|
|
except InvalidCookies:
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s] 所绑定的账号信息已失效", user.full_name, user.id)
|
2022-10-29 03:53:08 +00:00
|
|
|
|
else:
|
|
|
|
|
# 没有异常返回数据
|
|
|
|
|
return client, user_data
|
|
|
|
|
# 有上述异常的, client 会返回 None
|
|
|
|
|
return None, user_data
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2022-10-10 11:07:28 +00:00
|
|
|
|
@handler.command("daily_material", block=False)
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def daily_material(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
|
2022-09-17 16:28:51 +00:00
|
|
|
|
user = update.effective_user
|
2022-10-19 06:19:00 +00:00
|
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
|
args = self.get_args(context)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
now = datetime.now()
|
|
|
|
|
|
2022-09-20 15:00:08 +00:00
|
|
|
|
try:
|
|
|
|
|
weekday = (_ := int(args[0])) - (_ > 0)
|
|
|
|
|
weekday = (weekday % 7 + 7) % 7
|
|
|
|
|
time = title = f"星期{WEEK_MAP[weekday]}"
|
|
|
|
|
except (ValueError, IndexError):
|
2022-09-17 16:28:51 +00:00
|
|
|
|
title = "今日"
|
|
|
|
|
weekday = now.weekday() - (1 if now.hour < 4 else 0)
|
|
|
|
|
weekday = 6 if weekday < 0 else weekday
|
2022-10-22 07:03:59 +00:00
|
|
|
|
time = f"星期{WEEK_MAP[weekday]}"
|
2022-10-10 11:07:28 +00:00
|
|
|
|
full = bool(args and args[-1] == "full") # 判定最后一个参数是不是 full
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 %s[%s}] 每日素材命令请求 || 参数 weekday=%s full=%s", user.full_name, user.id, WEEK_MAP[weekday], full)
|
2022-09-17 18:07:42 +00:00
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
if weekday == 6:
|
2022-10-19 06:19:00 +00:00
|
|
|
|
await message.reply_text(
|
2022-10-10 11:07:28 +00:00
|
|
|
|
("今天" if title == "今日" else "这天") + "是星期天, <b>全部素材都可以</b>刷哦~", parse_mode=ParseMode.HTML
|
2022-09-17 16:28:51 +00:00
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
2022-09-18 04:19:29 +00:00
|
|
|
|
if self.locks[0].locked(): # 若检测到了第一个锁:正在下载每日素材表的数据
|
2022-10-19 06:19:00 +00:00
|
|
|
|
notice = await message.reply_text("派蒙正在摘抄每日素材表,以后再来探索吧~")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(notice, delay=5)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
return
|
|
|
|
|
|
2022-09-18 04:19:29 +00:00
|
|
|
|
if self.locks[1].locked(): # 若检测到了第二个锁:正在下载角色、武器、材料的图标
|
2022-10-19 06:19:00 +00:00
|
|
|
|
await message.reply_text("派蒙正在搬运每日素材的图标,以后再来探索吧~")
|
2022-09-17 16:28:51 +00:00
|
|
|
|
return
|
|
|
|
|
|
2022-10-19 06:19:00 +00:00
|
|
|
|
notice = await message.reply_text("派蒙可能需要找找图标素材,还请耐心等待哦~")
|
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2022-09-18 04:19:29 +00:00
|
|
|
|
# 获取已经缓存的秘境素材信息
|
2022-10-10 11:07:28 +00:00
|
|
|
|
local_data = {"avatar": [], "weapon": []}
|
2022-09-18 04:19:29 +00:00
|
|
|
|
if not self.data: # 若没有缓存每日素材表的数据
|
2022-09-17 16:28:51 +00:00
|
|
|
|
logger.info("正在获取每日素材缓存")
|
2022-10-07 05:02:49 +00:00
|
|
|
|
self.data = await self._refresh_data()
|
2022-09-17 16:28:51 +00:00
|
|
|
|
for domain, sche in self.data.items():
|
2023-05-14 10:02:28 +00:00
|
|
|
|
domain = domain.strip()
|
|
|
|
|
area = DOMAIN_AREA_MAP[domain] # 获取秘境所在的区域
|
2022-10-10 11:07:28 +00:00
|
|
|
|
type_ = "avatar" if DOMAINS.index(domain) < 4 else "weapon" # 获取秘境的培养素材的类型:是天赋书还是武器突破材料
|
2022-09-18 04:19:29 +00:00
|
|
|
|
# 将读取到的数据存入 local_data 中
|
2022-10-10 11:07:28 +00:00
|
|
|
|
local_data[type_].append({"name": area, "materials": sche[weekday][0], "items": sche[weekday][1]})
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
# 尝试获取用户已绑定的原神账号信息
|
2022-09-17 16:34:50 +00:00
|
|
|
|
client, user_data = await self._get_data_from_user(user)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2022-10-19 06:19:00 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2023-07-18 09:29:31 +00:00
|
|
|
|
render_data = RenderData(title=title, time=time, uid=client.player_id if client else client)
|
2022-10-29 03:53:08 +00:00
|
|
|
|
|
2022-11-01 08:28:59 +00:00
|
|
|
|
calculator_sync: bool = True # 默认养成计算器同步为开启
|
|
|
|
|
for type_ in ["avatar", "weapon"]:
|
|
|
|
|
areas = []
|
|
|
|
|
for area_data in local_data[type_]: # 遍历每个区域的信息:蒙德、璃月、稻妻、须弥
|
|
|
|
|
items = []
|
|
|
|
|
for id_ in area_data["items"]: # 遍历所有该区域下,当天(weekday)可以培养的角色、武器
|
|
|
|
|
added = False
|
|
|
|
|
for i in user_data[type_]: # 从已经获取的角色数据中查找对应角色、武器
|
|
|
|
|
if id_ == str(i.id):
|
|
|
|
|
if i.rarity > 3: # 跳过 3 星及以下的武器
|
|
|
|
|
if type_ == "avatar" and client and calculator_sync: # client 不为 None 时给角色添加天赋信息
|
|
|
|
|
try:
|
2022-11-22 10:36:52 +00:00
|
|
|
|
skills = await self._get_skills_data(client, i.origin)
|
2022-10-29 03:53:08 +00:00
|
|
|
|
i.skills = skills
|
2022-11-17 08:19:01 +00:00
|
|
|
|
except InvalidCookies:
|
|
|
|
|
calculator_sync = False
|
2023-07-18 09:29:31 +00:00
|
|
|
|
except SimnetBadRequest as e:
|
|
|
|
|
if e.ret_code == -502002:
|
2022-11-01 08:28:59 +00:00
|
|
|
|
calculator_sync = False # 发现角色养成计算器没启用 设置状态为 False 并防止下次继续获取
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(notice, delay=5)
|
2022-11-01 08:28:59 +00:00
|
|
|
|
await notice.edit_text(
|
|
|
|
|
"获取角色天赋信息失败,如果想要显示角色天赋信息,请先在米游社/HoYoLab中使用一次<b>养成计算器</b>后再使用此功能~",
|
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
|
|
|
|
items.append(i)
|
|
|
|
|
added = True
|
|
|
|
|
if added:
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
item = HONEY_DATA[type_][id_]
|
|
|
|
|
except KeyError: # 跳过不存在或者已忽略的角色、武器
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.warning("未在 honey 数据中找到 %s[%s] 的信息", type_, id_)
|
2022-11-01 08:28:59 +00:00
|
|
|
|
continue
|
|
|
|
|
if item[2] < 4: # 跳过 3 星及以下的武器
|
|
|
|
|
continue
|
|
|
|
|
items.append(
|
|
|
|
|
ItemData( # 添加角色数据中未找到的
|
|
|
|
|
id=id_,
|
|
|
|
|
name=item[1],
|
|
|
|
|
rarity=item[2],
|
|
|
|
|
icon=(await getattr(self.assets_service, type_)(id_).icon()).as_uri(),
|
2022-10-10 11:07:28 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
2022-11-01 08:28:59 +00:00
|
|
|
|
materials = []
|
|
|
|
|
for mid in area_data["materials"]: # 添加这个区域当天(weekday)的培养素材
|
|
|
|
|
try:
|
|
|
|
|
path = (await self.assets_service.material(mid).icon()).as_uri()
|
|
|
|
|
material = HONEY_DATA["material"][mid]
|
|
|
|
|
materials.append(ItemData(id=mid, icon=path, name=material[1], rarity=material[2]))
|
|
|
|
|
except AssetsCouldNotFound as exc:
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.warning("AssetsCouldNotFound message[%s] target[%s]", exc.message, exc.target)
|
2022-11-01 08:28:59 +00:00
|
|
|
|
await notice.edit_text("出错了呜呜呜 ~ 派蒙找不到一些素材")
|
|
|
|
|
return
|
|
|
|
|
areas.append(
|
|
|
|
|
AreaData(
|
|
|
|
|
name=area_data["name"],
|
|
|
|
|
materials=materials,
|
|
|
|
|
# template previewer pickle cannot serialize generator
|
|
|
|
|
items=list(sort_item(items)),
|
|
|
|
|
material_name=get_material_serial_name(map(lambda x: x.name, materials)),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
setattr(render_data, {"avatar": "character"}.get(type_, type_), areas)
|
2022-09-18 04:19:29 +00:00
|
|
|
|
|
2022-10-19 06:19:00 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-09-20 16:32:49 +00:00
|
|
|
|
|
2022-10-22 07:03:59 +00:00
|
|
|
|
# 是否发送原图
|
|
|
|
|
file_type = FileType.DOCUMENT if full else FileType.PHOTO
|
2022-09-20 16:32:49 +00:00
|
|
|
|
|
2022-10-22 07:03:59 +00:00
|
|
|
|
character_img_data, weapon_img_data = await asyncio.gather(
|
|
|
|
|
self.template_service.render( # 渲染角色素材页
|
2023-05-09 11:01:45 +00:00
|
|
|
|
"genshin/daily_material/character.jinja2",
|
2022-10-22 07:03:59 +00:00
|
|
|
|
{"data": render_data},
|
|
|
|
|
{"width": 1164, "height": 500},
|
|
|
|
|
file_type=file_type,
|
2022-10-22 09:12:39 +00:00
|
|
|
|
ttl=30 * 24 * 60 * 60,
|
2022-10-22 07:03:59 +00:00
|
|
|
|
),
|
|
|
|
|
self.template_service.render( # 渲染武器素材页
|
2023-05-09 11:01:45 +00:00
|
|
|
|
"genshin/daily_material/weapon.jinja2",
|
2022-10-22 07:03:59 +00:00
|
|
|
|
{"data": render_data},
|
|
|
|
|
{"width": 1164, "height": 500},
|
|
|
|
|
file_type=file_type,
|
2022-10-22 09:12:39 +00:00
|
|
|
|
ttl=30 * 24 * 60 * 60,
|
2022-10-22 07:03:59 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
2022-09-18 04:19:29 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(notice, delay=5)
|
2022-10-19 06:19:00 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2022-10-22 07:03:59 +00:00
|
|
|
|
|
|
|
|
|
character_img_data.filename = f"{title}可培养角色.png"
|
|
|
|
|
weapon_img_data.filename = f"{title}可培养武器.png"
|
|
|
|
|
|
|
|
|
|
await RenderGroupResult([character_img_data, weapon_img_data]).reply_media_group(message)
|
|
|
|
|
|
2022-09-20 16:32:49 +00:00
|
|
|
|
logger.debug("角色、武器培养素材图发送成功")
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2023-06-06 09:03:08 +00:00
|
|
|
|
@handler.command("refresh_daily_material", admin=True, block=False)
|
2023-07-18 09:29:31 +00:00
|
|
|
|
async def refresh(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
|
2022-10-07 05:02:49 +00:00
|
|
|
|
user = update.effective_user
|
2022-09-17 16:28:51 +00:00
|
|
|
|
message = update.effective_message
|
2022-10-07 05:02:49 +00:00
|
|
|
|
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.info("用户 {%s}[%s] 刷新[bold]每日素材[/]缓存命令", user.full_name, user.id, extra={"markup": True})
|
2022-09-17 16:28:51 +00:00
|
|
|
|
if self.locks[0].locked():
|
|
|
|
|
notice = await message.reply_text("派蒙还在抄每日素材表呢,我有在好好工作哦~")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(notice, delay=10)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
return
|
|
|
|
|
if self.locks[1].locked():
|
|
|
|
|
notice = await message.reply_text("派蒙正在搬运每日素材图标,在努力工作呢!")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(notice, delay=10)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
return
|
2022-09-18 04:19:29 +00:00
|
|
|
|
async with self.locks[1]: # 锁住第二把锁
|
2022-09-17 16:28:51 +00:00
|
|
|
|
notice = await message.reply_text("派蒙正在重新摘抄每日素材表,请稍等~", parse_mode=ParseMode.HTML)
|
2022-09-18 04:19:29 +00:00
|
|
|
|
async with self.locks[0]: # 锁住第一把锁
|
2022-09-17 16:28:51 +00:00
|
|
|
|
data = await self._refresh_data()
|
|
|
|
|
notice = await notice.edit_text(
|
2022-10-10 11:07:28 +00:00
|
|
|
|
"每日素材表" + ("摘抄<b>完成!</b>" if data else "坏掉了!等会它再长好了之后我再抄。。。") + "\n正搬运每日素材的图标中。。。",
|
|
|
|
|
parse_mode=ParseMode.HTML,
|
2022-09-17 16:28:51 +00:00
|
|
|
|
)
|
|
|
|
|
self.data = data or self.data
|
2022-10-07 05:02:49 +00:00
|
|
|
|
time = await self._download_icon(notice)
|
|
|
|
|
|
|
|
|
|
async def job(_, n):
|
2022-10-10 11:07:28 +00:00
|
|
|
|
await n.edit_text(n.text_html.split("\n")[0] + "\n每日素材图标搬运<b>完成!</b>", parse_mode=ParseMode.HTML)
|
2022-10-07 05:02:49 +00:00
|
|
|
|
await asyncio.sleep(INTERVAL)
|
|
|
|
|
await notice.delete()
|
|
|
|
|
|
|
|
|
|
context.application.job_queue.run_once(
|
2022-10-10 11:07:28 +00:00
|
|
|
|
partial(job, n=notice), when=time + INTERVAL, name="notice_msg_final_job"
|
2022-10-07 05:02:49 +00:00
|
|
|
|
)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
async def _refresh_data(self, retry: int = 5) -> DATA_TYPE:
|
2022-09-18 04:19:29 +00:00
|
|
|
|
"""刷新来自 honey impact 的每日素材表"""
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from bs4 import Tag
|
2022-10-10 11:07:28 +00:00
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
result = {}
|
2022-09-18 04:19:29 +00:00
|
|
|
|
for i in range(retry): # 重复尝试 retry 次
|
2022-09-17 16:28:51 +00:00
|
|
|
|
try:
|
|
|
|
|
response = await self.client.get("https://genshin.honeyhunterworld.com/?lang=CHS")
|
2022-10-10 11:07:28 +00:00
|
|
|
|
soup = BeautifulSoup(response.text, "lxml")
|
2022-09-17 16:28:51 +00:00
|
|
|
|
calendar = soup.select(".calendar_day_wrap")[0]
|
2022-10-10 11:07:28 +00:00
|
|
|
|
key: str = ""
|
2022-09-17 16:28:51 +00:00
|
|
|
|
for tag in calendar:
|
|
|
|
|
tag: Tag
|
2022-10-10 11:07:28 +00:00
|
|
|
|
if tag.name == "span": # 如果是秘境
|
2023-04-25 14:35:58 +00:00
|
|
|
|
key = tag.find("a").text.strip()
|
2022-09-17 16:28:51 +00:00
|
|
|
|
result[key] = [[[], []] for _ in range(7)]
|
2022-10-10 11:07:28 +00:00
|
|
|
|
for day, div in enumerate(tag.find_all("div")):
|
2022-10-07 05:02:49 +00:00
|
|
|
|
result[key][day][0] = []
|
2022-10-10 11:07:28 +00:00
|
|
|
|
for a in div.find_all("a"):
|
|
|
|
|
honey_id = re.findall(r"/(.*)?/", a["href"])[0]
|
|
|
|
|
mid: str = [i[0] for i in HONEY_DATA["material"].items() if i[1][0] == honey_id][0]
|
2022-10-07 05:02:49 +00:00
|
|
|
|
result[key][day][0].append(mid)
|
2022-09-18 04:19:29 +00:00
|
|
|
|
else: # 如果是角色或武器
|
2022-10-10 11:07:28 +00:00
|
|
|
|
id_ = re.findall(r"/(.*)?/", tag["href"])[0]
|
|
|
|
|
if tag.text.strip() == "旅行者": # 忽略主角
|
2022-09-17 16:28:51 +00:00
|
|
|
|
continue
|
2022-10-10 11:07:28 +00:00
|
|
|
|
id_ = ("" if id_.startswith("i_n") else "10000") + re.findall(r"\d+", id_)[0]
|
|
|
|
|
for day in map(int, tag.find("div")["data-days"]): # 获取该角色/武器的可培养天
|
2022-09-17 16:28:51 +00:00
|
|
|
|
result[key][day][1].append(id_)
|
|
|
|
|
for stage, schedules in result.items():
|
|
|
|
|
for day, _ in enumerate(schedules):
|
2022-09-17 18:07:42 +00:00
|
|
|
|
# noinspection PyUnresolvedReferences
|
2022-09-18 04:19:29 +00:00
|
|
|
|
result[stage][day][1] = list(set(result[stage][day][1])) # 去重
|
2022-10-10 11:07:28 +00:00
|
|
|
|
async with async_open(DATA_FILE_PATH, "w", encoding="utf-8") as file:
|
2023-07-18 09:29:31 +00:00
|
|
|
|
await file.write(jsonlib.dumps(result)) # skipcq: PY-W0079
|
2022-09-17 16:28:51 +00:00
|
|
|
|
logger.info("每日素材刷新成功")
|
|
|
|
|
break
|
2022-09-18 04:19:29 +00:00
|
|
|
|
except (HTTPError, SSLZeroReturnError):
|
|
|
|
|
from asyncio import sleep
|
2022-10-10 11:07:28 +00:00
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
await sleep(1)
|
|
|
|
|
if i <= retry - 1:
|
|
|
|
|
logger.warning("每日素材刷新失败, 正在重试")
|
|
|
|
|
else:
|
|
|
|
|
logger.error("每日素材刷新失败, 请稍后重试")
|
|
|
|
|
continue
|
|
|
|
|
# noinspection PyTypeChecker
|
|
|
|
|
return result
|
|
|
|
|
|
2022-10-07 05:02:49 +00:00
|
|
|
|
async def _download_icon(self, message: Optional[Message] = None) -> float:
|
2022-09-18 04:19:29 +00:00
|
|
|
|
"""下载素材图标"""
|
2022-10-07 05:02:49 +00:00
|
|
|
|
asset_list = []
|
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
from time import time as time_
|
2022-10-10 11:07:28 +00:00
|
|
|
|
|
2022-09-17 16:28:51 +00:00
|
|
|
|
lock = asyncio.Lock()
|
|
|
|
|
|
2022-10-07 05:02:49 +00:00
|
|
|
|
the_time = Value(c_double, time_() - INTERVAL)
|
|
|
|
|
|
|
|
|
|
async def edit_message(text):
|
|
|
|
|
"""修改提示消息"""
|
2022-09-17 16:28:51 +00:00
|
|
|
|
async with lock:
|
2022-10-10 11:07:28 +00:00
|
|
|
|
if message is not None and time_() >= (the_time.value + INTERVAL):
|
2023-07-18 09:29:31 +00:00
|
|
|
|
try:
|
2022-10-07 05:02:49 +00:00
|
|
|
|
await message.edit_text(
|
2022-10-10 11:07:28 +00:00
|
|
|
|
"\n".join(message.text_html.split("\n")[:2] + [text]), parse_mode=ParseMode.HTML
|
2022-10-07 05:02:49 +00:00
|
|
|
|
)
|
2022-09-17 17:33:11 +00:00
|
|
|
|
the_time.value = time_()
|
2023-07-18 09:29:31 +00:00
|
|
|
|
except (TimedOut, RetryAfter):
|
|
|
|
|
pass
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
2022-10-07 05:02:49 +00:00
|
|
|
|
async def task(item_id, name, item_type):
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.debug("正在开始下载 %s 的图标素材", name)
|
2022-10-07 05:02:49 +00:00
|
|
|
|
await edit_message(f"正在搬运 <b>{name}</b> 的图标素材。。。")
|
|
|
|
|
asset: AssetsServiceType = getattr(self.assets_service, item_type)(item_id) # 获取素材对象
|
|
|
|
|
asset_list.append(asset.honey_id)
|
|
|
|
|
# 找到该素材对象的所有图标类型
|
|
|
|
|
# 并根据图标类型找到下载对应图标的函数
|
|
|
|
|
for icon_type in asset.icon_types:
|
|
|
|
|
await getattr(asset, icon_type)(True) # 执行下载函数
|
2022-12-25 13:26:08 +00:00
|
|
|
|
logger.debug("%s 的图标素材下载成功", name)
|
2022-10-07 05:02:49 +00:00
|
|
|
|
await edit_message(f"正在搬运 <b>{name}</b> 的图标素材。。。<b>成功!</b>")
|
|
|
|
|
|
|
|
|
|
for TYPE, ITEMS in HONEY_DATA.items(): # 遍历每个对象
|
2022-09-17 16:28:51 +00:00
|
|
|
|
task_list = []
|
2022-10-07 05:02:49 +00:00
|
|
|
|
new_items = []
|
|
|
|
|
for ID, DATA in ITEMS.items():
|
|
|
|
|
if (ITEM := [ID, DATA[1], TYPE]) not in new_items:
|
|
|
|
|
new_items.append(ITEM)
|
2022-10-22 07:03:59 +00:00
|
|
|
|
task_list.append(task(*ITEM))
|
2022-09-18 04:19:29 +00:00
|
|
|
|
await asyncio.gather(*task_list) # 等待所有任务执行完成
|
2023-04-25 14:35:58 +00:00
|
|
|
|
try:
|
|
|
|
|
await message.edit_text(
|
|
|
|
|
"\n".join(message.text_html.split("\n")[:2] + ["图标素材下载完成!"]), parse_mode=ParseMode.HTML
|
|
|
|
|
)
|
|
|
|
|
except RetryAfter as e:
|
|
|
|
|
await asyncio.sleep(e.retry_after)
|
|
|
|
|
await message.edit_text(
|
|
|
|
|
"\n".join(message.text_html.split("\n")[:2] + ["图标素材下载完成!"]), parse_mode=ParseMode.HTML
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug(e)
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
logger.info("图标素材下载完成")
|
2022-10-07 05:02:49 +00:00
|
|
|
|
return the_time.value
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ItemData(BaseModel):
|
2022-09-18 04:19:29 +00:00
|
|
|
|
id: str # ID
|
|
|
|
|
name: str # 名称
|
|
|
|
|
rarity: int # 星级
|
|
|
|
|
icon: str # 图标
|
|
|
|
|
level: Optional[int] = None # 等级
|
|
|
|
|
constellation: Optional[int] = None # 命座
|
2022-10-28 07:58:07 +00:00
|
|
|
|
skills: Optional[List[int]] = None # 天赋等级
|
|
|
|
|
gid: Optional[int] = None # 角色在 genshin.py 里的 ID
|
2022-09-18 04:19:29 +00:00
|
|
|
|
refinement: Optional[int] = None # 精炼度
|
|
|
|
|
c_path: Optional[str] = None # 武器使用者图标
|
2022-11-22 10:36:52 +00:00
|
|
|
|
origin: Optional[Character] = None # 原始数据
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AreaData(BaseModel):
|
2023-08-21 08:35:01 +00:00
|
|
|
|
name: Literal["蒙德", "璃月", "稻妻", "须弥", "枫丹"] # 区域名
|
2022-09-20 15:00:08 +00:00
|
|
|
|
material_name: str # 区域的材料系列名
|
2022-09-18 04:19:29 +00:00
|
|
|
|
materials: List[ItemData] = [] # 区域材料
|
|
|
|
|
items: Iterable[ItemData] = [] # 可培养的角色或武器
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RenderData(BaseModel):
|
2022-09-18 04:19:29 +00:00
|
|
|
|
title: str # 页面标题,主要用于显示星期几
|
|
|
|
|
time: str # 页面时间
|
|
|
|
|
uid: Optional[int] = None # 用户UID
|
|
|
|
|
character: List[AreaData] = [] # 角色数据
|
|
|
|
|
weapon: List[AreaData] = [] # 武器数据
|
2022-09-17 16:28:51 +00:00
|
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
|
return self.__getattribute__(item)
|