PamGram/modules/wiki/base.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.1 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:
2023-06-06 14:33:50 +00:00
BASE_URL = "https://starrail-res.paimon.vip/data/"
2023-04-26 08:48:05 +00:00
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()