-
Notifications
You must be signed in to change notification settings - Fork 1
/
mtg_json.py
executable file
·138 lines (105 loc) · 3.7 KB
/
mtg_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 17 16:22:35 2021
@author: Petra Wenzl
@author: Kannan Thambiah <[email protected]>
"""
import argparse
import json
import os
import pandas as pd
from natsort import natsorted
def get_cards(json_data, parsed_arguments):
if (parsed_arguments.filter):
json_data = filter_out_special_cards(json_data)
json_data = sort_ascending_by_number(json_data)
json_data = process_doublefaced(json_data)
card_dict = reduce_json_to_dict(json_data)
return card_dict
def read_json(input_file):
with open(input_file) as file:
json_file = json.load(file)
return json_file
def filter_out_special_cards(json_file):
cards = [card for card in json_file['data']['cards'] if
'e' not in card['number']
and '†' not in card['number']]
tokens = [token for token in json_file['data']['tokens'] if
'CH' not in token['number']]
json_file['data']['cards'] = cards
json_file['data']['tokens'] = tokens
return json_file
def sort_ascending_by_number(json_file):
sorted_cards = natsorted(json_file['data']['cards'],
key=lambda card: card['number'])
sorted_tokens = natsorted(json_file['data']['tokens'],
key=lambda card: card['number'])
json_file['data']['cards'] = sorted_cards
json_file['data']['tokens'] = sorted_tokens
return json_file
def process_doublefaced(json_file):
unique_cards = []
prev_card = None
for card in json_file['data']['cards']:
if is_double_sided(prev_card, card):
prev_card['type'] = f"{prev_card['type']} // {card['type']}"
else:
unique_cards.append(card)
prev_card = card
json_file['data']['cards'] = unique_cards
return json_file
def is_double_sided(prev_card, card):
return prev_card is not None \
and card['name'] == prev_card['name'] \
and '//' in card['name']
def reduce_json_to_dict(json_file):
codes = []
names = []
numbers = []
types = []
colors = []
rarities = []
for card in json_file['data']['cards']:
codes.append(card['setCode'])
numbers.append(card['number'])
names.append(card['name'])
types.append(card['type'])
colors.append(card['colorIdentity'])
rarities.append(card['rarity'])
for token in json_file['data']['tokens']:
codes.append(token['setCode'])
numbers.append(token['number'])
names.append(token['name'])
types.append(token['type'])
colors.append(token['colorIdentity'])
rarities.append('')
cards_dict = {
'Set': codes,
'#': numbers,
'Name': names,
'Rarity': rarities,
'Type': types,
'Color': colors}
return cards_dict
def save_to_file(cards_dict, set_name):
df = pd.DataFrame(cards_dict)
df['Normal'] = ''
df['Foil'] = ''
df['Booster'] = ''
df.index += 1
df.to_csv(index=False, path_or_buf=set_name + '.csv')
print('Your cardname file has been created! :) ')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filter', action='store_true',
help='filter out cards with non-numeric characters in \
their number')
parser.add_argument('filename', action='store',
help='input json file to process')
parsed_arguments = parser.parse_args()
input_file = parsed_arguments.filename
set_name = os.path.splitext(input_file)[0]
json_data = read_json(input_file)
cards_dict = get_cards(json_data, parsed_arguments)
save_to_file(cards_dict, set_name)