|
|
|
|
|
|
|
import requests |
|
import json |
|
import os |
|
import random |
|
import time |
|
|
|
|
|
|
|
directory = './mtg-training-data/' |
|
if not os.path.exists(directory): |
|
os.makedirs(directory) |
|
|
|
|
|
json_filename = './mtg-training-data/mtgdata.json' |
|
|
|
|
|
def save_card_main(card): |
|
|
|
img_url = card['image_uris'].get('art_crop') |
|
filename = which_filename(card) |
|
|
|
filename = filename.replace('/', '-') |
|
filename = filename.replace(':', '-') |
|
filename = filename.replace('?', '') |
|
filename = filename.replace('!', '') |
|
filename = filename.replace('\'', '-') |
|
filename = filename.replace('\"', '') |
|
filename = filename.replace(' ', '_') |
|
filename = filename.replace('(', '') |
|
filename = filename.replace(')', '') |
|
filename = filename.replace(',', '') |
|
filename = filename.replace('’', '') |
|
filename = filename.replace('–', '-') |
|
filename = filename.replace('—', '-') |
|
filename = filename.replace('…', '') |
|
|
|
if not os.path.isfile('{}{}'.format(directory, filename)): |
|
|
|
|
|
save_image(img_url, filename) |
|
|
|
data = return_data(card) |
|
|
|
save_data(data, filename) |
|
|
|
|
|
|
|
def which_filename(card): |
|
if card.get('flavor_name'): |
|
name = card['flavor_name'] |
|
|
|
if card.get('illustration_id'): |
|
id = card['illustration_id'] |
|
else: |
|
id = card['multiverse_ids'][0] |
|
id = str(id) |
|
filename = card['name'] + '_' + card['set'] + '_' + id + '.jpg' |
|
return filename |
|
|
|
|
|
def save_image(img_url, filename): |
|
response = requests.get(img_url) |
|
with open('{}{}'.format(directory, filename), 'wb') as f: |
|
f.write(response.content) |
|
report to stdout that we just saved the url to the filename |
|
print('Saved {} to {}'.format(img_url, filename)) |
|
|
|
|
|
|
|
def save_data(data, filename): |
|
filename = filename.replace('.jpg', '.txt') |
|
with open('{}{}'.format(directory, filename), 'w', encoding="utf8") as f: |
|
f.write(str(data)) |
|
|
|
print('Saved {} to {}'.format(data, filename)) |
|
|
|
|
|
|
|
def return_data(card): |
|
glue = 'MTG card art' |
|
name = card['name'] |
|
|
|
if card.get('flavor_name'): |
|
name = card['flavor_name'] |
|
|
|
name = name.replace('\'', '’') |
|
artist = 'by ' + card['artist'] |
|
|
|
artist = artist.replace('\'', '’') |
|
year = get_year(card['released_at']) |
|
if card.get('colors'): |
|
colors_english = mtg_color_to_english(card['colors']) |
|
colors = clean_colors(card['colors']) |
|
else: |
|
colors_english = 'Colorless' |
|
colors = 'C' |
|
card_type = card['type_line'] |
|
rarity = card['rarity'] |
|
set_name = card['set_name'] |
|
set_code = ' ' |
|
set_code = card['set'] |
|
set_type = card['set_type'] |
|
plane = ' ' |
|
plane = determine_plane(set_code) |
|
if card.get('watermark'): |
|
watermark = card['watermark'] |
|
else: watermark = 'WATERMARK_PLACEHOLDER' |
|
mana_cost = clean_data(card['mana_cost']) |
|
if card.get('security_stamp'): |
|
security_stamp = parse_security_stamp(card['security_stamp']) |
|
else: security_stamp = 'SECURITY_STAMP_PLACEHOLDER' |
|
if card.get('power'): |
|
power = card['power'] |
|
else: power = 'POWER_PLACEHOLDER' |
|
if card.get('toughness'): |
|
toughness = card['toughness'] |
|
else: toughness = 'TOUGHNESS_PLACEHOLDER' |
|
power_toughness = power + '/' + toughness |
|
if card.get('keywords'): |
|
keywords = card['keywords'] |
|
else: keywords = 'KEYWORDS_PLACEHOLDER' |
|
if card.get('promo_types'): |
|
promo_types = card['promo_types'] |
|
else: promo_types = 'PROMO_TYPES_PLACEHOLDER' |
|
if card.get('story_spotlight'): |
|
story_spotlight = card['story_spotlight'] |
|
else: story_spotlight = 'SPOTLIGHT_PLACEHOLDER' |
|
|
|
extra_space = ' ' |
|
|
|
data = glue, name, artist, year, colors_english, colors, card_type, rarity, set_name, set_code, plane, set_type, watermark, extra_space, mana_cost, extra_space, security_stamp, power_toughness, keywords, promo_types, story_spotlight |
|
|
|
data = clean_data(data) |
|
return data |
|
|
|
|
|
def determine_plane(set_code): |
|
|
|
set_code = set_code.upper() |
|
if set_code == 'ALA' or set_code == 'CON' or set_code == 'ARB': |
|
plane = 'Alara' |
|
elif set_code == 'AKH' or set_code == 'HOU': |
|
plane = 'Amonkhet' |
|
elif set_code == 'STX': |
|
plane = 'Arcavios' |
|
elif set_code == 'SNC': |
|
plane = 'Capenna' |
|
elif set_code == 'ELD': |
|
plane = 'Eldraine' |
|
elif set_code == 'CNS' or set_code == 'CN2': |
|
plane = 'Fiora' |
|
elif set_code == 'IKO': |
|
plane = 'Ikoria' |
|
elif set_code == 'ISD' or set_code == 'DKA' or set_code == 'AVR' or set_code == 'SOI' or set_code == 'EMN' or set_code == 'MID' or set_code == 'VOW': |
|
plane = 'Innistrad' |
|
elif set_code == 'IXL' or set_code == 'RIX': |
|
plane = 'Ixalan' |
|
elif set_code == 'KLD' or set_code == 'AER': |
|
plane = 'Kaladesh' |
|
elif set_code == 'KHM': |
|
plane = 'Kaldheim' |
|
elif set_code == 'SOK' or set_code == 'BOK' or set_code == 'CHK': |
|
plane = 'Kamigawa, past' |
|
elif set_code == 'NEC': |
|
plane = 'Kamigawa, present' |
|
|
|
|
|
|
|
elif set_code == 'LOR' or set_code == 'MOR': |
|
plane = 'Lorwyn, LorShad' |
|
elif set_code == 'SHM' or set_code == 'EVE': |
|
plane = 'Shadowmoor, LorShad' |
|
elif set_code == 'MMQ': |
|
plane = 'Mercadia' |
|
elif set_code == 'NPH': |
|
plane = 'New Phyrexia' |
|
elif set_code == 'MRD' or set_code == 'DST' or set_code == '5DN' or set_code == 'SOM' or set_code == 'MBS': |
|
plane = 'Mirrodin' |
|
elif set_code == 'ARN': |
|
plane = 'Rabiah' |
|
elif set_code == 'TMP' or set_code == 'STH' or set_code == 'EXO' or set_code == 'NEM': |
|
plane = 'Rath' |
|
elif set_code == 'RAV' or set_code == 'GPT' or set_code == 'DIS' or set_code == 'RTR' or set_code == 'GTC' or set_code == 'DGM' or set_code == 'GRN' or set_code == 'RNA' or set_code == 'WAR': |
|
plane = 'Ravnica' |
|
elif set_code == 'M10' or set_code == 'M11' or set_code == 'M12' or set_code == 'M13' or set_code == 'M14' or set_code == 'M15': |
|
plane = 'Shandalar' |
|
elif set_code == "KTK" or set_code == "FRF" or set_code == "DTK": |
|
plane = 'Tarkir' |
|
elif set_code == 'THS' or set_code == 'BNG' or set_code == 'JOU' or set_code == 'THB': |
|
plane = 'Theros' |
|
elif set_code == 'HML': |
|
plane = 'Ulgrotha' |
|
elif set_code == 'ZEN' or set_code == 'WWK' or set_code == 'ROE' or set_code == 'BFZ' or set_code == 'OGW' or set_code == 'ZNR': |
|
plane = 'Zendikar' |
|
|
|
|
|
elif set_code == 'LEA' or set_code == 'LEG' or set_code == 'DOM' or set_code == 'DMU': |
|
plane = 'Dominaria' |
|
elif set_code == 'ATQ' or set_code == 'DRK': |
|
plane = 'Dominaria, Terisiare' |
|
elif set_code == 'ICE' or set_code == 'ALL' or set_code == 'CSP': |
|
plane = 'Dominaria, Terisiare, Ice Age' |
|
elif set_code == 'BRO': |
|
plane = 'Dominaria, past' |
|
elif set_code == 'MIR' or set_code == 'VIS' or set_code == 'WTH' or set_code == 'PCY': |
|
plane = 'Dominaria, Jamuraa' |
|
elif set_code == 'TSP' or set_code == 'PLC' or set_code == 'FUT': |
|
plane = 'Dominaria, Time Spiral' |
|
elif set_code == 'INV' or set_code == 'PLS' or set_code == 'APC': |
|
plane = 'Dominaria, Phyrexian Invasion' |
|
elif set_code == 'ODY' or set_code == 'TOR' or set_code == 'JUD' or set_code == 'ONS' or set_code == 'LGN' or set_code == 'SCG': |
|
plane = 'Dominaria, Otaria' |
|
else: |
|
plane = ' ' |
|
return plane |
|
|
|
|
|
def clean_colors(colors): |
|
colors = str(colors) |
|
colors = colors.replace('[', '') |
|
colors = colors.replace(']', '') |
|
colors = colors.replace('\'', '') |
|
colors = colors.replace(',', '') |
|
colors = colors.replace(' ', '') |
|
|
|
|
|
while True: |
|
colors = colors.replace('UW', 'WU') |
|
colors = colors.replace('BW', 'WB') |
|
colors = colors.replace('RW', 'WR') |
|
colors = colors.replace('GW', 'WG') |
|
colors = colors.replace('BU', 'UB') |
|
colors = colors.replace('GU', 'UG') |
|
colors = colors.replace('RU', 'UR') |
|
colors = colors.replace('RB', 'BR') |
|
colors = colors.replace('GB', 'BG') |
|
colors = colors.replace('GR', 'RG') |
|
if 'UW' not in colors and 'BW' not in colors and 'RW' not in colors and 'GW' not in colors and 'BU' not in colors and 'GU' not in colors and 'RU' not in colors and 'RB' not in colors and 'GB' not in colors and 'GR' not in colors: |
|
break |
|
return colors |
|
|
|
def parse_security_stamp(security_stamp): |
|
if security_stamp: |
|
if security_stamp == 'acorn': |
|
return 'acorn' |
|
if security_stamp == 'triangle': |
|
return 'Universes Beyond' |
|
if security_stamp == 'arena': |
|
return 'Arena' |
|
if security_stamp == 'circle': |
|
|
|
|
|
return None |
|
if security_stamp == 'heart': |
|
return 'My Little Pony' |
|
if security_stamp == 'oval': |
|
return None |
|
else: return None |
|
|
|
|
|
|
|
def mtg_color_to_english(colors): |
|
|
|
colors = ''.join([c for c in colors if c in 'WUBRG']) |
|
|
|
colors_english = '' |
|
for c in colors: |
|
if c == 'W': |
|
colors_english += 'White, ' |
|
if c == 'U': |
|
colors_english += 'Blue, ' |
|
if c == 'B': |
|
colors_english += 'Black, ' |
|
if c == 'R': |
|
colors_english += 'Red, ' |
|
if c == 'G': |
|
colors_english += 'Green, ' |
|
if c == '': |
|
colors_english += 'Colorless, ' |
|
|
|
colors_english = colors_english[:-2] |
|
return colors_english |
|
|
|
|
|
def get_year(date): |
|
year = date[:4] |
|
return year |
|
|
|
|
|
|
|
def split_dfc(card): |
|
face = 0 |
|
while face < 2: |
|
card['illustration_id'] = card['card_faces'][face]['illustration_id'] |
|
card['name'] = card['card_faces'][face]['name'] |
|
if card['card_faces'][face].get('flavor_name'): |
|
card['flavor_name'] = card['card_faces'][face]['flavor_name'] |
|
card['mana_cost'] = card['card_faces'][face]['mana_cost'] |
|
card['type_line'] = card['card_faces'][face]['type_line'] |
|
card['oracle_text'] = card['card_faces'][face]['oracle_text'] |
|
card['colors'] = card['card_faces'][face]['colors'] |
|
if card['card_faces'][face].get('power'): |
|
card['power'] = card['card_faces'][face]['power'] |
|
if card['card_faces'][face].get('toughness'): |
|
card['toughness'] = card['card_faces'][face]['toughness'] |
|
card['artist'] = card['card_faces'][face]['artist'] |
|
card['image_uris'] = card['card_faces'][face]['image_uris'] |
|
save_card_main(card) |
|
face += 1 |
|
|
|
|
|
|
|
def clean_data(data): |
|
data = str(data) |
|
data = data.replace('{', '') |
|
data = data.replace('}', '') |
|
data = data.replace('—', '-') |
|
data = data.replace('—', "-") |
|
|
|
data = data.replace('WATERMARK_PLACEHOLDER', '') |
|
data = data.replace('SECURITY_STAMP_PLACEHOLDER', '') |
|
data = data.replace('POWER_PLACEHOLDER', '') |
|
data = data.replace('TOUGHNESS_PLACEHOLDER', '') |
|
data = data.replace('KEYWORDS_PLACEHOLDER', '') |
|
data = data.replace('PROMO_TYPES_PLACEHOLDER', '') |
|
data = data.replace('SPOTLIGHT_PLACEHOLDER', '') |
|
|
|
data = data.replace(', ,', ',') |
|
|
|
data = data.replace('(', '') |
|
data = data.replace(')', '') |
|
|
|
data = data.replace('[', '') |
|
data = data.replace(']', '') |
|
|
|
data = data.replace('\'', '') |
|
|
|
data = data.replace('"', '') |
|
|
|
|
|
|
|
data = data.replace(',,', '') |
|
|
|
data = data.replace('/,', '') |
|
|
|
data = data.replace(' ,', ' ') |
|
|
|
data = data.replace(' ', ' ') |
|
|
|
data = data.replace('ǵ', 'g') |
|
|
|
data = data.replace('õ', 'o') |
|
|
|
data = data.replace('é', 'e') |
|
|
|
data = data.replace('ï', 'i') |
|
|
|
data = data.replace('ñ', 'n') |
|
|
|
data = data.replace('ë', 'e') |
|
|
|
data = data.replace('û', 'u') |
|
|
|
data = data.replace('ó', 'o') |
|
|
|
data = data.replace('ń', 'n') |
|
|
|
data = data.replace('`', '\'') |
|
return data |
|
|
|
|
|
with open(json_filename, 'r', encoding="utf8") as f: |
|
card_data = json.load(f) |
|
|
|
for card in card_data: |
|
|
|
if card['layout'] == 'transform' or card['layout'] == 'modal_dfc' or card['layout'] == 'reversible_card' or card['layout'] == 'double_faced_token': |
|
split_dfc(card) |
|
|
|
elif card['layout'] == 'art_series': |
|
continue |
|
else: save_card_main(card) |
|
|
|
|
|
|
|
|