-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (90 loc) · 3.65 KB
/
index.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
const admZip = require('adm-zip');
const fs = require('fs');
const http = require('http');
const url = require('url');
const resolvePath = require('path').resolve;
const documentation = '\n\n\n Refer to documentation here: ' + 'https://github.com/austinyearlykim/ez-mtgjson \n\n';
const jsonMap = {
allSets: 'https://mtgjson.com/json/AllSets.json.zip',
allSetsX: 'https://mtgjson.com/json/AllSets-x.json.zip',
allCards: 'https://mtgjson.com/json/AllCards.json.zip',
allCardsX: 'https://mtgjson.com/json/AllCards-x.json.zip'
};
const ezMtgJson = module.exports = {};
ezMtgJson.download = function(options) {
/*
options = {
target: 'allCards', //required
filePath: '../../myFileName.json', //optional Will download to file if specified
query: { //optional
name: 'Jace Beleren',
set: 'M10'
},
cache: false //optional
}
*/
validateOptions(options);
const sourceUrl = jsonMap[options.target];
const source = {
host: url.parse(sourceUrl).host,
port: 80,
path: url.parse(sourceUrl).pathname
};
return new Promise((resolve, reject) => {
http.get(source, (response) => {
if (response.statusCode !== 200) {
reject(mtgJsonUrl + ' returned a bad status code: ' + response.statusCode);
}
const bufferArray = []
let bufferLength = 0;
response
.on('data', function (chunk) {
bufferArray.push(chunk);
bufferLength += chunk.length;
})
.on('end', function () {
const buffer = new Buffer(bufferLength);
for (let i = 0, length = bufferArray.length, position = 0; i < length; i++) {
bufferArray[i].copy(buffer, position);
position += bufferArray[i].length;
}
const zip = new admZip(buffer);
const zipEntries = zip.getEntries();
for (let x = 0; x < zipEntries.length; x++) {
const unzippedJson = zip.readAsText(zipEntries[x]);
if (options.filePath) {
fs.writeFileSync(resolvePath(options.filePath), unzippedJson);
}
resolve(JSON.parse(unzippedJson));
}
});
});
});
}
function validateOptions(options) {
if (Array.isArray(options)) {
throw new Error('Your options object is an ARRAY [] when it should be an OBJECT {}.' + documentation);
}
if (!options.target) {
throw new Error('.download() requires a TARGET property on the options object.' + documentation);
}
const keys = Object.keys(jsonMap);
const correctJsonString = keys.some((key, index) => {
return key === options.target;
});
if (!correctJsonString) {
throw new Error('.download() requires a specific string for the TARGET property.' + documentation);
}
if (options.filePath && typeof options.filePath !== 'string') {
throw new Error('.download() requires the filePath to be of type string. Your current filePath type is: ' + typeof options.filePath + documentation);
}
}
function done(err, res) {
return {
done: (err, res) => {}
};
}
// ezMtgJson
// .download({ target: 'allCards', filePath: './asdf.js' })
// .then((json) => { console.log(json); })
// .catch((err) => { console.log(err); })