-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.js
101 lines (64 loc) · 2.17 KB
/
process.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
var fs = require("fs"),
readline = require('readline');
var quizObj = {},
thisQuestion = {},
quizNames = [];
quizObj.quiz = {};
thisQuestion.answers = [];
thisQuestion.question = {};
var rootDir = __dirname;
fs.readdir(rootDir+"/v2/data/OpenTriviaQA/categories", function(err, files){
if(err){
return console.error(err);
}
//console.log(files);
for(file in files){
var filename = files[file];
if(filename.indexOf('.js') < 0){
console.log(filename);
if (typeof quizObj.quiz[filename] == "undefined") {
quizObj.quiz[filename] = {};
}
else{
console.log("defined");
}
quizObj.quiz[filename].complete = true;
quizObj.quiz[filename].questions = [];
fs.readFileSync(rootDir+"/v2/data/OpenTriviaQA/categories/"+filename).toString().split('\n').forEach(function (line) {
if(line !== '' && typeof thisQuestion.question !== "undefined"){
if(line.indexOf('#Q') == 0){
var questionLine = line.split('#Q ');
thisQuestion.question = questionLine[1];
thisQuestion.type = "choice";
}
else if(line.indexOf('^') == 0){
var answerLine = line.split('^ ');
thisQuestion.answer = answerLine[1];
}
else if(line.indexOf('A') == 0 || line.indexOf('B') == 0 || line.indexOf('C') == 0 || line.indexOf('D') == 0){
var re = /A |B |C |D /;
var thisAnswer = line.split(re);
thisQuestion.answers.push(thisAnswer[1]);
}
}
else{
if(thisQuestion.answers.length !== 0 ){
quizObj.quiz[filename].questions.push(thisQuestion);
}
thisQuestion = {};
thisQuestion.answers = [];
thisQuestion.question = {};
//add to questions array
}
}
);
console.log("quizObj", quizObj);
//console.log(quizObj.quiz.animals.questions);
}
fs.writeFileSync("json/"+filename+'.json', JSON.stringify(quizObj));
quizNames.push(filename);
quizObj = {};
quizObj.quiz = {};
}
fs.writeFileSync("json/quizes.json", JSON.stringify(quizNames));
});