PamGram/core/dependence/assets.py

236 lines
8.5 KiB
Python
Raw Normal View History

import asyncio
from pathlib import Path
from ssl import SSLZeroReturnError
2023-04-27 12:25:06 +00:00
from typing import Optional, List, Dict
from aiofiles import open as async_open
2023-04-27 12:25:06 +00:00
from httpx import AsyncClient, HTTPError
from core.base_service import BaseService
2023-04-27 12:25:06 +00:00
from modules.wiki.base import WikiModel
from modules.wiki.models.avatar_config import AvatarIcon
from modules.wiki.models.light_cone_config import LightConeIcon
from utils.const import PROJECT_ROOT
from utils.log import logger
2023-04-27 12:25:06 +00:00
from utils.typedefs import StrOrURL, StrOrInt
2022-10-10 03:37:58 +00:00
ASSETS_PATH = PROJECT_ROOT.joinpath("resources/assets")
ASSETS_PATH.mkdir(exist_ok=True, parents=True)
2023-04-27 12:25:06 +00:00
DATA_MAP = {
"avatar": WikiModel.BASE_URL + "avatar_icons.json",
"light_cone": WikiModel.BASE_URL + "light_cone_icons.json",
}
class AssetsServiceError(Exception):
pass
class AssetsCouldNotFound(AssetsServiceError):
def __init__(self, message: str, target: str):
self.message = message
self.target = target
super().__init__(f"{message}: target={message}")
2023-04-27 12:25:06 +00:00
class _AssetsService:
client: Optional[AsyncClient] = None
def __init__(self, client: Optional[AsyncClient] = None) -> None:
2023-04-27 12:25:06 +00:00
self.client = client
2023-04-27 12:25:06 +00:00
async def _download(self, url: StrOrURL, path: Path, retry: int = 5) -> Optional[Path]:
"""从 url 下载图标至 path"""
logger.debug("正在从 %s 下载图标至 %s", url, path)
2023-03-31 06:01:03 +00:00
headers = None
for time in range(retry):
try:
response = await self.client.get(url, follow_redirects=False, headers=headers)
except Exception as error: # pylint: disable=W0703
if not isinstance(error, (HTTPError, SSLZeroReturnError)):
logger.error(error) # 打印未知错误
if time != retry - 1: # 未达到重试次数
await asyncio.sleep(1)
else:
raise error
continue
if response.status_code != 200: # 判定页面是否正常
return None
2022-10-10 03:37:58 +00:00
async with async_open(path, "wb") as file:
await file.write(response.content) # 保存图标
return path.resolve()
2023-04-27 12:25:06 +00:00
class _AvatarAssets(_AssetsService):
path: Path
data: List[AvatarIcon]
name_map: Dict[str, AvatarIcon]
id_map: Dict[int, AvatarIcon]
def __init__(self, client: Optional[AsyncClient] = None) -> None:
super().__init__(client)
self.path = ASSETS_PATH.joinpath("avatar")
self.path.mkdir(exist_ok=True, parents=True)
async def initialize(self):
logger.info("正在初始化角色素材图标")
html = await self.client.get(DATA_MAP["avatar"])
self.data = [AvatarIcon(**data) for data in html.json()]
self.name_map = {icon.name: icon for icon in self.data}
self.id_map = {icon.id: icon for icon in self.data}
tasks = []
for icon in self.data:
base_path = self.path / f"{icon.id}"
base_path.mkdir(exist_ok=True, parents=True)
gacha_path = base_path / "gacha.webp"
icon_path = base_path / "icon.webp"
normal_path = base_path / "normal.webp"
2023-04-28 12:03:38 +00:00
square_path = base_path / "square.png"
2023-04-27 12:25:06 +00:00
if not gacha_path.exists():
tasks.append(self._download(icon.gacha, gacha_path))
if not icon_path.exists():
tasks.append(self._download(icon.icon_, icon_path))
if not normal_path.exists():
tasks.append(self._download(icon.normal, normal_path))
2023-04-28 12:03:38 +00:00
if not square_path.exists() and icon.square:
tasks.append(self._download(icon.square, square_path))
2023-04-27 12:25:06 +00:00
if len(tasks) >= 100:
await asyncio.gather(*tasks)
tasks = []
if tasks:
await asyncio.gather(*tasks)
logger.info("角色素材图标初始化完成")
2023-04-28 12:03:38 +00:00
def get_path(self, icon: AvatarIcon, name: str, ext: str = "webp") -> Path:
2023-04-27 12:25:06 +00:00
path = self.path / f"{icon.id}"
path.mkdir(exist_ok=True, parents=True)
2023-04-28 12:03:38 +00:00
return path / f"{name}.{ext}"
2023-04-27 12:25:06 +00:00
def get_by_id(self, id_: int) -> Optional[AvatarIcon]:
return self.id_map.get(id_, None)
def get_by_name(self, name: str) -> Optional[AvatarIcon]:
return self.name_map.get(name, None)
2023-04-28 12:03:38 +00:00
def get_target(self, target: StrOrInt) -> AvatarIcon:
data = None
2023-04-27 12:25:06 +00:00
if isinstance(target, int):
2023-04-28 12:03:38 +00:00
data = self.get_by_id(target)
2023-04-27 12:25:06 +00:00
elif isinstance(target, str):
2023-04-28 12:03:38 +00:00
data = self.get_by_name(target)
if data is None:
raise AssetsCouldNotFound("角色素材图标不存在", target)
return data
2023-04-27 12:25:06 +00:00
def gacha(self, target: StrOrInt) -> Path:
icon = self.get_target(target)
return self.get_path(icon, "gacha")
2023-04-27 12:25:06 +00:00
def icon(self, target: StrOrInt) -> Path:
icon = self.get_target(target)
return self.get_path(icon, "icon")
2023-04-27 12:25:06 +00:00
def normal(self, target: StrOrInt) -> Path:
icon = self.get_target(target)
return self.get_path(icon, "normal")
2023-04-28 12:03:38 +00:00
def square(self, target: StrOrInt, allow_icon: bool = True) -> Path:
icon = self.get_target(target)
path = self.get_path(icon, "square", "png")
if not path.exists():
if allow_icon:
return self.get_path(icon, "icon")
raise AssetsCouldNotFound("角色素材图标不存在", target)
return path
2023-04-27 12:25:06 +00:00
class _LightConeAssets(_AssetsService):
path: Path
data: List[LightConeIcon]
name_map: Dict[str, LightConeIcon]
id_map: Dict[int, LightConeIcon]
def __init__(self, client: Optional[AsyncClient] = None) -> None:
super().__init__(client)
2023-04-27 12:25:06 +00:00
self.path = ASSETS_PATH.joinpath("light_cone")
self.path.mkdir(exist_ok=True, parents=True)
async def initialize(self):
logger.info("正在初始化光锥素材图标")
html = await self.client.get(DATA_MAP["light_cone"])
self.data = [LightConeIcon(**data) for data in html.json()]
self.name_map = {icon.name: icon for icon in self.data}
self.id_map = {icon.id: icon for icon in self.data}
tasks = []
for icon in self.data:
base_path = self.path / f"{icon.id}"
base_path.mkdir(exist_ok=True, parents=True)
gacha_path = base_path / "gacha.webp"
icon_path = base_path / "icon.webp"
if not gacha_path.exists():
tasks.append(self._download(icon.gacha, gacha_path))
if not icon_path.exists():
tasks.append(self._download(icon.icon_, icon_path))
if len(tasks) >= 100:
await asyncio.gather(*tasks)
tasks = []
if tasks:
await asyncio.gather(*tasks)
logger.info("光锥素材图标初始化完成")
def get_path(self, icon: LightConeIcon, name: str) -> Path:
path = self.path / f"{icon.id}"
path.mkdir(exist_ok=True, parents=True)
return path / f"{name}.webp"
def get_by_id(self, id_: int) -> Optional[LightConeIcon]:
return self.id_map.get(id_, None)
def get_by_name(self, name: str) -> Optional[LightConeIcon]:
return self.name_map.get(name, None)
def get_target(self, target: StrOrInt) -> Optional[LightConeIcon]:
if isinstance(target, int):
return self.get_by_id(target)
elif isinstance(target, str):
return self.get_by_name(target)
return None
2023-04-27 12:25:06 +00:00
def gacha(self, target: StrOrInt) -> Path:
icon = self.get_target(target)
if icon is None:
raise AssetsCouldNotFound("光锥素材图标不存在", target)
return self.get_path(icon, "gacha")
def icon(self, target: StrOrInt) -> Path:
icon = self.get_target(target)
if icon is None:
raise AssetsCouldNotFound("光锥素材图标不存在", target)
return self.get_path(icon, "icon")
class AssetsService(BaseService.Dependence):
"""asset服务
用于储存和管理 asset :
当对应的 asset (如某角色图标)不存在时该服务会先查找本地
若本地不存在则从网络上下载若存在则返回其路径
"""
2023-04-27 12:25:06 +00:00
client: Optional[AsyncClient] = None
avatar: _AvatarAssets
"""角色"""
2023-04-27 12:25:06 +00:00
light_cone: _LightConeAssets
"""光锥"""
def __init__(self):
2023-04-27 12:25:06 +00:00
self.client = AsyncClient(timeout=60.0)
self.avatar = _AvatarAssets(self.client)
self.light_cone = _LightConeAssets(self.client)
2023-04-27 12:25:06 +00:00
async def initialize(self): # pylint: disable=W0221
await self.avatar.initialize()
await self.light_cone.initialize()