genshin-wiki/scripts/item.py

96 lines
3.3 KiB
Python
Raw Permalink Normal View History

2023-05-01 13:40:50 +00:00
from pathlib import Path
2023-05-01 13:31:48 +00:00
import ujson as json
from aiofiles import open as async_open
2023-08-21 11:27:53 +00:00
from model.enums import FoodQuality, ItemType
2023-05-01 13:31:48 +00:00
from model.item import Food, Item, Material, Namecard
from utils.const import PROJECT_ROOT
2023-04-29 14:59:16 +00:00
from utils.manager import ResourceManager
from utils.typedefs import Lang
2023-05-01 13:31:48 +00:00
OUT_DIR = PROJECT_ROOT.joinpath("out")
2023-04-29 14:59:16 +00:00
# noinspection PyShadowingBuiltins
2023-05-01 13:40:50 +00:00
async def parse_item_data(
lang: Lang,
) -> tuple[Path, list[Item | Material | Food | Namecard]]:
2023-05-01 13:31:48 +00:00
out_path = OUT_DIR.joinpath(f"{lang}")
out_path.mkdir(exist_ok=True, parents=True)
2023-04-29 14:59:16 +00:00
manager = ResourceManager(lang=lang)
json_data = manager.fetch("MaterialExcelConfigData")
2023-05-01 13:31:48 +00:00
item_list = []
for data in filter(lambda x: "rankLevel" in x, json_data):
2023-04-29 14:59:16 +00:00
id = data["id"]
2023-08-21 12:53:04 +00:00
if (name := manager.get_text(data["nameTextMapHash"])) is None:
2023-05-01 13:31:48 +00:00
continue
2023-04-29 14:59:16 +00:00
type = manager.get_text(data["typeDescTextMapHash"])
icon = data["icon"]
rarity = data["rankLevel"]
2023-05-01 13:31:48 +00:00
description = manager.get_text(data["descTextMapHash"]) or ""
2023-04-29 14:59:16 +00:00
special_description = manager.get_text(data["specialDescTextMapHash"])
2023-05-01 13:31:48 +00:00
item_type = ItemType(data["itemType"].removeprefix("ITEM_"))
kwargs = {
"id": id,
"name": name,
"type": type,
"icon": icon,
"rarity": rarity,
"description": description,
"special_description": special_description,
"item_type": item_type,
}
try:
if "picPath" in data and data["picPath"]:
pictures = data["picPath"]
item = Namecard(pictures=pictures, **kwargs)
elif "materialType" in data:
2023-08-21 11:27:53 +00:00
# material_type = MaterialType(
# data["materialType"].removeprefix("MATERIAL_")
# )
# item = Material(material_type=material_type, **kwargs)
material_type = data["materialType"].removeprefix("MATERIAL_")
item = Material(
material_type=material_type,
material_type_description=manager.get_text(
data["typeDescTextMapHash"]
)
or "",
**kwargs,
2023-05-01 13:31:48 +00:00
)
elif "foodQuality" in data:
quality = FoodQuality(data["foodQuality"])
effect = manager.get_text(data["effectDescTextMapHash"])
effect_icon = data["effectIcon"]
effect_name = data["effectName"]
item = Food(
quality=quality,
effect=effect,
effect_icon=effect_icon,
effect_name=effect_name,
**kwargs,
)
else:
item = Item(**kwargs)
except Exception as e:
breakpoint()
raise e
item_list.append(item)
async with async_open(out_path / "item.json", encoding="utf-8", mode="w") as file:
await file.write(
json.dumps(
[i.dict(exclude_none=True) for i in item_list],
2023-05-01 13:31:48 +00:00
ensure_ascii=False,
encode_html_chars=False,
2023-05-01 13:40:50 +00:00
indent=4,
2023-05-01 13:31:48 +00:00
),
)
2023-05-01 13:40:50 +00:00
return out_path, item_list