Compare commits

...

2 commits

Author SHA1 Message Date
bc24b89d25 Adjust import warnings 2022-02-10 18:03:21 -05:00
1e275a8288 Handle nullable scryfall card fields 2022-02-10 17:03:03 -05:00
5 changed files with 50 additions and 43 deletions

View file

@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS `rarities` (
DELETE FROM `rarities`; DELETE FROM `rarities`;
INSERT INTO `rarities` (`rarity`, `rarity_ord`) VALUES INSERT INTO `rarities` (`rarity`, `rarity_ord`) VALUES
('n/a', 0),
('common', 1), ('common', 1),
('uncommon', 2), ('uncommon', 2),
('rare', 3), ('rare', 3),
@ -19,11 +20,11 @@ INSERT INTO `rarities` (`rarity`, `rarity_ord`) VALUES
CREATE TABLE IF NOT EXISTS `cards` ( CREATE TABLE IF NOT EXISTS `cards` (
`scryfall_id` TEXT PRIMARY KEY, `scryfall_id` TEXT PRIMARY KEY,
`oracle_id` TEXT NOT NULL, `oracle_id` TEXT,
`name` TEXT NOT NULL, `name` TEXT NOT NULL,
`set_code` TEXT NOT NULL, `set_code` TEXT,
`collector_number` TEXT NOT NULL, `collector_number` TEXT,
`release_date` TEXT NOT NULL, `release_date` TEXT,
`rarity` TEXT NOT NULL, `rarity` TEXT NOT NULL,
`color_identity` TEXT NOT NULL, `color_identity` TEXT NOT NULL,
`cmc` TEXT NOT NULL, -- Decimal value `cmc` TEXT NOT NULL, -- Decimal value

View file

@ -143,7 +143,7 @@ def update_scryfall(ctx, filename):
with open(filename) as f: with open(filename) as f:
cards = json.loads(f.read()) cards = json.loads(f.read())
else: else:
response = httpx.get("https://api.scryfall.com/bulk-data/oracle_cards") response = httpx.get("https://api.scryfall.com/bulk-data/all-cards")
info = response.json() info = response.json()
buffer = b"" buffer = b""

View file

@ -45,16 +45,13 @@ async def load(
foil=is_foil or None, foil=is_foil or None,
) )
if not found: if not found:
# logging.warning("Could not find card for row %s", row) logging.warning("Could not find card for row %s", row)
continue
elif len(found) > 1:
# logging.warning(
# "Found %s possibilities for row %s", len(found), row
# )
# for card in found:
# logging.warning(card)
continue continue
else: else:
if len(found) > 1:
logging.warning(
"Found %s possibilities for row %s", len(found), row
)
card = tutor.models.CardCopy( card = tutor.models.CardCopy(
card=found[0], card=found[0],
foil=is_foil, foil=is_foil,

View file

@ -227,27 +227,30 @@ async def advanced_search(
async def store_card(db: aiosqlite.Connection, card: tutor.models.Card) -> None: async def store_card(db: aiosqlite.Connection, card: tutor.models.Card) -> None:
await db.execute( await db.execute(
"INSERT INTO cards " """
"(`scryfall_id`, `oracle_id`, `name`, `set_code`, `collector_number`," INSERT INTO cards
" `rarity`, `color_identity`, `cmc`, `type_line`, `release_date`," (`scryfall_id`, `oracle_id`, `name`, `set_code`, `collector_number`,
" `edhrec_rank`, `oracle_text`) " `rarity`, `color_identity`, `cmc`, `type_line`, `release_date`,
"VALUES (:scryfall_id, :oracle_id, :name, :set_code, :collector_number," `edhrec_rank`, `oracle_text`)
" :rarity, :color_identity, :cmc, :type_line, :release_date, :edhrec_rank," VALUES (:scryfall_id, :oracle_id, :name, :set_code, :collector_number,
" :oracle_text) " :rarity, :color_identity, :cmc, :type_line, :release_date, :edhrec_rank,
"ON CONFLICT (scryfall_id) DO UPDATE " :oracle_text)
"SET `name` = :name" ON CONFLICT (scryfall_id) DO UPDATE
" , `set_code` = :set_code" SET `oracle_id` = :oracle_id
" , `collector_number` = :collector_number" , `name` = :name
" , `rarity` = :rarity" , `set_code` = :set_code
" , `color_identity` = :color_identity" , `collector_number` = :collector_number
" , `cmc` = :cmc" , `rarity` = :rarity
" , `type_line` = :type_line" , `color_identity` = :color_identity
" , `release_date` = :release_date" , `cmc` = :cmc
" , `edhrec_rank` = :edhrec_rank" , `type_line` = :type_line
" , `oracle_text` = :oracle_text", , `release_date` = :release_date
, `edhrec_rank` = :edhrec_rank
, `oracle_text` = :oracle_text
""",
{ {
"scryfall_id": str(card.scryfall_id), "scryfall_id": str(card.scryfall_id),
"oracle_id": str(card.oracle_id), "oracle_id": str(card.oracle_id) if card.oracle_id else None,
"name": card.name, "name": card.name,
"set_code": card.set_code, "set_code": card.set_code,
"collector_number": card.collector_number, "collector_number": card.collector_number,
@ -255,7 +258,7 @@ async def store_card(db: aiosqlite.Connection, card: tutor.models.Card) -> None:
"color_identity": tutor.models.Color.to_string(card.color_identity), "color_identity": tutor.models.Color.to_string(card.color_identity),
"cmc": str(card.cmc), "cmc": str(card.cmc),
"type_line": card.type_line, "type_line": card.type_line,
"release_date": str(card.release_date), "release_date": str(card.release_date) if card.release_date else None,
"edhrec_rank": card.edhrec_rank, "edhrec_rank": card.edhrec_rank,
"oracle_text": card.oracle_text, "oracle_text": card.oracle_text,
}, },

View file

@ -10,19 +10,25 @@ def to_card(data: dict) -> tutor.models.Card:
} }
return tutor.models.Card( return tutor.models.Card(
scryfall_id=data["id"], scryfall_id=data["id"],
oracle_id=data["oracle_id"], oracle_id=data.get("oracle_id"),
name=data["name"], name=data["name"],
set_code=data["set"].upper(), set_code=data["set"].upper() if "set" in data else None,
collector_number=data["collector_number"], collector_number=data.get("collector_number"),
rarity=tutor.models.Rarity.from_string(data["rarity"]), rarity=tutor.models.Rarity.from_string(data.get("rarity", "n/a")),
color_identity=tutor.models.Color.from_string("".join(data["color_identity"])), color_identity=tutor.models.Color.from_string(
cmc=decimal.Decimal(data["cmc"]), "".join(data.get("color_identity", []))
type_line=data["type_line"], ),
release_date=datetime.date.fromisoformat(data["released_at"]), cmc=decimal.Decimal(data.get("cmc", "0")),
games={game for game in tutor.models.Game if game.value in data["games"]}, type_line=data.get("type_line", ""),
release_date=datetime.date.fromisoformat(data["released_at"])
if "released_at" in data
else None,
games={
game for game in tutor.models.Game if game.value in data.get("games", [])
},
legalities={ legalities={
game_format: {l.value: l for l in tutor.models.Legality}[legality] game_format: {l.value: l for l in tutor.models.Legality}[legality]
for game_format, legality in data["legalities"].items() for game_format, legality in data.get("legalities", {}).items()
}, },
edhrec_rank=( edhrec_rank=(
int(data.get("edhrec_rank")) int(data.get("edhrec_rank"))