Skip to content

Commit

Permalink
refactor: Migrate to enka-py v2
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Jun 27, 2024
1 parent bf9aa41 commit c52e053
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
8 changes: 5 additions & 3 deletions enka_to_go/go/converter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any

from .maps import GO_EQUIPMENT_TYPE_MAP, GO_STAT_KEY_MAP

if TYPE_CHECKING:
from enka.models import Character, Talent
from enka.gi import Character, Talent


class EnkaToGOConverter:
Expand All @@ -12,7 +14,7 @@ def _format_key(cls, key: str) -> str:
return key.replace("'", "").replace('"', "").replace("-", " ").title().replace(" ", "")

@classmethod
def _get_talent_levels(cls, talents: list["Talent"], talent_order: list[int]) -> list[int]:
def _get_talent_levels(cls, talents: list[Talent], talent_order: list[int]) -> list[int]:
talent_levels: list[int] = [1, 1, 1]
for i, talent_id in enumerate(talent_order):
talent = next((t for t in talents if t.id == talent_id), None)
Expand All @@ -23,7 +25,7 @@ def _get_talent_levels(cls, talents: list["Talent"], talent_order: list[int]) ->
return talent_levels

@classmethod
def convert(cls, characters: list["Character"]) -> dict[str, Any]:
def convert(cls, characters: list[Character]) -> dict[str, Any]:
base = {
"format": "GOOD",
"version": 2,
Expand Down
2 changes: 1 addition & 1 deletion enka_to_go/go/maps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enka import EquipmentType, StatType
from enka.gi import EquipmentType, StatType

GO_EQUIPMENT_TYPE_MAP: dict[EquipmentType, str] = {
EquipmentType.FLOWER: "flower",
Expand Down
19 changes: 7 additions & 12 deletions enka_to_go/web_app/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import json
import logging
from typing import TYPE_CHECKING

import enka
import flet as ft
from loguru import logger

from ..go.converter import EnkaToGOConverter

if TYPE_CHECKING:
from enka import EnkaAPI

LOGGER_ = logging.getLogger(__name__)


class EnkaToGOWebApp:
def __init__(self, page: ft.Page, api: "EnkaAPI") -> None:
def __init__(self, page: ft.Page) -> None:
self.page = page
assert self.page.client_storage
self.storage = self.page.client_storage
self.api = api

# control refs
self.uid_text_field = ft.Ref[ft.TextField]()
Expand Down Expand Up @@ -59,9 +53,10 @@ async def _on_submit(self, _: ft.ControlEvent) -> None:

# fetch and convert data
try:
response = await self.api.fetch_showcase(uid)
async with enka.GenshinClient() as client:
response = await client.fetch_showcase(uid)
except Exception as e:
LOGGER_.exception("Failed to fetch data.")
logger.exception("Failed to fetch data.")
return await self.page.show_snack_bar_async(
ft.SnackBar(
ft.Text(f"Error: {e}", color=ft.colors.ON_ERROR_CONTAINER),
Expand All @@ -85,7 +80,7 @@ async def _on_submit(self, _: ft.ControlEvent) -> None:
try:
converted = EnkaToGOConverter.convert(response.characters)
except Exception:
LOGGER_.exception("Failed to convert data.")
logger.exception("Failed to convert data.")
return await self.page.show_snack_bar_async(
ft.SnackBar(
ft.Text(
Expand Down
12 changes: 1 addition & 11 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import asyncio
import logging
import sys

import enka
import flet as ft

from enka_to_go.web_app.main import EnkaToGOWebApp
Expand All @@ -16,18 +14,13 @@
],
)

loop = asyncio.get_event_loop()

api = enka.EnkaAPI(headers={"User-Agent": "EnkaToGO"})
loop.run_until_complete(api.start())


async def main(page: ft.Page) -> None:
page.title = "Enka to GO"
page.scroll = ft.ScrollMode.ADAPTIVE
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
web_app = EnkaToGOWebApp(page, api)
web_app = EnkaToGOWebApp(page)
await web_app.add_controls()


Expand All @@ -36,6 +29,3 @@ async def main(page: ft.Page) -> None:
view=None if sys.platform == "linux" else ft.AppView.WEB_BROWSER,
port=7091,
)

loop.run_until_complete(api.close())
loop.close()
4 changes: 3 additions & 1 deletion update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@


async def update_data() -> None:
async with enka.EnkaAPI() as api:
async with enka.GenshinClient() as api:
await api.update_assets()
async with enka.HSRClient() as api:
await api.update_assets()


Expand Down

0 comments on commit c52e053

Please sign in to comment.