feat: phone themes

This commit is contained in:
xtaodada 2024-03-29 22:22:12 +08:00
parent d2f19569fc
commit 07403cf093
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
5 changed files with 105 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from res_func.relic import fetch_relic_config
from res_func.relic_res import fix_set_image from res_func.relic_res import fix_set_image
from res_func.yatta.avatar import get_all_avatars from res_func.yatta.avatar import get_all_avatars
from res_func.head_icon import get_head_icons from res_func.head_icon import get_head_icons
from res_func.phone_theme import get_phone_themes
async def main(): async def main():
@ -15,6 +16,7 @@ async def main():
await fix_set_image() await fix_set_image()
await get_all_avatars() await get_all_avatars()
await get_head_icons() await get_head_icons()
await get_phone_themes()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -79,6 +79,8 @@ async def dump_materials():
async def read_materials(): async def read_materials():
if all_materials:
return
async with aiofiles.open(materials_path, "r", encoding="utf-8") as f: async with aiofiles.open(materials_path, "r", encoding="utf-8") as f:
data = ujson.loads(await f.read()) data = ujson.loads(await f.read())
for material in data: for material in data:

23
models/phone_theme.py Normal file
View File

@ -0,0 +1,23 @@
from typing import Optional, List
from pydantic import BaseModel
class PhoneTheme(BaseModel):
id: int
"""ID"""
name: str
"""名称"""
description: str
"""描述"""
story: Optional[str] = None
"""故事"""
urls: List[str]
# 原始数据
class PhoneThemeConfig(BaseModel):
ID: int
PhoneThemeMain: str

70
res_func/phone_theme.py Normal file
View File

@ -0,0 +1,70 @@
from pathlib import Path
from typing import List
import aiofiles
import ujson
from models.phone_theme import PhoneTheme, PhoneThemeConfig
from func.data import all_materials_map, read_materials
from .base_data import get_base_data
from .client import client
from .url import phone_theme_url, hoyoverse_game_url, mihoyo_game_url
data_path = Path("data")
async def get_phone_theme() -> List[PhoneThemeConfig]:
data = await get_base_data(phone_theme_url)
datas = []
for i in data.values():
datas.append(PhoneThemeConfig(**i))
return datas
async def test_url(base: str, path: str) -> str:
url = f"{base}{path}"
data = await client.head(url)
if data.status_code != 200:
return ""
return url
async def gen_phone_theme(themes: List[PhoneThemeConfig]) -> List[PhoneTheme]:
await read_materials()
datas = []
for theme in themes:
info = all_materials_map.get(theme.ID)
name, desc, story = "", "", ""
if info:
name = info.name
desc = info.description
story = info.story
h_url = await test_url(hoyoverse_game_url, theme.PhoneThemeMain)
m_url = await test_url(mihoyo_game_url, theme.PhoneThemeMain)
urls = [h_url, m_url]
datas.append(
PhoneTheme(
id=theme.ID,
name=name,
description=desc,
story=story,
urls=urls,
)
)
return datas
async def dump_themes(path: Path, datas: List[PhoneTheme]):
data = [theme.dict() for theme in datas]
data.sort(key=lambda x: x["id"])
async with aiofiles.open(path, "w", encoding="utf-8") as f:
await f.write(ujson.dumps(data, indent=4, ensure_ascii=False))
async def get_phone_themes():
print("获取手机主题数据")
themes = await get_phone_theme()
datas = await gen_phone_theme(themes)
await dump_themes(data_path / "phone_themes.json", datas)
print("手机主题数据获取完成")

View File

@ -11,6 +11,7 @@ relic_sub_affix_config = f"{base_data_url}ExcelOutput/RelicSubAffixConfig.json"
avatar_player_icon_url = f"{base_data_url}ExcelOutput/AvatarPlayerIcon.json" avatar_player_icon_url = f"{base_data_url}ExcelOutput/AvatarPlayerIcon.json"
player_icon_url = f"{base_data_url}ExcelOutput/PlayerIcon.json" player_icon_url = f"{base_data_url}ExcelOutput/PlayerIcon.json"
item_player_card_url = f"{base_data_url}ExcelOutput/ItemPlayerCard.json" item_player_card_url = f"{base_data_url}ExcelOutput/ItemPlayerCard.json"
phone_theme_url = f"{base_data_url}ExcelOutput/PhoneThemeConfig.json"
base_station_url = "https://starrailstation.com" base_station_url = "https://starrailstation.com"
@ -27,4 +28,11 @@ light_cone_yatta_url = f"{base_yatta_url}/hsr/v2/cn/equipment"
material_yatta_url = f"{base_yatta_url}/hsr/v2/cn/item" material_yatta_url = f"{base_yatta_url}/hsr/v2/cn/item"
relic_yatta_url = f"{base_yatta_url}/hsr/v2/cn/relic" relic_yatta_url = f"{base_yatta_url}/hsr/v2/cn/relic"
base_enka_url = "https://enka.network/ui/hsr/" base_enka_url = "https://enka.network/ui/hsr/"
base_hoyoverse_url = "https://act-webstatic.hoyoverse.com/"
hoyoverse_game_url = f"{base_hoyoverse_url}game_record/hkrpg/"
base_mihoyo_url = "https://act-webstatic.mihoyo.com/"
mihoyo_game_url = f"{base_mihoyo_url}game_record/hkrpg/"