This repository has been archived by the owner on Nov 2, 2019. It is now read-only.
/
server.js
77 lines (67 loc) · 2.17 KB
/
server.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
var fs = require('fs');
var sources = require("./sources.json");
var express = require('express');
var util = require('util');
var request = require('request');
var app = express();
var path = require('path');
var url = require('url');
var spawn = require("child_process").spawn;
app.use('/', express.static(path.join(__dirname, 'public')));
var server = app.listen(5000, function() {
console.log('Server listening on port 5000');
});
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.on('newComment', function(comment, callback) {
commentBody = (comment.text);
siteName = (comment.sitename);
var label = "";
console.log(commentBody);
siteName = (url.parse(siteName).hostname);
console.log(siteName);
var reliabilityResult = Object(sources[siteName]);
var biasDetector = spawn('python3', ["text_classification/classify_text.py", commentBody]);
biasDetector.stdout.on('data', function(data) {
var outputText = data.toString('utf8');
console.log(outputText);
if (reliabilityResult['type'] != undefined) {
var rating = ("Source rating: " + reliabilityResult['type']);
} else if (reliabilityResult['type'] == undefined) {
var rating = ("Source could not be found in unreliable sites database. ");
}
if (outputText == undefined) {
outputText = ("No text provided");
}
console.log(outputText);
sentAnalysis(commentBody, outputText, rating);
});
});
function sentAnalysis(commentBody, outputText, rating) {
request({
method: 'post',
url: 'http://text-processing.com/api/sentiment/',
form: {
text: commentBody
},
json: true,
}, function(error, response, body) {
var label = ("Sentiment analysis: " + body['label']);
console.log(label);
if (label == undefined) {
label = ("Sentiment analysis error");
}
io.sockets.emit('broadcast', {
description: outputText,
rating: rating,
label: label
})
});
}
});
app.get('/main', function(req, res) {
var output = outputText;
res.render(__dirname + "index.html", {
output: outputText
});
});