Created
November 14, 2022 19:53
-
-
Save GeekyDeaks/c3d362fb2308e03526d9ab2c4b93e6aa to your computer and use it in GitHub Desktop.
MTG converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import argparse | |
CARD_RE = re.compile('^(.+) \\| (.+)$') | |
CONDITION_RE = re.compile('^Condition') | |
PRICE_RE = re.compile('^£(\\d+\\.\\d+) GBP') | |
QTY_RE = re.compile('^Qty: (\\d+)$') | |
cards = [] | |
card = None | |
parser = argparse.ArgumentParser(description='Convert a madhouse order') | |
parser.add_argument('file') | |
args = parser.parse_args() | |
with open(args.file, encoding='utf8') as fh: | |
for line in fh.readlines(): | |
line = line.strip() | |
# ignore blanks | |
if not len(line): | |
continue | |
#print(f'line: {line}') | |
# does it look like a card? | |
r = CARD_RE.match(line) | |
if(r): | |
card_name = r.group(1) | |
card_set = r.group(2) | |
if not card or card['name'] != card_name: | |
card = { | |
'name': card_name, | |
'set': card_set, | |
'qty': 1 | |
} | |
cards.append(card) | |
#print(f'new card: {card}') | |
# check if we already have the card | |
# if we don't have a card name by here | |
if not card: | |
continue | |
r = PRICE_RE.match(line) | |
if(r): | |
card_price = r.group(1) | |
card['price'] = card_price | |
r = QTY_RE.match(line) | |
if(r): | |
card_qty = r.group(1) | |
card['qty'] = card_qty | |
#print(f'card: {card}') | |
# dump the cards | |
for card in cards: | |
print(f"{card['qty']}x {card['name']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment