From 07403cf09375f1e011ebde7f1beab8bdb268de14 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 29 Mar 2024 22:22:12 +0800 Subject: [PATCH] feat: phone themes --- fix_data.py | 2 ++ func/data.py | 2 ++ models/phone_theme.py | 23 ++++++++++++++ res_func/phone_theme.py | 70 +++++++++++++++++++++++++++++++++++++++++ res_func/url.py | 8 +++++ 5 files changed, 105 insertions(+) create mode 100644 models/phone_theme.py create mode 100644 res_func/phone_theme.py diff --git a/fix_data.py b/fix_data.py index 647ed56..5708110 100644 --- a/fix_data.py +++ b/fix_data.py @@ -6,6 +6,7 @@ from res_func.relic_res import fix_set_image from res_func.yatta.avatar import get_all_avatars from res_func.head_icon import get_head_icons +from res_func.phone_theme import get_phone_themes async def main(): @@ -15,6 +16,7 @@ async def main(): await fix_set_image() await get_all_avatars() await get_head_icons() + await get_phone_themes() if __name__ == "__main__": diff --git a/func/data.py b/func/data.py index 593885a..1c6a2c7 100644 --- a/func/data.py +++ b/func/data.py @@ -79,6 +79,8 @@ async def dump_materials(): async def read_materials(): + if all_materials: + return async with aiofiles.open(materials_path, "r", encoding="utf-8") as f: data = ujson.loads(await f.read()) for material in data: diff --git a/models/phone_theme.py b/models/phone_theme.py new file mode 100644 index 0000000..97b1c26 --- /dev/null +++ b/models/phone_theme.py @@ -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 diff --git a/res_func/phone_theme.py b/res_func/phone_theme.py new file mode 100644 index 0000000..73c7058 --- /dev/null +++ b/res_func/phone_theme.py @@ -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("手机主题数据获取完成") diff --git a/res_func/url.py b/res_func/url.py index 04bbfb1..2f735dc 100644 --- a/res_func/url.py +++ b/res_func/url.py @@ -11,6 +11,7 @@ avatar_player_icon_url = f"{base_data_url}ExcelOutput/AvatarPlayerIcon.json" player_icon_url = f"{base_data_url}ExcelOutput/PlayerIcon.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" @@ -27,4 +28,11 @@ material_yatta_url = f"{base_yatta_url}/hsr/v2/cn/item" relic_yatta_url = f"{base_yatta_url}/hsr/v2/cn/relic" + 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/"