2022-08-04 13:56:23 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
import re
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
from typing import Any, Dict, TYPE_CHECKING
|
|
|
|
|
2022-08-13 06:46:33 +00:00
|
|
|
from . import __version__
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from aiohttp import ClientResponse
|
|
|
|
|
2022-07-17 16:45:24 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2022-07-05 06:41:16 +00:00
|
|
|
|
|
|
|
# Base URL
|
2022-07-15 16:36:34 +00:00
|
|
|
BASE_URL = "https://enka.network/{PATH}"
|
2022-07-05 06:41:16 +00:00
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
# Request
|
2022-08-19 19:53:02 +00:00
|
|
|
CHUNK_SIZE = 5 * 2**20
|
2022-07-12 19:55:54 +00:00
|
|
|
RETRY_MAX = 10
|
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
def create_path(path: str) -> str:
|
|
|
|
return BASE_URL.format(PATH=path)
|
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
def create_ui_path(filename: str) -> str:
|
|
|
|
return create_path(f"ui/{filename}.png")
|
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-07-05 06:41:16 +00:00
|
|
|
def validate_uid(uid: str) -> bool:
|
2022-07-12 19:55:54 +00:00
|
|
|
"""
|
|
|
|
Validate UID
|
|
|
|
"""
|
2022-08-17 04:54:16 +00:00
|
|
|
return len(uid) == 9 and uid.isdigit() and re.match(r"([1,2,5-9])\d{8}", uid)
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-07-12 19:55:54 +00:00
|
|
|
|
|
|
|
def get_default_header():
|
2022-07-29 04:31:21 +00:00
|
|
|
# Get python version
|
|
|
|
python_version = sys.version_info
|
|
|
|
|
|
|
|
return {
|
2022-08-17 04:54:16 +00:00
|
|
|
"User-Agent": "EnkaNetwork.py/{version} (Python {major}.{minor}.{micro})".format(
|
2022-08-13 06:46:33 +00:00
|
|
|
version=__version__,
|
2022-07-29 04:31:21 +00:00
|
|
|
major=python_version.major,
|
|
|
|
minor=python_version.minor,
|
|
|
|
micro=python_version.micro
|
|
|
|
),
|
|
|
|
}
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
class _MissingSentinel:
|
|
|
|
__slots__ = ()
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
return False
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
def __bool__(self):
|
|
|
|
return False
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return 0
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '...'
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
MISSING: Any = _MissingSentinel()
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
async def to_data(response: ClientResponse) -> Dict[str, Any]:
|
2022-07-29 04:31:21 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
data = bytearray()
|
|
|
|
data_to_read = True
|
|
|
|
while data_to_read:
|
|
|
|
red = 0
|
|
|
|
while red < CHUNK_SIZE:
|
|
|
|
chunk = await response.content.read(CHUNK_SIZE - red)
|
2022-07-12 19:55:54 +00:00
|
|
|
|
2022-08-04 13:56:23 +00:00
|
|
|
if not chunk:
|
|
|
|
data_to_read = False
|
|
|
|
break
|
|
|
|
|
|
|
|
data.extend(chunk)
|
|
|
|
red += len(chunk)
|
|
|
|
|
|
|
|
content = {
|
|
|
|
"status": response.status,
|
2022-08-15 20:17:07 +00:00
|
|
|
"content": data
|
2022-08-04 13:56:23 +00:00
|
|
|
}
|
|
|
|
return content
|
2022-07-12 19:55:54 +00:00
|
|
|
|