Skip to content

Commit

Permalink
Add filtering for non-English only cards/sets
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Jan 2, 2023
1 parent d196391 commit 1680151
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 81 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Development
- Fix filtering of digital only cards
- Switch from inclusion to exclusion for set types
- Add exclusion of card layouts (to catch tokens in non-token sets)
- Add (optional, default) exclusion of non-English cards/sets
(e.g. Renaissance)

2.3.0
-----
Expand Down
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
- Group sets by parent set is xlsx output
- Block? probably not
- At the very least, merge promos into somewhere?
- Filter non-English card sets
- Option to filter out non-English sets / cards
- diffs to stdout
- Use setuptools-scm so I don't have to think about releases as much
3 changes: 3 additions & 0 deletions mtg_ssm/containers/bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def filter_cards_and_sets(
exclude_set_types: Optional[Set[ScrySetType]] = None,
exclude_card_layouts: Optional[Set[ScryCardLayout]] = None,
exclude_digital: bool = False,
exclude_foreing_only: bool = False,
) -> ScryfallDataSet:
"""Filter a ScryfallDataSet to exclude desired set types, card layouts, and digital only products."""
accepted_setcodes = set()
Expand All @@ -37,6 +38,8 @@ def filter_cards_and_sets(
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)

Expand Down
26 changes: 18 additions & 8 deletions mtg_ssm/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ def get_args(args: Optional[List[str]] = None) -> argparse.Namespace:
)

parser.add_argument(
"-d",
"--dialect",
nargs=2,
metavar=("EXTENSION", "DIALECT"),
action="append",
default=[],
help="Mapping of file extensions to serializer dialects. "
"May be repeated for multiple different extensions.",
"--include-foreign-only",
default=False,
action="store_true",
help="Include foreign only cards/sets (e.g. Renaissance)",
)

default_exclude_set_types = {ScrySetType.MEMORABILIA, ScrySetType.TOKEN}
Expand All @@ -104,6 +100,17 @@ def get_args(args: Optional[List[str]] = None) -> argparse.Namespace:
+ ", ".join(ScryCardLayout),
)

parser.add_argument(
"-d",
"--dialect",
nargs=2,
metavar=("EXTENSION", "DIALECT"),
action="append",
default=[],
help="Mapping of file extensions to serializer dialects. "
"May be repeated for multiple different extensions.",
)

# Commands
subparsers = parser.add_subparsers(dest="action", title="actions")
subparsers.required = True
Expand Down Expand Up @@ -170,6 +177,7 @@ def get_oracle(
exclude_set_types: Set[ScrySetType],
exclude_card_layouts: Set[ScryCardLayout],
include_digital: bool,
include_foreign_only: bool,
) -> Oracle:
"""Get a card_db with current mtgjson data."""
scrydata = fetcher.scryfetch()
Expand All @@ -178,6 +186,7 @@ def get_oracle(
exclude_set_types=exclude_set_types,
exclude_card_layouts=exclude_card_layouts,
exclude_digital=not include_digital,
exclude_foreing_only=not include_foreign_only,
)
return Oracle(scrydata)

Expand Down Expand Up @@ -269,6 +278,7 @@ def main() -> None:
exclude_set_types=args.exclude_set_types,
exclude_card_layouts=args.exclude_card_layouts,
include_digital=args.include_digital,
include_foreign_only=args.include_foreign_only,
)
args.func(args, oracle)

Expand Down
16 changes: 16 additions & 0 deletions tests/containers/test_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ def test_exclude_token_layout(scryfall_data: bundles.ScryfallDataSet) -> None:
card_names2 = {c.name for c in tokens_removed.cards}
assert "p03" not in set_codes2 and "sld" in set_codes2
assert "Goblin" not in card_names2


def test_exclude_foreign_only(scryfall_data: bundles.ScryfallDataSet) -> None:
set_codes = {s.code for s in scryfall_data.sets}
card_ids = {c.id for c in scryfall_data.cards}

assert "ren" in set_codes
assert UUID("81917a2b-9bf6-4aa6-947d-36b0f45d6fe3") in card_ids

tokens_removed = bundles.filter_cards_and_sets(
scryfall_data, exclude_foreing_only=True
)
set_codes2 = {s.code for s in tokens_removed.sets}
card_ids2 = {c.id for c in tokens_removed.cards}
assert "ren" not in set_codes2
assert UUID("81917a2b-9bf6-4aa6-947d-36b0f45d6fe3") not in card_ids2
4 changes: 2 additions & 2 deletions tests/data/bulk_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"content_encoding": "gzip",
"content_type": "application/json",
"description": "A JSON file containing every card object on Scryfall in English or the printed language if the card is only available in one language.",
"download_uri": "https://data.scryfall.io/default-cards/default-cards-20230101100506.json",
"download_uri": "https://data.scryfall.io/default-cards/default-cards-20230101220647.json",
"id": "e2ef41e3-5778-4bc2-af3f-78eca4dd9c23",
"name": "Default Cards",
"object": "bulk_data",
"type": "default_cards",
"updated_at": "2023-01-01T10:05:06.298000+00:00",
"updated_at": "2023-01-01T22:06:47.443000+00:00",
"uri": "https://api.scryfall.com/bulk-data/e2ef41e3-5778-4bc2-af3f-78eca4dd9c23"
}
],
Expand Down
Loading

0 comments on commit 1680151

Please sign in to comment.