EnkaNetwork.py/enkanetwork/client.py

82 lines
2.8 KiB
Python
Raw Normal View History

2022-07-05 08:46:47 +00:00
import logging
2022-07-12 19:55:54 +00:00
import os
import json
2022-06-22 06:14:31 +00:00
from typing import Union
2022-06-22 06:14:31 +00:00
from .model import EnkaNetworkResponse
from .exception import VaildateUIDError, UIDNotFounded
2022-07-16 19:35:08 +00:00
from .assets import Assets
2022-07-12 19:55:54 +00:00
from .utils import create_path, validate_uid, request
2022-07-18 08:54:24 +00:00
from .enum import Language
2022-07-12 19:55:54 +00:00
2022-07-29 04:31:21 +00:00
2022-06-22 06:14:31 +00:00
class EnkaNetworkAPI:
2022-07-05 08:46:47 +00:00
LOGGER = logging.getLogger(__name__)
2022-07-29 04:31:21 +00:00
RAWDATA = "https://raw.githubusercontent.com/mrwan200/enkanetwork.py-data/{PATH}" # noqa: E501
2022-07-05 08:46:47 +00:00
2022-07-29 04:31:21 +00:00
def __init__(self, lang: str = "en", debug: bool = False, key: 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
# 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
# Key
self.__key = key
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)
async def set_language(self, lang: Language) -> None:
self.lang = lang
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}...")
if not validate_uid(str(uid)):
2022-07-29 04:31:21 +00:00
raise VaildateUIDError("Validate UID failed. Please check your UID.") # noqa: E501
2022-07-05 08:46:47 +00:00
self.LOGGER.debug(f"Fetching user with UID {uid}...")
2022-07-29 04:31:21 +00:00
resp = await request(url=create_path(f"u/{uid}/__data.json" + ("?key={key}" if self.__key else ""))) # noqa: E501
2022-07-12 19:55:54 +00:00
# Check if status code is not 200 (Ex. 500)
if resp["status"] != 200:
raise UIDNotFounded(f"UID {uid} not found.")
2022-06-22 06:14:31 +00:00
2022-07-12 19:55:54 +00:00
# Parse JSON data
data = resp["content"]
2022-07-29 04:31:21 +00:00
2022-07-12 19:55:54 +00:00
if not data:
raise UIDNotFounded(f"UID {uid} not found.")
self.LOGGER.debug("Got data from EnkaNetwork.")
self.LOGGER.debug(f"Raw data: {data}")
# Return data
self.LOGGER.debug("Parsing data...")
return EnkaNetworkResponse.parse_obj(data)
2022-06-22 06:14:31 +00:00
async def update_assets(self) -> None:
2022-07-12 19:55:54 +00:00
self.LOGGER.debug("Downloading new content...")
_PATH = Assets._get_path_assets()
2022-07-12 19:55:54 +00:00
for folder in _PATH:
for filename in os.listdir(_PATH[folder]):
self.LOGGER.debug(f"Downloading {folder} file {filename}...")
_json = await request(
2022-07-29 04:31:21 +00:00
url=self.RAWDATA.format(PATH=f"master/exports/{folder}/{filename}") # noqa: E501
2022-07-12 19:55:54 +00:00
)
2022-07-05 08:46:47 +00:00
2022-07-12 19:55:54 +00:00
self.LOGGER.debug(f"Writing {folder} file {filename}...")
2022-07-29 04:31:21 +00:00
with open(os.path.join(_PATH[folder], filename), "w", encoding="utf-8") as f: # noqa: E501
json.dump(_json["content"], f, ensure_ascii=False, indent=4) # noqa: E501
2022-06-22 06:14:31 +00:00
# Reload config
2022-07-29 04:31:21 +00:00
self.assets.reload_assets()