Skip to main content

Magic: the Gathering decklist parser

Project description

mtg-parser

PyPI PyPI - Python Version Github - Latest Release GitHub

mtg_parser is a Python library to download and parse Magic: The Gathering decklists. It supports the MTGO/MTGA formats as well as the most popular deck building websites.

Table of contents

Installation

The following section covers the installation of mtg_parser.

Before using mtg_parser, you will need:

  • python>=3.9

To install mtg_parser, simply run one of the following commands in the terminal of your choice:

$ pip install mtg-parser
# or
$ uv add mtg-parser
# or
$ poetry add mtg-parser

Supported Formats

[!NOTE] mtg_parser has been developed with a primary focus on Commander. While it may function with other formats, compatibility is not guaranteed.

In addition to MTGO and MTGA formats, mtg_parser supports the following websites:

Known issues

Parsing decklists on some websites require specific configuration:

  • aetherhub.com requires a Cloudflare-bypass requests compatible http client such as cloudscraper
  • deckstats.net requires a Cloudflare-bypass requests compatible http client such as cloudscraper
  • moxfield.com requires a custom User-Agent (see here)

Usage

QuickStart

[!NOTE] As of mtg_parser="=>0.0.1a50" doesn't provide a default http client anymore.

To parse a decklist with mtg_parser, you need to provide an HTTP client that implements a requests compatible interface (such as requests or httpx).

import requests
import mtg_parser

cards = mtg_parser.parse_deck(url, requests.Session())

For conveniency, mtg_parser provides an optional HttpClientFacade to facilitate handling of different websites with different http clients.

import cloudscraper
import httpx
import mtg_parser

client_facade = mtg_parser.HttpClientFacade(httpx.Client(timeout=10.0))
client_facade.set_override('aetherhub.com', cloudscraper.create_scraper())
client_facade.set_override('moxfield.com', httpx.Client(
    timeout=10.0,
    headers={'User-Agent': os.getenv('MOXFIELD_USER_AGENT')},
))

cards = mtg_parser.parse_deck(url, client_facade)

Return format

parse_deck() returns an Iterable[Card] matching the following description

class Card:
    name: str
    quantity: int
    extension: Optional[str]
    number: Optional[str]
    tags: Iterable[str] = []

cards = mtg_parser.parse_deck(url, http_client)
for card in cards:
    print(card)

Parsing MTGO / MTGA

mtg_parser can parse textual decklists in either MTGO or MTGA format

import mtg_parser

decklist = """
    1 Atraxa, Praetors' Voice
    1 Imperial Seal
    1 Lim-Dûl's Vault
    1 Jeweled Lotus (CMR) 319
    1 Llanowar Elves (M12) 182
    3 Brainstorm #Card Advantage #Draw
"""

cards = mtg_parser.parse_deck(deck_list)

Parsing from aetherhub.com

Aetherhub Integration Tests

mtg_parser can parse public decks from aetherhub.com

[!IMPORTANT] aetherhub.com requires a Cloudflare-bypass requests compatible http client such as cloudscraper.

import cloudscraper
import mtg_parser

url = 'https://aetherhub.com/Deck/<deck_name>'

cards = mtg_parser.parse_deck(url, cloudscraper.create_scraper())

Parsing from archidekt.com

Archideckt Integration Tests

mtg_parser can parse public decks from archidekt.com

import requests
import mtg_parser

url = 'https://www.archidekt.com/decks/<deck_id>/'

cards = mtg_parser.parse_deck(url, requests.Session())

Parsing from deckstats.net

Deckstats Integration Tests

mtg_parser can parse public decks from deckstats.net

[!IMPORTANT] deckstats.net requires a Cloudflare-bypass requests compatible http client such as cloudscraper.

import cloudscraper
import mtg_parser

url = 'https://deckstats.net/decks/<user_id>/<deck_id>'

cards = mtg_parser.parse_deck(url, cloudscraper.create_scraper())

Parsing from moxfield.com

Moxfield Integration Tests

mtg_parser can parse public decks from moxfield.com

[!IMPORTANT] Moxfield.com prohibits scraping their website, as it violates their Terms of Service.

For authorized access, please contact support@moxfield.com to request a custom User-Agent.

import httpx
import mtg_parser

url = 'https://www.moxfield.com/decks/<deck_id>'

headers = {'user-agent': '<MOXFIELD_USER_AGENT>'}
with httpx.Client(headers=headers) as http_client:
    cards = mtg_parser.parse_deck(url, http_client)

Parsing from mtggoldfish.com

MTGGoldfish Integration Tests

mtg_parser can parse public decks from mtggoldfish.com

import requests
import mtg_parser

url = 'https://www.mtggoldfish.com/deck/<deck_id>'

cards = mtg_parser.parse_deck(url, requests.Session())

Parsing from mtgjson.com

mtgjson Integration Tests

mtg_parser can parse decks from mtgjson.com

import requests
import mtg_parser

url = 'https://mtgjson.com/api/v5/decks/<deck_name>.json'

cards = mtg_parser.parse_deck(url, requests.Session())

Parsing from scryfall.com

Scryfall Integration Tests

mtg_parser can parse public decks from scryfall.com

import requests
import mtg_parser

url = 'https://scryfall.com/<userid>/decks/<deck_id>/'

cards = mtg_parser.parse_deck(url, requests.Session())

Parsing from tappedout.net

Tappedout Integration Tests

mtg_parser can parse public decks from tappedout.net

import requests
import mtg_parser

url = 'https://tappedout.net/mtg-decks/<deck_id>/'

cards = mtg_parser.parse_deck(url, requests.Session())

Parsing from tcgplayer.com

TCGplayer Integration Tests

mtg_parser can parse public decks from either tcgplayer.com or infinite.tcgplayer.com

import requests
import mtg_parser

url = 'https://www.tcgplayer.com/content/magic-the-gathering/deck/<deck_name>/<deck_id>'
# or url = 'https://infinite.tcgplayer.com/magic-the-gathering/deck/<deck_name>/<deck_id>'

cards = mtg_parser.parse_deck(url, requests.Session())

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mtg_parser-0.0.1a52.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mtg_parser-0.0.1a52-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file mtg_parser-0.0.1a52.tar.gz.

File metadata

  • Download URL: mtg_parser-0.0.1a52.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/23.5.0

File hashes

Hashes for mtg_parser-0.0.1a52.tar.gz
Algorithm Hash digest
SHA256 9f83d3ca44a1a1962f9009d4bba63cbdf7050bcd32bdff6a5de8475dc729ecf3
MD5 1d55185648aa709de5fdc084dee8e507
BLAKE2b-256 e76dd813e049d32c02b03877d789d96e9400d8c7ee02c0ff76e487845ea15f08

See more details on using hashes here.

File details

Details for the file mtg_parser-0.0.1a52-py3-none-any.whl.

File metadata

  • Download URL: mtg_parser-0.0.1a52-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/23.5.0

File hashes

Hashes for mtg_parser-0.0.1a52-py3-none-any.whl
Algorithm Hash digest
SHA256 3a1c9a4b97ed92ec235d16ab1267c7bc03f7d746435d0860e45c4fd077c46cbe
MD5 4e45df330448e06b26f6239070f0a67e
BLAKE2b-256 b3468d8f3c5ba70acbaf0df9f788634b5499bf013e88f6ecd8807aaf943c3869

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page