diff --git a/enkanetwork/cache.py b/enkanetwork/cache.py index d1f5bf7..911f1f4 100644 --- a/enkanetwork/cache.py +++ b/enkanetwork/cache.py @@ -1,16 +1,17 @@ import json +from typing import Any, Dict from cachetools import TTLCache __all__ = ('Cache',) class Cache: - def __init__(self, maxsize, ttl): + def __init__(self, maxsize: int, ttl: int) -> None: self.cache = TTLCache(maxsize, ttl) - async def get(self, key): + async def get(self, key) -> Dict[str, Any]: data = self.cache.get(key) return json.loads(data) if data is not None else data - async def set(self, key, value): + async def set(self, key, value) -> None: self.cache[key] = json.dumps(value) diff --git a/enkanetwork/client.py b/enkanetwork/client.py index d5c238c..17ec3a5 100644 --- a/enkanetwork/client.py +++ b/enkanetwork/client.py @@ -1,6 +1,9 @@ from __future__ import annotations +import os +import json import logging + from .http import HTTPClient from .model import EnkaNetworkResponse from .assets import Assets @@ -95,11 +98,18 @@ class EnkaNetworkAPI: print("Updating assets...") self.LOGGER.debug("Downloading new content...") - # get path path = Assets._get_path_assets() + for folder in path: + for filename in os.listdir(path[folder]): + self.LOGGER.debug(f"Downloading {folder} file {filename}...") - # update assets - await self.__http.update_asset(path) + data = await self.__http.update_asset(folder, filename) + + self.LOGGER.debug(f"Writing {folder} file {filename}...") + + # dumps to json file + with open(os.path.join(path[folder], filename), "w", encoding="utf-8") as f: + json.dump(data["content"], f, ensure_ascii=False, indent=4) # Reload config self.assets.reload_assets() diff --git a/enkanetwork/http.py b/enkanetwork/http.py index b2f149a..87aa867 100644 --- a/enkanetwork/http.py +++ b/enkanetwork/http.py @@ -43,7 +43,7 @@ class Route: path: str, endpoint: str = 'enka', uid: Optional[str] = None, - ): + ) -> None: self.method = method self.uid = uid self.url = '' @@ -59,8 +59,8 @@ class HTTPClient: def __init__(self, *, key: str = '', agent: str = '') -> None: self.__session: aiohttp.ClientSession = MISSING self.__headers: Dict = {} - self.__agent = agent - self.__key = key + self.__agent: str = agent + self.__key: str = key async def close(self) -> None: if self.__session is not MISSING: @@ -107,7 +107,7 @@ class HTTPClient: self.LOGGER.warning(f"Failure to fetch {url} ({response.status}) Retry {tries} / {RETRY_MAX}") if tries > RETRY_MAX: raise HTTPException(f"Failed to download {url}") - await asyncio.sleep(1 + tries * 2) # 1 + tries * 2 + await asyncio.sleep(1) # 1 + tries * 2 continue if response.status == 403: @@ -139,20 +139,8 @@ class HTTPClient: url = f'/u/{uid}/__data.json' + ("?key={key}" if self.__key else "") return self.request(Route('GET', url, 'enka', uid)) - async def update_asset(self, path: dict) -> NoReturn: + def update_asset(self, folder: str, filename: str) -> NoReturn: + # get new assets + url = f"master/exports/{folder}/{filename}" + return self.request(Route('GET', url, 'assets')) - self.LOGGER.debug("Downloading new content...") - - for folder in path: - for filename in os.listdir(path[folder]): - self.LOGGER.debug(f"Downloading {folder} file {filename}...") - - # get new assets - url = f"master/exports/{folder}/{filename}" - data = await self.request(Route('GET', url, 'assets')) - - self.LOGGER.debug(f"Writing {folder} file {filename}...") - - # dumps to json file - with open(os.path.join(path[folder], filename), "w", encoding="utf-8") as f: - json.dump(data["content"], f, ensure_ascii=False, indent=4) diff --git a/enkanetwork/model/stats.py b/enkanetwork/model/stats.py index 9c792c8..0ba8545 100644 --- a/enkanetwork/model/stats.py +++ b/enkanetwork/model/stats.py @@ -17,7 +17,7 @@ class StatsPercentage(BaseModel): id: int = 0 value: float = 0.0 - def to_percentage(self) -> int: + def to_percentage(self) -> float: return round((round(self.value, 3) * 100), 1) def to_percentage_symbol(self) -> str: diff --git a/enkanetwork/utils.py b/enkanetwork/utils.py index f2f6513..c79fc03 100644 --- a/enkanetwork/utils.py +++ b/enkanetwork/utils.py @@ -1,9 +1,7 @@ from __future__ import annotations import re -import aiohttp import logging -import asyncio import json import sys