tutor/tutor/scryfall.py

45 lines
1.6 KiB
Python

import datetime
import decimal
import tutor.models
def to_card(data: dict) -> tutor.models.Card:
prices = {
k: decimal.Decimal(v) if v else None for k, v in data.get("prices", {}).items()
}
return tutor.models.Card(
scryfall_id=data["id"],
oracle_id=data.get("oracle_id"),
name=data["name"],
set_code=data["set"].upper() if "set" in data else None,
collector_number=data.get("collector_number"),
rarity=tutor.models.Rarity.from_string(data.get("rarity", "n/a")),
color_identity=tutor.models.Color.from_string(
"".join(data.get("color_identity", []))
),
cmc=decimal.Decimal(data.get("cmc", "0")),
mana_cost=data.get("mana_cost"),
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={
game_format: {l.value: l for l in tutor.models.Legality}[legality]
for game_format, legality in data.get("legalities", {}).items()
},
edhrec_rank=(
int(data.get("edhrec_rank"))
if data.get("edhrec_rank") is not None
else None
),
oracle_text=data.get("oracle_text"),
price_usd=prices.get("usd"),
price_usd_foil=prices.get("usd_foil"),
price_eur=prices.get("eur"),
price_eur_foil=prices.get("eur_foil"),
price_tix=prices.get("tix"),
)