81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import logging
|
|
import os
|
|
import sys
|
|
from .notion import NotionClient
|
|
from .download import get_dict_from_url_response_body
|
|
from .models import Database
|
|
|
|
if __name__ == "__main__":
|
|
logging.getLogger().setLevel(logging.INFO)
|
|
|
|
try:
|
|
token = os.environ["NOTION_TOKEN"]
|
|
database_id = os.environ["NOTION_DATABASE_ID"]
|
|
card_id_property = "Card ID"
|
|
except KeyError as e:
|
|
logging.error("NOTION_TOKEN and NOTION_DATABASE_ID are required")
|
|
sys.exit(1)
|
|
|
|
database = Database()
|
|
|
|
# Download and build Meta database
|
|
logging.info("Downloading meta...")
|
|
meta = get_dict_from_url_response_body("https://onepiece-cardgame.dev/meta.json")
|
|
|
|
# for attack in meta["a"]:
|
|
# database.register_attack(attack["atk_id"], **attack)
|
|
|
|
for color in meta["c"]:
|
|
database.register_color(color["color_id"], **color)
|
|
|
|
# for source in meta["s"]:
|
|
# database.register_source(source["src_id"], **source)
|
|
|
|
for _type in meta["t"]:
|
|
database.register_type(_type["type_id"], **_type)
|
|
|
|
for card_type in meta["ct"]:
|
|
database.register_card_type(card_type["type_id"], **card_type)
|
|
|
|
for rarity in meta["r"]:
|
|
database.register_rarity(rarity["rarity_id"], **rarity)
|
|
|
|
# for key in meta['key']:
|
|
# database.register_key(key['id'], **key)
|
|
|
|
# Download and build Sources database
|
|
logging.info("Downloading sources...")
|
|
sources = get_dict_from_url_response_body(
|
|
"https://onepiece-cardgame.dev/sources.json"
|
|
)
|
|
|
|
for source in sources:
|
|
database.register_source(source["src_id"], **source)
|
|
|
|
# Donwload and build Cards database
|
|
logging.info("Downloading cards...")
|
|
cards = get_dict_from_url_response_body("https://onepiece-cardgame.dev/cards.json")
|
|
|
|
for card in cards:
|
|
database.register_card(card["cid"], **card)
|
|
|
|
# Fetch current data from Notion
|
|
notion_cli = NotionClient(token)
|
|
notion_state = dict()
|
|
|
|
try:
|
|
logging.info("Retrieving notion state...")
|
|
notion_db = notion_cli.get_database_state(database_id)
|
|
for card in notion_db["results"]:
|
|
notion_state[card[card_id_property]] = card
|
|
except Exception as e:
|
|
logging.error(e)
|
|
sys.exit(2)
|
|
|
|
logging.info("Merging databases...")
|
|
for card in database.iter_cards():
|
|
if card.card_id in notion_state:
|
|
notion_cli.update_card(notion_state[card.card_id], **{})
|
|
else:
|
|
notion_cli.create_card(database_id, **card.to_notion())
|