-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
97 lines (65 loc) · 2.07 KB
/
import.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
const https = require('https');
const fs = require('fs');
const unzipper = require('unzipper');
const path = require('path');
const { jsonLoader, jsonWriter } = require('./helpers/json');
const rimraf = require('rimraf');
const packages = [
'https://dd.b.pvp.net/latest/set1-lite-pt_br.zip',
'https://dd.b.pvp.net/latest/set2-lite-pt_br.zip',
'https://dd.b.pvp.net/latest/set3-lite-pt_br.zip',
'https://dd.b.pvp.net/latest/set4-lite-pt_br.zip',
];
download = async url => {
var dir = './temp';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const writer = fs.createWriteStream("./temp/package.zip");
const response = await https.get(url, response => {
response.pipe(writer);
});
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
})
}
unzipIt = async file => {
var reader = fs.createReadStream(file)
.pipe(unzipper.Extract({
path: path.resolve('./temp/unzipped')
}));
return new Promise((resolve, reject) => {
reader.on('finish', resolve);
reader.on('error', reject);
})
}
copyArray = file => {
return jsonLoader(file);
}
clean = () => {
fs.unlinkSync('./temp/package.zip');
rimraf.sync('./temp/unzipped');
}
execute = async () => {
var dataset = [];
for(var i = 0; i < packages.length; i++) {
console.log('baixando pacote')
await download(packages[i]);
console.log('download ok');
console.log('deszipando');
await unzipIt('./temp/package.zip');
console.log('deszipado!');
console.log('carregando json e copiando array');
const set = copyArray(path.resolve(`./temp/unzipped/pt_br/data/set${i+1}-pt_br.json`));
dataset = dataset.concat(set);
console.log('carregado!');
console.log('limpando package');
clean();
console.log('feito');
}
console.log('Gravando dataset');
jsonWriter(dataset, path.resolve('./data/cards.json'));
console.log('Gravado!');
}
execute();