-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
342 lines (311 loc) · 12.4 KB
/
app.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
♪ And all you need to do is write some JavaScript code
And use the real-time web with Node ♪
https://www.youtube.com/watch?v=IkmHStAWXis
*/
var express = require('express');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function(req, file, mu) {
mu(null, './public/files/');
},
filename: function(req, file, mu) {
mu(null, Date.now() + file.originalname.replace(/\s+/g, '').toLowerCase());
}
});
var upload = multer({storage: storage});
var http = require("http");
var https = require("https");
var _ocrsdk = require('./lib/ocrsdk');
// For private keys
var config = require('./config');
var fs = require('fs');
var crypto = require('crypto');
var Parse = require('parse').Parse;
var File = Parse.Object.extend("File");
var ocrsdk = _ocrsdk.create(config.ocrsdkID, config.ocrsdkPass);
var API_VER = '1.0';
var app = express();
Parse.initialize(config.parseAppID, config.parseKey);
app.use(express.static(__dirname + '/public'));
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/api/' + API_VER + '/upload', upload.single('file'), function(req, res) {
console.log(req.file);
var resp = {
"result": "",
"file": {"id": "", "filepath": "", "text": "", "notes": [], "user": ""}
};
var text = "";
var content;
var filepath = req.file.path;
if(filepath.substring(filepath.length - 3, filepath.length) !== "jpg") {
https.get("https://api.idolondemand.com/1/api/sync/extracttext/v1"
+ "?apikey=" + config.hpKey + "&url=" + config.baseURL
+ req.file.path.replace('public/', ''), function(resp) {
resp.on('data', function(data) {
text += data;
}).on('end', function() {
console.log(text);
content = JSON.parse(text);
processFile();
});
});
} else {
var opt = new _ocrsdk.ProcessingSettings();
opt.language = "English";
opt.exportFormat = "txt";
ocrsdk.processImage(filepath, opt, function(err, data) {
if(err) {
console.log("OCRSDK Error: " + err.message);
}
ocrsdk.waitForCompletion(data.id, function(err, data) {
if(err) {
console.log("OCRSDK Error: " + err.message);
}
https.get(data.resultUrl.toString(), function(resp) {
resp.on('data', function(data) {
text += data;
}).on('end', function() {
console.log(text);
content = {
"document": [{
"content": text
}]
};
processFile();
})
});
});
});
}
var processFile = function() {
var hash = checksum(req.file.path, function(digest) {
var query = new Parse.Query(File);
query.equalTo("checksum", digest);
query.find({
success: function(results) {
if(results.length > 0) {
resp.result = "exists";
resp.file.id = results[0].id;
resp.file.filepath = results[0].get("filepath");
resp.file.text = results[0].get("text");
resp.file.notes = results[0].get("notes");
resp.file.user = results[0].get("user");
fs.unlinkSync(req.file.path);
res.send(resp);
}
else {
var file = new File();
file.set("filepath", req.file.path);
file.set("checksum", digest);
file.set("upvotes", 0);
file.set("downvotes", 0);
file.set("notes", []);
file.set("text", content.document[0].content);
file.set("user", req.body.user);
file.save(null, {
success: function(file) {
console.log(file);
resp.result = "created";
resp.file.id = file.id;
resp.file.filepath = file.get("filepath");
resp.file.text = file.get("text");
resp.file.user = file.get("user");
createNotes(file, function() {
resp.file.notes = file.get("notes");
res.send(resp);
})
},
error: function(file, err) {
console.log("Parse error: " + err);
res.status(500).send("500 - an error occurred.");
}
});
}
},
error: function(err) {
console.log("Error: " + err);
res.status(500).send("500 - an error occurred.")
}
});
});
};
});
app.get('/api/' + API_VER + '/getnotes/:id', function(req, res) {
var query = new Parse.Query(File);
query.get(req.params.id, {
success: function(file) {
var notes = file.get("notes");
var result = {notes: notes};
res.send(result);
},
error: function(file, err) {
console.log("Parse error: " + err);
res.status(500).send("500 - an error occurred.");
}
})
});
app.get('/api/' + API_VER + '/getfiles/:user', function(req, res) {
console.log("Attempting to get files for " + req.params.user);
var query = new Parse.Query(File);
query.equalTo("user", req.params.user);
var resp = {"results": []};
query.find({
success: function(results) {
for(var i = 0; i < results.length; i++) {
resp.results.push({"file": {
"id": results[i].id,
"filepath": results[i].get("filepath"),
"text": results[i].get("text"),
"notes": results[i].get("notes")
}})
}
res.send(resp);
},
error: function(err) {
console.log("Error: " + err);
res.status(500).send("500 - an error occurred");
}
});
});
/* Leenote: a route located at /api/1.0/storenotes/<file ID> */
app.post('/api/' + API_VER + '/storenotes/:id', function(req, res) {
var query = new Parse.Query(File);
query.get(req.params.id, {
success: function(file) {
/*
right here, we can put your notes creation API logic
and store it to Parse
Leenote: use the http module https://nodejs.org/api/http.html
*/
file.set("notes", []);
},
error: function(file, err) {
res.status(404).send("File with ID " + req.params.id + " could not be loaded.");
}
});
});
app.get('/api/' + API_VER + '/score/:id', function(req, res) {
var query = new Parse.Query(File);
query.get(req.params.id, {
success: function(file) {
var upvotes = file.get("upvotes");
var downvotes = file.get("downvotes");
var result = {score: (upvotes - downvotes)};
res.send(result);
},
error: function(file, err) {
console.log("Parse error: " + err);
res.status(500).send("500 - an error occurred.");
}
})
});
app.get('/api/' + API_VER + '/:action/:id', function(req, res) {
var query = new Parse.Query(File);
// Leenote: this is either upvote or downvote from :action
var vote = req.params.action + "s";
query.get(req.params.id, {
success: function(file) {
file.set(vote, file.get(vote) + 1);
file.save(null, {
success: function(file) {
res.status(200).end();
},
error: function(file, err) {
console.log("Parse error: " + err);
res.status(500).send("500 - an error occurred.");
}
});
},
error: function(file, err) {
console.log("Parse error: " + err);
res.status(500).send("500 - an error occurred.");
}
})
});
function createNotes(file, callback) {
var content = "";
var notes = [];
var text = file.get("text");
if(text.length >= 7864) {
text = text.substring(0, 7863);
}
console.log("Gotten text: " + text);
var relevance = 0.6;
// WELCOME TO CALLBACK HELL (fixme)
http.get("http://access.alchemyapi.com/calls/text/TextGetRankedNamedEntities?"
+ "apikey=" + config.alchemyKey + "&text=" + text + "&outputMode=json", function(resp) {
resp.on('data', function(data) {
content += data;
}).on('end', function() {
var rawnotes = JSON.parse(content);
console.log(content);
for (var i = 0; i < rawnotes.entities.length; i++) {
if (rawnotes.entities[i].relevance > relevance) {
notes.push(rawnotes.entities[i]);
notes[notes.length - 1].sentences = [];
notes[notes.length - 1].subTopics = [];
}
}
console.log("Pass 1: " + notes);
content = "";
http.get("http://access.alchemyapi.com/calls/text/TextGetRelations?"
+ "apikey=" + config.alchemyKey + "&text=" + text + "&keywords=1&outputMode=json", function(resp) {
resp.on('data', function(data) {
content += data;
}).on('end', function() {
var rawnotes2 = JSON.parse(content);
console.log(content);
try {
for (var x = 0; x < notes.length; x++) {
for (var j = 0; j < rawnotes2.relations.length; j++) {
if(rawnotes2.relations[j].subject.hasOwnProperty("keywords")) {
if (notes[x].text == rawnotes2.relations[j].subject.keywords[0].text) {
notes[x].sentences.push(rawnotes2.relations[j].object.text)
if (rawnotes2.relations[j].object.hasOwnProperty("keywords")) {
for (var b = 0; b < rawnotes2.relations[j].object.keywords.length; b++) {
notes[x].subTopics.push(rawnotes2.relations[j].subject.keywords[b].text);
}
}
}
}
}
}
}
catch(err) {
console.log("Not getting notes for this one... " + err);
}
console.log("Pass 2: " + notes);
file.set("notes", notes);
file.save(null, {
success: function(file) {
console.log("Saved file successfully");
callback();
},
error: function(file, err) {
console.log("Parse oopsie " + err);
}
})
})
}) // ends the 2nd http get
})
});
}
function checksum(filepath, callback) {
var hash = crypto.createHash('md5');
var stream = fs.createReadStream(filepath);
stream.on('data', function(data) {
hash.update(data, 'utf8');
});
var digest = "";
stream.on('end', function () {
digest += hash.digest('hex');
console.log("Digested hash: " + digest);
callback(digest);
});
}
app.listen(process.env.PORT || 3005);