2022-08-04 13:56:23 +00:00
|
|
|
from __future__ import annotations
|
2022-07-05 06:41:16 +00:00
|
|
|
|
2022-08-05 01:56:35 +00:00
|
|
|
import os
|
|
|
|
import json
|
2022-08-04 13:56:23 +00:00
|
|
|
import logging
|
2022-08-05 01:56:35 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
from .http import HTTPClient
|
2022-06-22 06:14:31 +00:00
|
|
|
from .model import EnkaNetworkResponse
|
2022-07-16 19:35:08 +00:00
|
|
|
from .assets import Assets
|
2022-07-18 08:54:24 +00:00
|
|
|
from .enum import Language
|
2022-08-02 17:37:24 +00:00
|
|
|
from .cache import Cache
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
from typing import Union, Optional, Type, TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from typing_extensions import Self
|
|
|
|
from types import TracebackType
|
|
|
|
|
|
|
|
__all__ = ("EnkaNetworkAPI",)
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-06-22 06:14:31 +00:00
|
|
|
class EnkaNetworkAPI:
|
2022-08-04 13:56:23 +00:00
|
|
|
|
2022-07-05 08:46:47 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
def __init__(self, lang: str = "en", *, debug: bool = False, key: str = "", cache: bool = True, agent: str = "") -> None: # noqa: E501
|
2022-07-05 08:46:47 +00:00
|
|
|
# Logging
|
|
|
|
logging.basicConfig()
|
2022-07-29 04:31:21 +00:00
|
|
|
logging.getLogger("enkanetwork").setLevel(logging.DEBUG if debug else logging.ERROR) # noqa: E501
|
2022-06-22 06:14:31 +00:00
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
# Set language and load config
|
2022-07-16 19:35:08 +00:00
|
|
|
self.assets = Assets(lang)
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-02 17:37:24 +00:00
|
|
|
# Cache
|
|
|
|
self._enable_cache = cache
|
|
|
|
self.cache = Cache(1024, 60 * 3)
|
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
# http client
|
|
|
|
self.__http = HTTPClient(key=key, agent=agent)
|
|
|
|
self._closed = False
|
2022-08-03 07:50:43 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
async def __aenter__(self) -> Self:
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def __aexit__(
|
|
|
|
self,
|
|
|
|
exc_type: Optional[Type[BaseException]],
|
|
|
|
exc_value: Optional[BaseException],
|
|
|
|
traceback: Optional[TracebackType],
|
|
|
|
) -> None:
|
|
|
|
self._close = True
|
|
|
|
if self._close:
|
|
|
|
await self.__http.close()
|
|
|
|
|
|
|
|
def is_closed(self) -> bool:
|
|
|
|
return self._closed
|
2022-06-22 06:14:31 +00:00
|
|
|
|
2022-07-18 08:54:24 +00:00
|
|
|
@property
|
|
|
|
def lang(self) -> Language:
|
|
|
|
return self.assets.LANGS
|
|
|
|
|
|
|
|
@lang.setter
|
|
|
|
def lang(self, lang: Language) -> None:
|
|
|
|
self.assets._set_language(lang)
|
|
|
|
|
2022-08-02 17:37:24 +00:00
|
|
|
def set_cache(self, cache: Cache) -> None:
|
|
|
|
self.cache = cache
|
|
|
|
|
2022-07-18 08:54:24 +00:00
|
|
|
async def set_language(self, lang: Language) -> None:
|
|
|
|
self.lang = lang
|
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
async def fetch_user(self, uid: Union[str, int]) -> EnkaNetworkResponse:
|
2022-07-05 08:46:47 +00:00
|
|
|
self.LOGGER.debug(f"Validating with UID {uid}...")
|
2022-08-04 13:56:23 +00:00
|
|
|
|
2022-08-02 17:37:24 +00:00
|
|
|
if self._enable_cache:
|
2022-08-04 17:26:19 +00:00
|
|
|
self.LOGGER.warn("Getting data from cache...")
|
2022-08-05 01:41:21 +00:00
|
|
|
data = await self.cache.get(uid)
|
2022-08-02 17:37:24 +00:00
|
|
|
|
|
|
|
if data is not None:
|
|
|
|
# Return data
|
|
|
|
self.LOGGER.debug("Parsing data...")
|
|
|
|
return EnkaNetworkResponse.parse_obj(data)
|
2022-08-02 17:40:30 +00:00
|
|
|
|
2022-08-05 01:41:21 +00:00
|
|
|
user = await self.__http.fetch_user(uid)
|
|
|
|
|
|
|
|
data = user["content"]
|
|
|
|
|
|
|
|
self.LOGGER.debug(f"Fetching user with UID {uid}...")
|
|
|
|
|
|
|
|
if self._enable_cache:
|
|
|
|
self.LOGGER.debug("Caching data...")
|
|
|
|
await self.cache.set(uid, data)
|
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
# Return data
|
|
|
|
self.LOGGER.debug("Parsing data...")
|
|
|
|
return EnkaNetworkResponse.parse_obj(data)
|
2022-06-22 06:14:31 +00:00
|
|
|
|
2022-07-17 06:08:16 +00:00
|
|
|
async def update_assets(self) -> None:
|
2022-08-04 13:56:23 +00:00
|
|
|
print("Updating assets...")
|
2022-07-12 19:55:54 +00:00
|
|
|
self.LOGGER.debug("Downloading new content...")
|
2022-08-04 13:56:23 +00:00
|
|
|
|
|
|
|
path = Assets._get_path_assets()
|
2022-08-05 01:56:35 +00:00
|
|
|
for folder in path:
|
|
|
|
for filename in os.listdir(path[folder]):
|
|
|
|
self.LOGGER.debug(f"Downloading {folder} file {filename}...")
|
|
|
|
|
|
|
|
data = await self.__http.update_asset(folder, filename)
|
|
|
|
|
|
|
|
self.LOGGER.debug(f"Writing {folder} file {filename}...")
|
2022-08-04 13:56:23 +00:00
|
|
|
|
2022-08-05 01:56:35 +00:00
|
|
|
# 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)
|
2022-06-22 06:14:31 +00:00
|
|
|
|
2022-07-17 06:08:16 +00:00
|
|
|
# Reload config
|
2022-07-29 04:31:21 +00:00
|
|
|
self.assets.reload_assets()
|