PamGram/modules/wiki/relic.py

47 lines
1.4 KiB
Python
Raw Normal View History

2023-04-26 08:48:05 +00:00
from typing import List, Dict, Optional
from modules.wiki.base import WikiModel
2023-08-29 06:03:21 +00:00
from modules.wiki.models.relic import YattaRelic
2023-04-26 08:48:05 +00:00
class Relic(WikiModel):
relic_url = WikiModel.BASE_URL + "relics.json"
relic_path = WikiModel.BASE_PATH / "relics.json"
def __init__(self):
super().__init__()
2023-08-29 06:03:21 +00:00
self.all_relics: List[YattaRelic] = []
self.all_relics_map: Dict[int, YattaRelic] = {}
self.all_relics_name: Dict[str, YattaRelic] = {}
2023-04-26 08:48:05 +00:00
def clear_class_data(self) -> None:
self.all_relics.clear()
self.all_relics_map.clear()
self.all_relics_name.clear()
async def refresh(self):
datas = await self.remote_get(self.relic_url)
await self.dump(datas.json(), self.relic_path)
await self.read()
async def read(self):
if not self.relic_path.exists():
await self.refresh()
return
datas = await WikiModel.read(self.relic_path)
self.clear_class_data()
for data in datas:
2023-08-29 06:03:21 +00:00
m = YattaRelic(**data)
2023-04-26 08:48:05 +00:00
self.all_relics.append(m)
self.all_relics_map[m.id] = m
self.all_relics_name[m.name] = m
2023-08-29 06:03:21 +00:00
def get_by_id(self, cid: int) -> Optional[YattaRelic]:
2023-05-11 15:09:39 +00:00
return self.all_relics_map.get(cid)
2023-04-26 08:48:05 +00:00
2023-08-29 06:03:21 +00:00
def get_by_name(self, name: str) -> Optional[YattaRelic]:
2023-05-11 15:09:39 +00:00
return self.all_relics_name.get(name)
2023-04-26 08:48:05 +00:00
def get_name_list(self) -> List[str]:
return list(self.all_relics_name.keys())