HonkaiStarRailWikiDataParser/res_func/yatta/avatar.py

82 lines
2.7 KiB
Python
Raw Normal View History

2023-07-05 08:42:21 +00:00
import asyncio
import json
from pathlib import Path
from typing import List
import aiofiles
import ujson
2023-08-29 05:15:55 +00:00
from func.client import retry
from func.data import all_avatars
2023-07-20 05:07:56 +00:00
from models.avatar import YattaAvatar
2023-07-05 08:42:21 +00:00
from res_func.client import client
2023-07-20 05:07:56 +00:00
from res_func.url import avatar_skill_url
2023-07-05 08:42:21 +00:00
avatar_data = {}
avatars_skills_icons = {}
avatars_skills_path = Path("data/skill")
avatars_skills_path.mkdir(exist_ok=True, parents=True)
2023-07-18 15:53:05 +00:00
@retry
2023-07-05 08:42:21 +00:00
async def get_single_avatar_skill_icon(url: str, real_path: str) -> None:
req = await client.get(url)
try:
req.raise_for_status()
2023-07-18 15:53:05 +00:00
except Exception as e:
2023-07-05 08:42:21 +00:00
print(f"{url} 获取技能图片失败")
2023-07-18 15:53:05 +00:00
raise e
2023-07-05 08:42:21 +00:00
async with aiofiles.open(f"data/skill/{real_path}", "wb") as f:
await f.write(req.content)
if "8001" in real_path:
real_path = real_path.replace("8001", "8002")
async with aiofiles.open(f"data/skill/{real_path}", "wb") as f:
await f.write(req.content)
elif "8003" in real_path:
real_path = real_path.replace("8003", "8004")
async with aiofiles.open(f"data/skill/{real_path}", "wb") as f:
await f.write(req.content)
async def dump_icons():
final_data = dict(sorted(avatar_data.items(), key=lambda x: x[0]))
2023-08-29 05:15:55 +00:00
async with aiofiles.open(
"data/avatar_eidolon_icons.json", "w", encoding="utf-8"
) as f:
2023-07-05 08:42:21 +00:00
await f.write(ujson.dumps(final_data, indent=4, ensure_ascii=False))
async def get_all_avatars() -> None:
print("开始获取星魂图片")
2023-07-20 05:07:56 +00:00
for avatar in all_avatars:
urls = [i.icon_url for i in avatar.eidolons]
avatar_data[str(avatar.id)] = urls
2023-07-05 08:42:21 +00:00
await dump_icons()
print("获取星魂图片成功")
2023-07-20 05:07:56 +00:00
print("开始获取技能图片")
await get_all_avatars_skills_icons(all_avatars)
print("获取技能图片成功")
2023-07-05 08:42:21 +00:00
2023-07-20 05:07:56 +00:00
async def get_all_avatars_skills_icons(avatars: List[YattaAvatar]):
2023-07-05 08:42:21 +00:00
remote_path = ["Normal", "BP", "Passive", "Maze", "Ultra"]
local_path = ["basic_atk", "skill", "talent", "technique", "ultimate"]
tasks = []
2023-07-20 05:07:56 +00:00
for avatar in avatars:
2023-11-14 16:00:44 +00:00
if avatar.id in [8002, 8004, 1311]:
2023-07-05 08:42:21 +00:00
continue
for i in range(len(remote_path)):
tasks.append(
get_single_avatar_skill_icon(
2023-07-20 05:07:56 +00:00
f"{avatar_skill_url}SkillIcon_{avatar.id}_{remote_path[i]}.png",
2023-08-29 05:15:55 +00:00
f"{avatar.id}_{local_path[i]}.png",
2023-07-05 08:42:21 +00:00
)
)
await asyncio.gather(*tasks)
tasks.clear()
datas = [file.name.split(".")[0] for file in avatars_skills_path.glob("*")]
2023-08-29 05:15:55 +00:00
async with aiofiles.open(
avatars_skills_path / "info.json", "w", encoding="utf-8"
) as f:
2023-07-05 08:42:21 +00:00
await f.write(json.dumps(datas, indent=4, ensure_ascii=False))