PamGram/modules/wiki/base.py

40 lines
1.2 KiB
Python
Raw Normal View History

2023-04-26 08:48:05 +00:00
from pathlib import Path
from typing import List, Dict
2023-04-26 08:48:05 +00:00
import aiofiles
import ujson as jsonlib
from httpx import AsyncClient
2023-04-26 08:48:05 +00:00
class WikiModel:
BASE_URL = "https://raw.githubusercontent.com/PaiGramTeam/HonkaiStarRailWikiDataParser/remote/data/"
BASE_PATH = Path("data/wiki")
BASE_PATH.mkdir(parents=True, exist_ok=True)
2023-04-26 08:48:05 +00:00
def __init__(self):
self.client = AsyncClient(timeout=120.0)
2023-04-26 08:48:05 +00:00
async def remote_get(self, url: str):
return await self.client.get(url)
@staticmethod
2023-04-26 08:48:05 +00:00
async def dump(datas, path: Path):
async with aiofiles.open(path, "w", encoding="utf-8") as f:
await f.write(jsonlib.dumps(datas, indent=4, ensure_ascii=False))
@staticmethod
2023-04-26 08:48:05 +00:00
async def read(path: Path) -> List[Dict]:
async with aiofiles.open(path, "r", encoding="utf-8") as f:
datas = jsonlib.loads(await f.read())
return datas
2023-04-26 08:48:05 +00:00
@staticmethod
async def save_file(data, path: Path):
async with aiofiles.open(path, "wb") as f:
await f.write(data)
2023-04-26 08:48:05 +00:00
@staticmethod
async def read_file(path: Path):
async with aiofiles.open(path, "rb") as f:
return await f.read()