-
Notifications
You must be signed in to change notification settings - Fork 7
/
bundles.py
82 lines (72 loc) · 2.48 KB
/
bundles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Data bundle definitions."""
import copy
from typing import List, NamedTuple, Optional, Set
from mtg_ssm.scryfall.models import (
ScryCard,
ScryCardLayout,
ScryMigration,
ScrySet,
ScrySetType,
)
class ScryfallDataSet(NamedTuple):
"""Bundle for storing Scryfall data."""
sets: List[ScrySet]
cards: List[ScryCard]
migrations: List[ScryMigration]
def filter_cards_and_sets( # noqa: C901
scryfall_data: ScryfallDataSet,
*,
exclude_set_types: Optional[Set[ScrySetType]] = None,
exclude_card_layouts: Optional[Set[ScryCardLayout]] = None,
exclude_digital: bool = False,
exclude_foreing_only: bool = False,
merge_promos: bool = False,
) -> ScryfallDataSet:
"""Filter a ScryfallDataSet to exclude desired set types, card layouts, and digital only products."""
accepted_setcodes = set()
remapped_setcodes = {}
for set_ in scryfall_data.sets:
if exclude_set_types and set_.set_type in exclude_set_types:
continue
if exclude_digital and set_.digital:
continue
if (
merge_promos
and set_.set_type == ScrySetType.PROMO
and set_.parent_set_code is not None
and set_.code == f"p{set_.parent_set_code}"
):
remapped_setcodes[set_.code] = set_.parent_set_code
continue
accepted_setcodes.add(set_.code)
accepted_cards = []
nonempty_setcodes = set()
for card_loop in scryfall_data.cards:
card = card_loop # capture loop variable
while card.set in remapped_setcodes:
collector_number = card.collector_number
if collector_number.isdigit():
collector_number += "p"
card = copy.copy(card)
card.set = remapped_setcodes[card.set]
card.collector_number = collector_number
if card.set not in accepted_setcodes:
continue
if exclude_card_layouts and card.layout in exclude_card_layouts:
continue
if exclude_digital and card.digital:
continue
if exclude_foreing_only and card.lang != "en":
continue
accepted_cards.append(card)
nonempty_setcodes.add(card.set)
accepted_sets = [
s
for s in scryfall_data.sets
if s.code in accepted_setcodes and s.code in nonempty_setcodes
]
return ScryfallDataSet(
sets=accepted_sets,
cards=accepted_cards,
migrations=scryfall_data.migrations,
)