2022-03-14 02:53:47 -04:00
|
|
|
from collections.abc import Iterator
|
|
|
|
from typing import TypedDict, Optional, Union, Any
|
2021-05-02 23:22:52 -04:00
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
|
2022-03-14 01:08:14 -04:00
|
|
|
ARKHAMDB_ORIGIN = "https://arkhamdb.adamgoldsmith.name"
|
2021-05-02 23:22:52 -04:00
|
|
|
|
2022-03-14 02:53:47 -04:00
|
|
|
ArkhamDBCard = Any
|
|
|
|
|
2021-05-02 23:22:52 -04:00
|
|
|
|
2022-03-14 00:40:14 -04:00
|
|
|
class ArkhamDBDeck(TypedDict):
|
|
|
|
id: int
|
|
|
|
name: str
|
|
|
|
date_creation: str
|
|
|
|
date_update: str
|
|
|
|
description_md: str
|
|
|
|
user_id: Optional[str]
|
|
|
|
investigator_code: str
|
|
|
|
investigator_name: str
|
|
|
|
slots: dict[str, int]
|
|
|
|
sideSlots: Union[list[str], dict[str, int]]
|
|
|
|
ignoreDeckLimitSlots: Optional[str]
|
|
|
|
version: str
|
2022-03-14 03:00:34 -04:00
|
|
|
xp: Optional[int]
|
|
|
|
xp_spent: Optional[int]
|
2022-03-14 00:40:14 -04:00
|
|
|
xp_adjustment: int
|
|
|
|
exile_string: Optional[str]
|
|
|
|
taboo_id: int
|
|
|
|
meta: str
|
|
|
|
tags: str
|
|
|
|
previous_deck: int
|
|
|
|
next_deck: int
|
|
|
|
problem: Optional[str]
|
2021-05-02 23:22:52 -04:00
|
|
|
|
2022-03-14 01:08:14 -04:00
|
|
|
|
2021-05-02 23:22:52 -04:00
|
|
|
class ArkhamDBClient:
|
2021-05-02 23:40:57 -04:00
|
|
|
_session: aiohttp.ClientSession
|
2021-05-02 23:22:52 -04:00
|
|
|
|
|
|
|
origin: str
|
|
|
|
|
2022-03-14 01:04:06 -04:00
|
|
|
def __init__(self, arkhamdb_origin: str = ARKHAMDB_ORIGIN) -> None:
|
2021-05-02 23:40:57 -04:00
|
|
|
self._session = aiohttp.ClientSession()
|
2021-05-02 23:22:52 -04:00
|
|
|
self.origin = arkhamdb_origin
|
|
|
|
|
2022-03-14 01:04:06 -04:00
|
|
|
async def close(self) -> None:
|
2021-05-02 23:40:57 -04:00
|
|
|
await self._session.close()
|
|
|
|
|
2022-03-14 02:53:47 -04:00
|
|
|
async def get_cards(self) -> list[ArkhamDBCard]:
|
|
|
|
async with self._session.get(self.origin + "/api/public/cards/") as resp:
|
|
|
|
return await resp.json()
|
|
|
|
|
2021-05-02 23:22:52 -04:00
|
|
|
async def get_latest_deck(self, deck_id: int) -> ArkhamDBDeck:
|
|
|
|
next_deck_id = deck_id
|
|
|
|
deck = None
|
|
|
|
while deck is None or deck["next_deck"] is not None:
|
2021-05-02 23:40:57 -04:00
|
|
|
async with self._session.get(
|
2022-03-14 01:08:14 -04:00
|
|
|
self.origin + f"/api/public/deck/{next_deck_id}.json"
|
|
|
|
) as resp:
|
2021-05-02 23:22:52 -04:00
|
|
|
deck = await resp.json()
|
|
|
|
next_deck_id = deck["next_deck"]
|
|
|
|
|
|
|
|
return deck
|
|
|
|
|
2022-03-14 01:08:14 -04:00
|
|
|
async def get_latest_decks(
|
|
|
|
self, deck_ids: dict[int, str]
|
|
|
|
) -> dict[int, tuple[str, ArkhamDBDeck]]:
|
2021-05-02 23:22:52 -04:00
|
|
|
latest_decks: dict[int, tuple[str, ArkhamDBDeck]] = {}
|
|
|
|
for deck_id, prefix in deck_ids.items():
|
|
|
|
try:
|
|
|
|
deck = await self.get_latest_deck(deck_id)
|
2022-03-14 01:08:14 -04:00
|
|
|
latest_decks[deck["id"]] = (prefix, deck)
|
2021-05-02 23:22:52 -04:00
|
|
|
except aiohttp.ContentTypeError:
|
|
|
|
# TODO: json was invalid, should probably alert user
|
|
|
|
pass
|
|
|
|
|
|
|
|
return latest_decks
|