Skip to content

Commit

Permalink
feat: phone themes
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Mar 29, 2024
1 parent d2f1956 commit 07403cf
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fix_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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__":
Expand Down
2 changes: 2 additions & 0 deletions func/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions models/phone_theme.py
Original file line number Diff line number Diff line change
@@ -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 changes: 70 additions & 0 deletions res_func/phone_theme.py
Original file line number Diff line number Diff line change
@@ -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("手机主题数据获取完成")
8 changes: 8 additions & 0 deletions res_func/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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/"

0 comments on commit 07403cf

Please sign in to comment.