-
Notifications
You must be signed in to change notification settings - Fork 51
/
ParseMagic.js
122 lines (112 loc) · 2.81 KB
/
ParseMagic.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
var fs = require('fs');
var mongoose = require('mongoose');
// Download magic the gathering sets of cards.
// wget http://mtgjson.com/json/AllSets.json
/*
* This program parses sets of MTG cards and stores the mongolab.
* AllSets is structured like
* {
* SetOne: {
* metaDataHere: ...
* allCards: [...]
* },
* SetTwo: {
* metaDataHere: ...
* allCards: [...]
* }, etc
* }
*/
var setSchema = mongoose.Schema({
name: String,
code: String,
gathererCode: String,
magicCardsInfoCode: String,
releaseDate: String,
border: String,
type: String,
booster: [String]
});
var Set = mongoose.model('magic-set', setSchema);
var connected = false;
var totalSetsSaved = 0;
var setNames = [];
function doConnection() {
var baseUrl = 'mongodb://';
var userName = 'csc';
var password = 'Re*lD*t*22#';
var siteAndPort = 'ds049848.mongolab.com:49848';
var databaseName = 'elvenlab01';
var url =
baseUrl +
userName +
':' +
password +
'@' +
siteAndPort +
'/' +
databaseName;
console.log(url);
mongoose.connect(url);
}
function insertSet(setName, data) {
if (!connected) {
connected = true;
doConnection();
}
var set = new Set({
name: data.name,
code: data.code,
gatheringCode: data.gatheringCode,
magicCardsInfoCode: data.magicCardsInfoCode,
releaseDate: data.releaseDate,
border: data.border,
type: data.type,
booster: data.booster
});
set.save(function(err) {
console.log('saved: ', set.name);
totalSetsSaved++;
if (totalSetsSaved === setNames.length) {
mongoose.disconnect();
console.log(setNames.length);
}
});
}
function writeSetToFile(setName, data) {
// Windows does not like the name CON so we rename it.
if (setName === 'CON') {
setName = 'CONOT';
}
fs.writeFile(setName + '.json', JSON.stringify(data, null, 4), function(
err,
data
) {
if (err) {
console.log(err);
} else {
console.log('Success');
}
});
}
function writeData(setName, data, writeToDisk, writeSets) {
if (writeToDisk) {
writeSetToFile(setName, data);
}
if (writeSets) {
insertSet(setName, data);
}
}
fs.readFile('AllSets.json', function(err, allSets) {
if (err) throw err;
var allSets = JSON.parse(allSets);
for (var set in allSets) {
if (allSets.hasOwnProperty(set)) {
setNames.push(set);
}
}
for (var i = 0; i < setNames.length; i++) {
var setName = setNames[i];
writeData(setName, allSets[setName], true, true);
}
console.log('ending all reads and writes.');
});