Skip to content

Commit

Permalink
Add vtesdecks.com as deck provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-panhaleux committed Jan 6, 2025
1 parent 7e1cb47 commit 9406b72
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
4.4 (unreleased)
----------------

- Nothing changed yet.
- Add vtesdecks.com as deck provider


4.3 (2024-10-05)
Expand Down
24 changes: 24 additions & 0 deletions krcg/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ def from_vdb(cls, uid: str):
ret[vtes.VTES[int(cid)]] = count
return ret

@classmethod
def from_vtesdecks(cls, uid: str):
"""Fetch a deck from VTESDecks."""
r = requests.get("https://api.vtesdecks.com/1.0/decks/" + uid)
r.raise_for_status()
r = r.json()
logger.debug("VTESDecks replied: %s", r)
ret = cls(id=uid, author=r.get("author", r.get("owner", None)))
ret.name = r.get("name", None)
ret.comments = r.get("description", "")
ret.date = datetime.datetime.fromisoformat(r.get("modifyDate")).date()
if not vtes.VTES:
vtes.VTES.load()
for card in itertools.chain(r["crypt"], r["library"]):
count = int(card["number"])
if count <= 0:
continue
ret[vtes.VTES[int(card["id"])]] = count
return ret

@classmethod
def from_url(cls, url: str):
"""Fetch from any deckbuilding website"""
Expand Down Expand Up @@ -140,6 +160,10 @@ def from_url(cls, url: str):
elif result.path.startswith("/decks/"):
return cls.from_vdb(result.path[7:])
raise ValueError("Unknown VDB URL format")
elif result.netloc == "vtesdecks.com":
if not result.path.startswith("/deck"):
raise ValueError("Unknown VTESDecks URL path")
return cls.from_vtesdecks(result.path[5:])
raise ValueError("Unknown deck URL provider")

def check(self) -> bool:
Expand Down
106 changes: 106 additions & 0 deletions tests/test_deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,112 @@ def test_from_vdb():
}


def test_from_vtesdecks():
d = deck.Deck.from_vtesdecks("user-lionelpx-bf26e06e078348e8b5852d4e86dbdf6c")
assert d.to_json() == {
"id": "user-lionelpx-bf26e06e078348e8b5852d4e86dbdf6c",
"name": "Test",
"author": "lionelpx",
"comments": "<p>Here goes my description!</p>",
"date": "2025-01-06",
"crypt": {
"cards": [
{
"count": 7,
"id": 200001,
"name": "Aabbt Kindred",
},
{
"count": 6,
"id": 201520,
"name": "Nefertiti (ADV)",
},
],
"count": 13,
},
"library": {
"cards": [
{
"cards": [
{
"count": 2,
"id": 100588,
"name": "Dreams of the Sphinx",
},
{
"count": 8,
"id": 100667,
"name": "The Eternals of Sirius",
},
{
"count": 6,
"id": 102121,
"name": "Villein",
},
],
"count": 16,
"type": "Master",
},
{
"cards": [
{
"count": 12,
"id": 100650,
"name": "Enticement",
},
],
"count": 12,
"type": "Action",
},
{
"cards": [
{
"count": 6,
"id": 100769,
"name": "Forgotten Labyrinth",
},
{
"count": 6,
"id": 101001,
"name": "Into Thin Air",
},
],
"count": 12,
"type": "Action Modifier",
},
{
"cards": [
{
"count": 6,
"id": 100518,
"name": "Deflection",
},
{
"count": 6,
"id": 101321,
"name": "On the Qui Vive",
},
],
"count": 12,
"type": "Reaction",
},
{
"cards": [
{
"count": 12,
"id": 100973,
"name": "Indomitability",
},
],
"count": 12,
"type": "Combat",
},
],
"count": 64,
},
}


def test_deck_to_vdb():
TWDA = twda._TWDA()
with open(os.path.join(os.path.dirname(__file__), "2010tcdbng.html")) as f:
Expand Down

0 comments on commit 9406b72

Please sign in to comment.