-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexport-dataset.js
More file actions
executable file
·69 lines (53 loc) · 1.86 KB
/
Copy pathexport-dataset.js
File metadata and controls
executable file
·69 lines (53 loc) · 1.86 KB
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
#!/usr/bin/env node
// author: https://github.com/jakubhyza
const fs = require('fs');
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer.from(bitmap).toString('base64');
}
if(process.argv.length < 3)
{
console.log("Usage: export-dataset.js datasetname [id_offset]");
return;
}
var datasetname = process.argv[2];
var idOffset = process.argv.length > 3 ? parseInt(process.argv[3]) : 0;
console.log("Exporting dataset " + datasetname + " into a file " + datasetname + ".dlc");
if (fs.existsSync(datasetname))
{
console.log("Dataset exists, exporting...");
var datasetExport = {
filetype: "examiner-dlc",
version: "1.3",
name: datasetname,
data: []
}
var questions = fs.readdirSync(datasetname);
datasetExport.data = questions.map(questionId=>{
var questionFiles = fs.readdirSync(datasetname + "/" + questionId);
var answers = [];
questionFiles.forEach(file=>{
var filepath = datasetname + "/" + questionId + "/" + file;
if (file == "question.png")
return;
var isCorrect = file.charAt(0) == "c";
answers.push({
type: "image",
correct: isCorrect,
src: "data:image/png;base64," + base64_encode(filepath)
});
});
return {
id: parseInt(questionId) + idOffset,
type: "question-with-answers",
question: {
type: "image",
src: "data:image/png;base64," + base64_encode(datasetname + "/" + questionId + "/question.png")
},
answers: answers
}
}).sort((a,b)=>a.id - b.id);
fs.writeFileSync(datasetname + ".dlc", JSON.stringify(datasetExport, null, 4));
}