2022-06-22 06:14:31 +00:00
|
|
|
import aiohttp
|
|
|
|
import sys
|
2022-07-05 08:46:47 +00:00
|
|
|
import logging
|
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-05 06:41:16 +00:00
|
|
|
from .json import Config
|
|
|
|
from .utils import create_path, VERSION, validate_uid
|
2022-06-22 06:14:31 +00:00
|
|
|
class EnkaNetworkAPI:
|
2022-07-05 06:41:16 +00:00
|
|
|
USER_AGENT = "EnkaNetwork.py/{version} (Python {major}.{minor}.{micro})"
|
2022-07-05 08:46:47 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def __init__(self, lang: str = "en", debug: bool = False) -> None:
|
|
|
|
# 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
|
|
|
|
Config.set_languege(lang)
|
2022-07-05 08:46:47 +00:00
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
Config.load_json_data()
|
|
|
|
Config.load_json_lang()
|
2022-06-22 06:14:31 +00:00
|
|
|
|
|
|
|
async def __get_headers(self):
|
|
|
|
# Get python version
|
|
|
|
python_version = sys.version_info
|
|
|
|
|
|
|
|
return {
|
|
|
|
"User-Agent": self.USER_AGENT.format(
|
2022-07-05 06:41:16 +00:00
|
|
|
version=VERSION,
|
2022-06-22 06:14:31 +00:00
|
|
|
major=python_version.major,
|
|
|
|
minor=python_version.minor,
|
|
|
|
micro=python_version.micro
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
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
|
|
|
async with aiohttp.ClientSession(headers=await self.__get_headers()) as session:
|
|
|
|
# Fetch JSON data
|
|
|
|
resp = await session.request(method="GET", url=create_path(f"u/{uid}/__data.json"))
|
|
|
|
|
|
|
|
# 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-05 06:41:16 +00:00
|
|
|
# Parse JSON data
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
raise UIDNotFounded(f"UID {uid} not found.")
|
2022-06-22 06:14:31 +00:00
|
|
|
|
2022-07-05 08:46:47 +00:00
|
|
|
self.LOGGER.debug("Got data from EnkaNetwork.")
|
|
|
|
self.LOGGER.debug(f"Raw data: {data}")
|
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
# Cleanup
|
|
|
|
await session.close()
|
2022-06-22 06:14:31 +00:00
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
# Return data
|
2022-07-05 08:46:47 +00:00
|
|
|
self.LOGGER.debug("Parsing data...")
|
2022-07-05 06:41:16 +00:00
|
|
|
return EnkaNetworkResponse.parse_obj(data)
|