onepiece-tcg-notion-importer/onepiece_tcg/models.py

134 lines
3.9 KiB
Python

import json
import logging
from typing import Dict, Iterator
class Card(object):
global_id: str
card_id: str
name: str
type: str
color: str
source: str
rarity: str
image_url: str
alternate_art: str
# gid: str # GlobalID: "47",
# cid: str # CardID: "OP01-047",
# n: str # Name: "Trafalgar Law",
# t: str # Type: "2",
# col: str # Color: "7",
# cs: str # Source: "5",
# tr: str # "Supernovas\/ Heart Pirates",
# a: str # AttackType: "1",
# p: str # Power: "6000",
# cp: str # null,
# l: dict # null,
# r: str # Rarity: "5",
# ar: str # null,
# iu: str # ImageURL: "https:\/\/onepiece-cardgame.dev\/images\/cards\/OP01-047_616aca_jp.jpg",
# e: str # Effect: "<Blocker>\r\n[On Play] You may return one of your Characters to your hand: Play 1 Cost 3 or lower Character Card from your hand.",
# al: dict # Alternate Art: "P1",
# intl: str # International: "0",
# srcN: str # SourceName: "Romance Dawn [OP-01]",
# srcD: dict # null
def __init__(
self,
global_id,
card_id,
name,
type,
color,
source,
rarity,
image_url,
alternate_art,
):
self.global_id = global_id
self.card_id = card_id
self.name = name
self.type = type
self.color = color
self.source = source
self.rarity = rarity
self.image_url = image_url
self.alternate_art = alternate_art
def __repr__(self):
return json.dumps(self.__dict__)
def to_notion(self):
result = {
"Name": {"title": [{"text": {"content": self.name}}]},
"Rarity": {"type": "select", "select": {"name": self.rarity}},
"Card ID": {
"type": "rich_text",
"rich_text": [
{
"type": "text",
"text": {"content": self.card_id},
},
],
},
}
if self.source:
result["Source"] = {"type": "select", "select": {"name": self.source}}
return result
class Database:
data: Dict[str, Dict[str, Dict]]
def __init__(self):
self.data = dict()
def _retrieve_property(self, _type, _id, _property="name"):
try:
return self.data[_type][_id][_property]
except KeyError as e:
logging.error(f"Error trying to retrieve <{_type}.{_id}.{_property}>: {e} ")
return None
def _register(self, _type: str, _id: str, **kwargs):
if _type not in self.data:
self.data[_type] = dict()
self.data[_type][_id] = kwargs
def register_card(self, _id: str, **kwargs):
self._register("card", _id, **kwargs)
def register_card_type(self, _id: str, **kwargs):
self._register("card_type", _id, **kwargs)
def register_type(self, _id: str, **kwargs):
self._register("type", _id, **kwargs)
def register_rarity(self, _id: str, **kwargs):
self._register("rarity", _id, **kwargs)
def register_source(self, _id: str, **kwargs):
self._register("source", _id, **kwargs)
def register_color(self, _id: str, **kwargs):
self._register("color", _id, **kwargs)
def register_card(self, _id: str, **kwargs):
self._register("card", _id, **kwargs)
def iter_cards(self) -> Iterator:
for cid, card in self.data["card"].items():
yield Card(
global_id=card["gid"],
card_id=cid,
name=card["n"],
type=self._retrieve_property("type", card["t"]),
color=self._retrieve_property("color", card["col"]),
source=self._retrieve_property("source", card["cs"]),
rarity=self._retrieve_property("rarity", card["r"]),
image_url=card["iu"],
alternate_art=card["al"],
)