新增 角色培养素材查询 功能

This commit is contained in:
omg-xtao 2022-08-31 14:48:15 +08:00 committed by GitHub
parent 65cf31e68b
commit f27cf0d427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 116 additions and 12 deletions

View File

@ -1,11 +1,16 @@
from utils.redisdb import RedisDB from utils.redisdb import RedisDB
from utils.service.manager import listener_service from utils.service.manager import listener_service
from .cache import GameStrategyCache from .cache import GameCache
from .services import GameStrategyService from .services import GameStrategyService, GameMaterialService
@listener_service() @listener_service()
def create_game_strategy_service(redis: RedisDB): def create_game_strategy_service(redis: RedisDB):
_cache = GameStrategyCache(redis) _cache = GameCache(redis, "game:strategy")
_service = GameStrategyService(_cache) return GameStrategyService(_cache)
return _service
@listener_service()
def create_game_material_service(redis: RedisDB):
_cache = GameCache(redis, "game:material")
return GameMaterialService(_cache)

View File

@ -3,10 +3,10 @@ from typing import List
from utils.redisdb import RedisDB from utils.redisdb import RedisDB
class GameStrategyCache: class GameCache:
def __init__(self, redis: RedisDB, ttl: int = 3600): def __init__(self, redis: RedisDB, qname: str, ttl: int = 3600):
self.client = redis.client self.client = redis.client
self.qname = "game:strategy" self.qname = qname
self.ttl = ttl self.ttl = ttl
async def get_url_list(self, character_name: str): async def get_url_list(self, character_name: str):

View File

@ -1,12 +1,11 @@
from typing import List, Optional from typing import List, Optional
from models.apihelper.hyperion import Hyperion from models.apihelper.hyperion import Hyperion
from .cache import GameStrategyCache from .cache import GameCache
class GameStrategyService: class GameStrategyService:
def __init__(self, cache: GameCache, collections: Optional[List[int]] = None):
def __init__(self, cache: GameStrategyCache, collections: Optional[List[int]] = None):
self._cache = cache self._cache = cache
self._hyperion = Hyperion() self._hyperion = Hyperion()
if collections is None: if collections is None:
@ -44,3 +43,27 @@ class GameStrategyService:
artwork_info = await self._hyperion.get_artwork_info(2, post_id) artwork_info = await self._hyperion.get_artwork_info(2, post_id)
await self._cache.set_url_list(character_name, artwork_info.results.image_url_list) await self._cache.set_url_list(character_name, artwork_info.results.image_url_list)
return artwork_info.results.image_url_list[0] return artwork_info.results.image_url_list[0]
class GameMaterialService(GameStrategyService):
def __init__(self, cache: GameCache, collections: Optional[List[int]] = None):
super().__init__(cache, collections)
self._collections = [428421, 1164644] if collections is None else collections
self._special = ['雷电将军', '珊瑚宫心海', '菲谢尔', '托马', '八重神子', '九条裟罗', '辛焱', '神里绫华']
async def get_material(self, character_name: str) -> str:
cache = await self._cache.get_url_list(character_name)
if len(cache) >= 1:
return cache[-1]
for collection_id in self._collections:
post_id = await self._get_strategy_from_hyperion(collection_id, character_name)
if post_id != -1:
break
else:
return ""
artwork_info = await self._hyperion.get_artwork_info(2, post_id)
await self._cache.set_url_list(character_name, artwork_info.results.image_url_list)
image_url_list = artwork_info.results.image_url_list
return image_url_list[2] if character_name in self._special else image_url_list[1]

View File

@ -0,0 +1,65 @@
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.constants import ChatAction, ParseMode
from telegram.ext import filters, ConversationHandler, CommandHandler, MessageHandler, CallbackContext
from core.game.services import GameMaterialService
from logger import Log
from plugins.base import BasePlugins
from utils.bot import get_all_args
from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts
from utils.helpers import url_to_file
from utils.plugins.manager import listener_plugins_class
from utils.service.inject import inject
@listener_plugins_class()
class Material(BasePlugins):
"""角色培养素材查询"""
KEYBOARD = [[InlineKeyboardButton(
text="查看角色培养素材列表并查询",
switch_inline_query_current_chat="查看角色培养素材列表并查询")]]
@inject
def __init__(self, game_material_service: GameMaterialService = None):
self.game_material_service = game_material_service
@classmethod
def create_handlers(cls) -> list:
material = cls()
return [
CommandHandler("material", material.command_start, block=False),
MessageHandler(filters.Regex("^角色培养素材查询(.*)"), material.command_start, block=False),
]
@error_callable
@restricts(return_data=ConversationHandler.END)
async def command_start(self, update: Update, context: CallbackContext) -> None:
message = update.message
user = update.effective_user
args = get_all_args(context)
if len(args) >= 1:
character_name = args[0]
else:
reply_message = await message.reply_text("请回复你要查询的培养素材的角色名",
reply_markup=InlineKeyboardMarkup(self.KEYBOARD))
if filters.ChatType.GROUPS.filter(reply_message):
self._add_delete_message_job(context, message.chat_id, message.message_id)
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
return
url = await self.game_material_service.get_material(character_name)
if not url:
reply_message = await message.reply_text(f"没有找到 {character_name} 的培养素材",
reply_markup=InlineKeyboardMarkup(self.KEYBOARD))
if filters.ChatType.GROUPS.filter(reply_message):
self._add_delete_message_job(context, message.chat_id, message.message_id)
self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id)
return
Log.info(f"用户 {user.full_name}[{user.id}] 查询角色培养素材命令请求 || 参数 {character_name}")
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
file_path = await url_to_file(url, "")
caption = "Form 米游社 友人A " \
f"查看 [原图]({url})"
await message.reply_photo(photo=open(file_path, "rb"), caption=caption, filename=f"{character_name}.png",
allow_sending_without_reply=True, parse_mode=ParseMode.MARKDOWN_V2)

View File

@ -50,8 +50,19 @@ class Inline:
input_message_content=InputTextMessageContent(f"角色攻略查询{role_name}", input_message_content=InputTextMessageContent(f"角色攻略查询{role_name}",
parse_mode=ParseMode.MARKDOWN_V2) parse_mode=ParseMode.MARKDOWN_V2)
)) ))
elif "查看角色培养素材列表并查询" == args[0]:
characters_list = await self.wiki_service.get_characters_name_list()
for role_name in characters_list:
results_list.append(
InlineQueryResultArticle(
id=str(uuid4()),
title=role_name,
description=f"查看角色培养素材列表并查询 {role_name}",
input_message_content=InputTextMessageContent(f"角色培养素材查询{role_name}",
parse_mode=ParseMode.MARKDOWN_V2)
))
if len(results_list) == 0: if not results_list:
results_list.append( results_list.append(
InlineQueryResultArticle( InlineQueryResultArticle(
id=str(uuid4()), id=str(uuid4()),