-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
209 lines (181 loc) · 7.2 KB
/
upload.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//var pdfUtil = require('pdf-to-text');
var fs = require('fs');
var request = require('request');
//var PDFJS =require('../../build/dist');
var PDFParser = require('pdf2json');
var http = require('http');
var url = require('url');
var fs = require('fs');
var async = require('async');
var Sync = require('sync');
var deasync = require('deasync');
var progress = 0;
var totalProg = 0;
function processAndAdd(str2, setNum, roundNum, questionNum) {
var pageEndStr = "\n----------------Page";
var pageEndIndex = str2.indexOf(pageEndStr);
if (~pageEndIndex) {
str2 = str2.substring(0, pageEndIndex);
}
str2=str2.replace("EARTH ANDE SPACE", "EARTH AND SPACE");
//console.log(str2);
var tossupQ = strBetween(str2, ") ", "ANSWER:");
var category, type;
if (~tossupQ.indexOf("Multiple Choice")) {
category = tossupQ.substring(0, tossupQ.indexOf("Multiple Choice")).trim().toUpperCase();
}
else if (~tossupQ.indexOf("Multiple choice")) {
category = tossupQ.substring(0, tossupQ.indexOf("Multiple choice")).trim().toUpperCase();
}
else if (~tossupQ.indexOf("Short answer")) {
category = tossupQ.substring(0, tossupQ.indexOf("Short answer")).trim().toUpperCase();
}
else {
category = tossupQ.substring(0, tossupQ.indexOf("Short Answer")).trim().toUpperCase();
}
category = category.trim();
var categoryList = {
"EARTH SCIENCE": 0,
"EARTH AND SPACE": 0,
"ASTRONOMY": 0,
"BIOLOGY": 1,
"CHEMISTRY": 2,
"PHYSICS": 3,
"MATHEMATICS":4,
"MATH":4,
"ENERGY":5,
"GENERAL SCIENCE": 6,
"COMPUTER SCIENCE": 7
};
var catName = category+"";
category = categoryList[category];
var tossupA = strBetween(str2, "ANSWER: ", "BONUS");
str2 = str2.substring(str2.indexOf("BONUS"));
var bonusQ = strBetween(str2, ") ", "ANSWER:");
var bonusA = strBetween(str2, "ANSWER: ");
if (tossupQ && tossupA && bonusQ && bonusA && typeof category == 'undefined') throw "ERRORIS" + catName+"."+category+"\n"+tossupQ;
if (tossupQ) //if not empty string.
{
data.questions[setNum+"_"+roundNum+"_"+questionNum] = {
"tossupQ": tossupQ,
"tossupA": tossupA,
"bonusQ": bonusQ,
"bonusA": bonusA,
"category": category,
"setNum": setNum,
"roundNum": roundNum,
"catDiff": category+"."+roundNum
};
console.log(JSON.stringify(data.questions[setNum+"_"+roundNum+"_"+questionNum]).substring(0, 100));
}
}
function strBetween(input, s1, s2) {
if (s2)
return input.substring(input.indexOf(s1) + s1.length, input.indexOf(s2)).replace(/___*/g, "").trim();
else
return input.substring(input.indexOf(s1) + s1.length).replace(/___*/g, "").trim();
}
var getText = function(setNum, roundNum, callback) {
var roundString = "round";
if (setNum == 6) roundString = "Sample6_ROUND";
if (setNum == 7 || setNum == 3) roundString = "ROUND-";
if (setNum == 8) roundString = "Round-";
var afterString = "";
if (setNum == 3) afterString = "C";
if (setNum == 8) afterString = "-A";
var path = "http://science.energy.gov/~/media/wdts/nsb/pdf/HS-Sample-Questions/Sample-Set-"+setNum+"/"+roundString+roundNum+afterString+".pdf";
console.log(path);
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
request({url: path, encoding: null}, function (error, response, body) {
//http.get(url.parse(path), function(response) {
//if (!error && response.statusCode == 200) {
//var csv = body;
//res.send("hi");
//res.send(body);
var pdfParser = new PDFParser(this, 1);
pdfParser.on("pdfParser_dataError", function (errData) {
console.log("errored"+JSON.stringify(errData));
console.log(setNum+"."+roundNum);
//callback();
//throw JSON.stringify(errData);
//getText(setNum, roundNum, callback);
//return JSON.stringify(errData); //res.end();
});
pdfParser.on("pdfParser_dataReady", function (pdfData) {
//console.log("done" + pdfParser.getRawTextContent());
console.log("done");
var txt = pdfParser.getRawTextContent(); //res.end();
//console.log(txt);
var str = txt;
var questionNum = 1;
while (~str.indexOf("TOSS-UP"))
{
str = str.substring(str.indexOf("TOSS-UP")+7);
var str22 = str.substring(0, str.indexOf("TOSS-UP"));
processAndAdd(str22, setNum, roundNum, questionNum);
questionNum++;
//console.log(str2+"HUHU");
}
questionNum++;
str22 = str;
processAndAdd(str22, setNum, roundNum, questionNum);
progress++;
console.log("Progress: "+progress+" of "+totalProg);
callback();
});
/*var data = [];
response.on('data', function(chunk) {
data.push(chunk);
console.log(chunk.toString());
}).on('end', function() {
//at this point data is an array of Buffers
//so Buffer.concat() can make us a new Buffer
//of all of them together
var buffer = Buffer.concat(data);
console.log(buffer.toString('base64'));
*/
console.log(body);
pdfParser.parseBuffer(body);
// });
});
}
//getText(1, 8, function() {console.log(data);});
var array = [];
var data = {};
data.questions = {};
//console.log(JSON.stringify(data));
for (var setNum = 8; setNum <=8; setNum++) {
for (var roundNum = 1; roundNum <= 17; roundNum++) {
if (roundNum > 15 && (setNum == 5 || setNum ==6)) continue;
array.push({"setNum":setNum,"roundNum":roundNum});
totalProg++;
}
}
console.log(array);
function main() {
//async.each(array, function(item, callback) {
for (var i in array) {
var item = array[i];
getText(item.setNum, item.roundNum, function() {});
deasync.sleep(10);
}
//getText(1,4,callback);
//deasync.sleep(2000);
//},
while (progress < totalProg)
deasync.sleep(100);
console.log(JSON.stringify(data));
done();
function done() {
console.log("finished");
fs.writeFile("output-hs-8.json", JSON.stringify(data), function(err) {
if(err) {
return console.log(err);
}
//res.write("Saved");
console.log("The file was saved!");
});
};
//console.log(array);
}
main();