2022-03-14 02:53:47 -04:00
|
|
|
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
|
|
|
|
2022-04-02 11:49:22 -04:00
|
|
|
# TODO: some sort of caching?
|
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-04-02 12:16:42 -04:00
|
|
|
async def get_cards(self, encounter=False) -> list[ArkhamDBCard]:
|
|
|
|
async with self._session.get(
|
|
|
|
self.origin + "/api/public/cards/",
|
|
|
|
params={"encounter": "1"} if encounter else {},
|
|
|
|
) as resp:
|
2022-03-14 02:53:47 -04:00
|
|
|
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()
|
2022-09-10 23:21:16 -04:00
|
|
|
next_deck_id = deck.get("next_deck")
|
2021-05-02 23:22:52 -04:00
|
|
|
|
|
|
|
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
|