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
|
|
|
|
2022-07-05 06:41:16 +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-06-22 06:14:31 +00:00
|
|
|
class EnkaNetworkAPI:
|
2022-07-05 08:46:47 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
# JSON Data & Language
|
|
|
|
RAWDATA = "https://raw.githubusercontent.com/mrwan200/enkanetwork.py-data/{PATH}"
|
|
|
|
|
|
|
|
def __init__(self, lang: str = "en", debug: bool = False, key: str = "") -> None:
|
2022-07-05 08:46:47 +00:00
|
|
|
# Logging
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.getLogger("enkanetwork").setLevel(logging.DEBUG if debug else logging.ERROR)
|
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
|
|
|
|
|
|
|
# Key
|
|
|
|
self.__key = key
|
2022-06-22 06:14:31 +00:00
|
|
|
|
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-07-05 06:41:16 +00:00
|
|
|
if not validate_uid(str(uid)):
|
2022-06-22 06:14:31 +00:00
|
|
|
raise VaildateUIDError("Validate UID failed. Please check your UID.")
|
2022-07-05 08:46:47 +00:00
|
|
|
|
|
|
|
self.LOGGER.debug(f"Fetching user with UID {uid}...")
|
2022-07-05 06:41:16 +00:00
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
resp = await request(url=create_path(f"u/{uid}/__data.json" + ("?key={key}" if self.__key else "")))
|
|
|
|
|
|
|
|
# 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-05 06:41:16 +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
|
|
|
|
2022-07-17 06:08:16 +00:00
|
|
|
async def update_assets(self) -> None:
|
2022-07-12 19:55:54 +00:00
|
|
|
self.LOGGER.debug("Downloading new content...")
|
2022-07-17 06:08:16 +00:00
|
|
|
_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(
|
|
|
|
url=self.RAWDATA.format(PATH=f"master/exports/{folder}/{filename}")
|
|
|
|
)
|
2022-07-05 08:46:47 +00:00
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
self.LOGGER.debug(f"Writing {folder} file {filename}...")
|
|
|
|
with open(os.path.join(_PATH[folder], filename), "w", encoding="utf-8") as f:
|
|
|
|
json.dump(_json["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
|
|
|
|
self.assets.reload_assets()
|