เก็บรายละเอียด

This commit is contained in:
Dee Dev 2022-08-05 08:56:35 +07:00
parent db30b678fd
commit 395c6c758c
5 changed files with 26 additions and 29 deletions

View File

@ -1,16 +1,17 @@
import json import json
from typing import Any, Dict
from cachetools import TTLCache from cachetools import TTLCache
__all__ = ('Cache',) __all__ = ('Cache',)
class Cache: class Cache:
def __init__(self, maxsize, ttl): def __init__(self, maxsize: int, ttl: int) -> None:
self.cache = TTLCache(maxsize, ttl) self.cache = TTLCache(maxsize, ttl)
async def get(self, key): async def get(self, key) -> Dict[str, Any]:
data = self.cache.get(key) data = self.cache.get(key)
return json.loads(data) if data is not None else data return json.loads(data) if data is not None else data
async def set(self, key, value): async def set(self, key, value) -> None:
self.cache[key] = json.dumps(value) self.cache[key] = json.dumps(value)

View File

@ -1,6 +1,9 @@
from __future__ import annotations from __future__ import annotations
import os
import json
import logging import logging
from .http import HTTPClient from .http import HTTPClient
from .model import EnkaNetworkResponse from .model import EnkaNetworkResponse
from .assets import Assets from .assets import Assets
@ -95,11 +98,18 @@ class EnkaNetworkAPI:
print("Updating assets...") print("Updating assets...")
self.LOGGER.debug("Downloading new content...") self.LOGGER.debug("Downloading new content...")
# get path
path = Assets._get_path_assets() path = Assets._get_path_assets()
for folder in path:
for filename in os.listdir(path[folder]):
self.LOGGER.debug(f"Downloading {folder} file {filename}...")
# update assets data = await self.__http.update_asset(folder, filename)
await self.__http.update_asset(path)
self.LOGGER.debug(f"Writing {folder} file {filename}...")
# 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)
# Reload config # Reload config
self.assets.reload_assets() self.assets.reload_assets()

View File

@ -43,7 +43,7 @@ class Route:
path: str, path: str,
endpoint: str = 'enka', endpoint: str = 'enka',
uid: Optional[str] = None, uid: Optional[str] = None,
): ) -> None:
self.method = method self.method = method
self.uid = uid self.uid = uid
self.url = '' self.url = ''
@ -59,8 +59,8 @@ class HTTPClient:
def __init__(self, *, key: str = '', agent: str = '') -> None: def __init__(self, *, key: str = '', agent: str = '') -> None:
self.__session: aiohttp.ClientSession = MISSING self.__session: aiohttp.ClientSession = MISSING
self.__headers: Dict = {} self.__headers: Dict = {}
self.__agent = agent self.__agent: str = agent
self.__key = key self.__key: str = key
async def close(self) -> None: async def close(self) -> None:
if self.__session is not MISSING: if self.__session is not MISSING:
@ -107,7 +107,7 @@ class HTTPClient:
self.LOGGER.warning(f"Failure to fetch {url} ({response.status}) Retry {tries} / {RETRY_MAX}") self.LOGGER.warning(f"Failure to fetch {url} ({response.status}) Retry {tries} / {RETRY_MAX}")
if tries > RETRY_MAX: if tries > RETRY_MAX:
raise HTTPException(f"Failed to download {url}") raise HTTPException(f"Failed to download {url}")
await asyncio.sleep(1 + tries * 2) # 1 + tries * 2 await asyncio.sleep(1) # 1 + tries * 2
continue continue
if response.status == 403: if response.status == 403:
@ -139,20 +139,8 @@ class HTTPClient:
url = f'/u/{uid}/__data.json' + ("?key={key}" if self.__key else "") url = f'/u/{uid}/__data.json' + ("?key={key}" if self.__key else "")
return self.request(Route('GET', url, 'enka', uid)) return self.request(Route('GET', url, 'enka', uid))
async def update_asset(self, path: dict) -> NoReturn: def update_asset(self, folder: str, filename: str) -> NoReturn:
# get new assets
url = f"master/exports/{folder}/{filename}"
return self.request(Route('GET', url, 'assets'))
self.LOGGER.debug("Downloading new content...")
for folder in path:
for filename in os.listdir(path[folder]):
self.LOGGER.debug(f"Downloading {folder} file {filename}...")
# get new assets
url = f"master/exports/{folder}/{filename}"
data = await self.request(Route('GET', url, 'assets'))
self.LOGGER.debug(f"Writing {folder} file {filename}...")
# 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)

View File

@ -17,7 +17,7 @@ class StatsPercentage(BaseModel):
id: int = 0 id: int = 0
value: float = 0.0 value: float = 0.0
def to_percentage(self) -> int: def to_percentage(self) -> float:
return round((round(self.value, 3) * 100), 1) return round((round(self.value, 3) * 100), 1)
def to_percentage_symbol(self) -> str: def to_percentage_symbol(self) -> str:

View File

@ -1,9 +1,7 @@
from __future__ import annotations from __future__ import annotations
import re import re
import aiohttp
import logging import logging
import asyncio
import json import json
import sys import sys