forked from 5e-bits/5e-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatefeaturechoices.js
110 lines (94 loc) · 3.04 KB
/
updatefeaturechoices.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
var http = require("http");
var fs = require("fs");
const args = process.argv;
let data_type = args[2];
let class_name = args[3];
fill_in_features();
function getFeaturesFromAPI(callback) {
var url = "http://www.dnd5eapi.co/api/features/";
// console.log(url);
http
.get(url, res => {
const statusCode = res.statusCode;
const contentType = res.headers["content-type"];
// console.log('were in');
let error;
if (statusCode !== 200) {
error = new Error(`Request Failed.\n` + `Status Code: ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error(
`Invalid content-type.\n` +
`Expected application/json but received ${contentType}`
);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding("utf8");
let rawData = "";
res.on("data", chunk => (rawData += chunk));
res.on("end", () => {
try {
let results = [];
let parsedData = JSON.parse(rawData);
parsedData.results.forEach(obj => {
results.push(obj);
});
callback(results);
} catch (e) {
console.log(e.message);
}
});
})
.on("error", e => {
console.log(`Got error: ${e.message}`);
});
}
function sleep(seconds) {
var waitUntil = new Date().getTime() + seconds;
while (new Date().getTime() < waitUntil) true;
}
function fill_in_features(datatype_string, class_name) {
let input_filename = "./5e-SRD-" + "features" + ".json";
let fill_in = (err, data) => {
data = JSON.parse(data);
newData = [];
// for each feature
getFeaturesFromAPI(filled => {
for (let i = 0; i < data.length; i++) {
// filled is the correct list.
// we want to go through each choice'd feature and update its list
if (data[i].choice !== undefined) {
data[i].choice.from.forEach(wrongFeature => {
wrongFeature.url = filled.find(correctFeature => {
return correctFeature.name == wrongFeature.name;
}).url;
});
newData.push(data[i]);
}
if (newData.length == 28) {
let output_filename = "./upload-5e-SRD-" + datatype_string + ".json";
fs.writeFile(output_filename, JSON.stringify(data), err => {
if (err) throw err;
console.log("Success");
console.log(
"mongoimport -h ds133158.mlab.com:33158 -d 5e-srd-api -c " +
datatype_string +
" -u admin -p password --file upload-5e-SRD-" +
datatype_string +
".json --jsonArray"
);
});
break;
}
}
});
};
fs.readFile(input_filename, "utf8", (err, data) => {
fill_in(err, data);
});
}
// mongoimport -h ds133158.mlab.com:33158 -d 5e-srd-api -c classfeatures -u admin -p password --file upload-5e-SRD-classfeatures.json --jsonArray