2024-07-05 14:10:36 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import List, Dict
|
2022-08-28 14:37:31 +00:00
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
import aiofiles
|
|
|
|
import ujson as jsonlib
|
|
|
|
from httpx import AsyncClient
|
2023-10-08 10:52:23 +00:00
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
class WikiModel:
|
|
|
|
BASE_URL = "https://zzz-res.paimon.vip/data/"
|
|
|
|
BASE_PATH = Path("data/wiki-zzz")
|
|
|
|
BASE_PATH.mkdir(parents=True, exist_ok=True)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.client = AsyncClient(timeout=120.0)
|
2022-08-28 14:37:31 +00:00
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
async def remote_get(self, url: str):
|
|
|
|
return await self.client.get(url)
|
2022-08-28 14:37:31 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-07-05 14:10:36 +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))
|
2022-08-28 14:37:31 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2024-07-05 14:10:36 +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
|
2022-08-28 14:37:31 +00:00
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
@staticmethod
|
|
|
|
async def save_file(data, path: Path):
|
|
|
|
async with aiofiles.open(path, "wb") as f:
|
|
|
|
await f.write(data)
|
2022-08-28 14:37:31 +00:00
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
@staticmethod
|
|
|
|
async def read_file(path: Path):
|
|
|
|
async with aiofiles.open(path, "rb") as f:
|
|
|
|
return await f.read()
|