tutor/tutor/scryfall.py

57 lines
2.1 KiB
Python

import datetime
import decimal
import uuid
from returns.maybe import Maybe
import tutor.models
def to_card(data: dict) -> Maybe[tutor.models.Card]:
prices = {
k: decimal.Decimal(v) if v else None for k, v in data.get("prices", {}).items()
}
return Maybe.do(
tutor.models.Card(
scryfall_id=data["id"],
oracle_id=oracle_id,
name=data["name"],
set_code=set_code,
set_name=set_name,
collector_number=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=release_date,
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=Maybe.from_optional(data.get("edhrec_rank"))
.map(int)
.value_or(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"),
)
for oracle_id in Maybe.from_optional(data.get("oracle_id")).map(uuid.UUID)
for set_code in Maybe.from_optional(data.get("set")).map(str.upper)
for set_name in Maybe.from_optional(data.get("set_name")).map(str)
for collector_number in Maybe.from_optional(data.get("collector_number")).map(
str
)
for release_date in Maybe.from_optional(data.get("released_at")).map(
datetime.date.fromisoformat
)
)