-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-db.js
197 lines (159 loc) · 5.25 KB
/
update-db.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const path = require('path');
const fs = require('fs');
const ROOT_DIR = './databases';
const INPUT_DIR = 'input';
const OUTPUT_DIR = 'output';
var moment = require('moment');
var ts = moment().format('YYYY-MM-DD');
function onErr(err) {
console.log(err);
return 1;
}
function listFiles(dir, filter) {
if (!fs.existsSync(dir)) {
console.warn("no dir ", dir);
return;
}
const output = [];
const files = fs.readdirSync(dir);
for (var i = 0; i < files.length; i++) {
const filename = path.join(dir, files[i]);
const stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
output.concat(listFiles(filename, filter)); // recurse
} else if (filter.test(filename)) {
output.push(filename);
}
};
return output;
}
function main() {
const FUNCTION_ARRAY = [
['Extract: Scryfall Full Oracle => Minimal Oracle', processScryfall],
];
const prompt = require('prompt');
console.log("Starting mtg-tool...");
let functionList = '';
for (let i = 0; i < FUNCTION_ARRAY.length; i++) {
functionList += `${i}. ${FUNCTION_ARRAY[i][0]}\n`;
}
prompt.start();
prompt.get({
properties: {
function: {
description: 'Select function:\n' + functionList,
default: '0'
}
}
}, function(err, result) {
if (err) { return onErr(err); }
const functionIndex = parseInt(result.function);
FUNCTION_ARRAY[functionIndex][1]();
});
}
function processScryfall() {
const BASIC_ATTRS = [
'cmc',
'color_identity',
'colors',
'layout',
'name',
'oracle_id',
'oracle_text',
'type_line',
];
const prompt = require('prompt');
const lineByLine = require('n-readlines');
const jsonFiles = listFiles(path.join(ROOT_DIR, INPUT_DIR), /\.json$/);
if (jsonFiles.length > 0) {
let jsonFilesList = '';
for (let i = 0; i < jsonFiles.length; i++) {
jsonFilesList += `${i}. ${jsonFiles[i]}\n`;
}
prompt.start();
prompt.get({
properties: {
file: {
description: `Please select an input file:\n` + jsonFilesList,
default: '0',
}
}
}, function(err, result) {
if (err) { return onErr(err); }
const chosenFile = jsonFiles[parseInt(result.file)];
console.log(`Processing ${chosenFile}...`);
let rawData = fs.readFileSync(chosenFile);
let longestValues = {};
BASIC_ATTRS.forEach(attr => {
longestValues[attr] = 0;
})
longestValues['image_uris'] = 0;
let cards = JSON.parse(rawData);
if (Array.isArray(cards)) {
let output = [];
for (let iCard = 0; iCard < cards.length; iCard++) {
const card = cards[iCard];
// skip tokens
if (card['layout'].indexOf('token') === -1) {
let minCard = {};
// get basic attributes
BASIC_ATTRS.forEach(attr => {
let value = card[attr];
if (value) {
if (Array.isArray(value)) {
minCard[attr] = value.join(',');
} else {
minCard[attr] = value;
}
if (typeof minCard[attr] === 'string') {
longestValues[attr] = Math.max(longestValues[attr], minCard[attr].length);
} else {
longestValues[attr] = Math.max(longestValues[attr], minCard[attr]);
}
} else {
minCard[attr] = null;
}
});
// get image URIs
let images = [];
// for flip cards
if (card['layout'] === 'transform') {
const faces = card['card_faces'];
if (faces && Array.isArray(faces)) {
for (let iFace = 0; iFace < faces.length; iFace++) {
const face = faces[iFace];
const faceUris = face['image_uris'];
if (faceUris && faceUris['normal']) {
images.push(faceUris['normal']);
}
}
}
} else {
const cardUris = card['image_uris'];
if (cardUris && cardUris['normal']) {
images.push(cardUris['normal']);
}
}
minCard['image_uris'] = images.join(',');
longestValues['image_uris'] = Math.max(longestValues['image_uris'], minCard['image_uris'].length);
// append card to result
output.push(minCard);
}
}
console.log('Extracted ' + output.length + ' non-token cards from ' + result.file);
const outputFile = path.join(ROOT_DIR, OUTPUT_DIR, 'oracle-card_' + ts + '.json');
fs.writeFileSync(outputFile, JSON.stringify(output, null, 2));
console.log(`--> output written to ${outputFile}`);
// fs.writeFileSync(`${ROOT_DIR}/output/min-oracle-server.json`, JSON.stringify({ 'OracleCards': output }, null, 2));
// console.log('--> min-oracle-server.json written to output folder');
console.log('Longest Values: ');
console.log(JSON.stringify(longestValues, null, 2));
} else {
console.log('Error: expected contents to be an array');
}
});
} else {
console.log(`No input JSON files detected in ${ROOT_DIR}/${INPUT_DIR}`);
}
}
main();