Skip to main content

Magic: the Gathering decklist parser

Project description

mtg-parser

PyPI PyPI - Python Version Github - Nightly Check GitHub

mtg_parser is a Python library to download and parse Magic: The Gathering decklists. It supports the most popular decklists hosting websites.

The following section covers the installation of mtg_parser.

Table of contents

Installation

The following section covers the installation of mtg_parser.

Before using mtg_parser, you will need:

  • python >= 3.8.1

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

$ pip install 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

Moxfield

moxfield.com prevents the scraping of their website (it's against their Terms of Service). Please contact support@moxfield.com if you want to proceed anyway.

Usage

Start by importing the mtg_parser module:

import mtg_parser

Now let's parse a decklist (in any of the supported formats) and display the cards:

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

mtg_parser.parse_deck() is a shortcut method to the specialized versions. Provided that url is a valid Moxfield url, the following lines are equivalent:

cards = mtg_parser.parse_deck(url)
# is the same as:
cards = mtg_parser.moxfield.parse_deck(url)

In general, it's advised to use mtg_parser.parse_deck() as the overhead is insignificant.

If for any reason, you need to configure how mtg_parser is fetching remote decklists, you can provide an optional requests.Session object.

from requests import Session

s = Session()
# Configure your session here
cards = mtg_parser.parse_deck(url, session=s)
for card in cards:
    print(card)

parse_deck() methods return a generator of Card objects matching the following description

class Card:
    name: str
    quantity: int = 1
    extension: Optional[str] = None
    number: Optional[str] = None
    tags: List[str] = []

Parsing textual decklist

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.decklist.parse_deck(deck_list)
for card in cards:
    print(card)

Parsing decklists from aetherhub.com

mtg_parser can parse public decks from aetherhub.com

import mtg_parser

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

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

Parsing decklists from archidekt.com

mtg_parser can parse public decks from archidekt.com

import mtg_parser

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

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

Parsing decklists from deckstats.net

mtg_parser can parse public decks from deckstats.net

import mtg_parser

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

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

Parsing decklists from moxfield.com

mtg_parser can parse public decks from moxfield.com

import mtg_parser

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

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

Parsing decklists from mtggoldfish.com

mtg_parser can parse public decks from mtggoldfish.com

import mtg_parser

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

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

Parsing decklists from mtgjson.com

mtg_parser can parse decks from mtgjson.com

import mtg_parser

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

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

Parsing decklists from scryfall.com

mtg_parser can parse public decks from scryfall.com

import mtg_parser

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

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

Parsing decklists from tappedout.net

mtg_parser can parse public decks from tappedout.net

import mtg_parser

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

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

Parsing decklists from infinite.tcgplayer.com

mtg_parser can parse public decks from infinite.tcgplayer.com

import mtg_parser

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

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

Parsing decklists from decks.tcgplayer.com

mtg_parser can parse public decks from decks.tcgplayer.com

import mtg_parser

url = 'https://decks.tcgplayer.com/magic/commander/<user_name>/<deck_name>/<deck_id>'

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

Project details


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.1a37.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

mtg_parser-0.0.1a37-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mtg_parser-0.0.1a37.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/23.5.0

File hashes

Hashes for mtg_parser-0.0.1a37.tar.gz
Algorithm Hash digest
SHA256 ddca80ddea01504ff3367d224adf9b5317f906fe236e47fec7031a1669732ae1
MD5 a0a62ae75b27b9da6167aad04ecb36f0
BLAKE2b-256 69e6e20f23a9c351fb7ce655cf0ab70ec324aaf2bc82b62dc76b74d522fcd697

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mtg_parser-0.0.1a37-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/23.5.0

File hashes

Hashes for mtg_parser-0.0.1a37-py3-none-any.whl
Algorithm Hash digest
SHA256 fa4c8fbb2a3a2fde2a36dd56efeda4ab5b8048bb6b27a58b6205e048a6ceb4d3
MD5 5ff61a7c4048ce21e94372a25f78d9d6
BLAKE2b-256 bf69039a2a57ad08035ed6514c9a8dc7d767849aa8518732f46db8aa08c68e17

See more details on using hashes here.

Supported by

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