Skip to content

Commit

Permalink
🐛 Fix weapon icon
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed May 26, 2024
1 parent d34bb74 commit 9e87a19
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
7 changes: 6 additions & 1 deletion func/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
async def get_item_base(item_path: str, item_type: Type[BaseModel], fix_func=None):
req = await client.get(f"{BASE_URL}/{item_path}")
req.raise_for_status()
data = fix_func(req.json()) if fix_func else req.json()
data = await fix_func(req.json()) if fix_func else req.json()
item_data = item_type(**data)
return item_data

Expand All @@ -25,3 +25,8 @@ def save_data(item_path: str, item_datas: List[BaseModel]):
item_data = [i.dict(by_alias=True) for i in item_datas]
with path.open("w", encoding="utf-8") as f:
json.dump(item_data, f, ensure_ascii=False, indent=4)


async def have_url(url: str) -> bool:
req = await client.head(url)
return req.status_code == 200
2 changes: 1 addition & 1 deletion func/character.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async def get_characters():
return await get_item_base("character", EncoreAvatars)


def fix_character(data: Dict[str, Any]) -> Dict[str, Any]:
async def fix_character(data: Dict[str, Any]) -> Dict[str, Any]:
data["Name"] = data["Name"]["Content"]
return data

Expand Down
23 changes: 22 additions & 1 deletion func/weapon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict, Any

from .base import get_item_base, save_data
from models.weapon import EncoreWeapon, EncoreWeapons

Expand All @@ -6,8 +8,27 @@ async def get_weapons():
return await get_item_base("weapon", EncoreWeapons)


def get_url_ext(url: str) -> str:
return url.split(".")[-1]


def replace_url_ext(url: str, ext: str) -> str:
return ".".join(url.split(".")[:-1]) + f".{ext}"


async def fix_weapon_icon(data: Dict[str, Any]) -> Dict[str, Any]:
for k in ["Icon", "IconMiddle", "IconSmall"]:
if data[k] and data[k].startswith("/"):
data[k] = f"https://api.encore.moe/resource/Data{data[k]}"
if get_url_ext(data[k]) == "png":
continue
new_url = replace_url_ext(data[k], "png")
data[k] = new_url
return data


async def get_weapon(weapon_id: int):
return await get_item_base(f"weapon/{weapon_id}", EncoreWeapon)
return await get_item_base(f"weapon/{weapon_id}", EncoreWeapon, fix_func=fix_weapon_icon)


async def main():
Expand Down

0 comments on commit 9e87a19

Please sign in to comment.