forked from shinha97/FlashHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (80 loc) · 2.55 KB
/
server.js
File metadata and controls
85 lines (80 loc) · 2.55 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//analytics array
var http = require('http'),
fs = require('fs'),
url = require('url'),
feed = require('rss-to-json');
//access ibm watson nlu api
var NaturalLanguageUnderstandingV1,
natural_language_understanding,
analytics = {};
//convert search to rss and convert to json
function rss2json(searchString){
var rss = 'https://news.google.com/news/rss/search/section/q/'+searchString+'/'+searchString+'?hl=en&gl=US&ned=us';
console.log("Converting rss, "+rss+"...");
feed.load(rss,function(err,json){
console.log("Converted json...");
useNLU(json);
});
//callback();
}
//get sentiment and emotions for first 10 articles
function useNLU(jsonItem){
var parameters;
NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');
natural_language_understanding = new NaturalLanguageUnderstandingV1({
'username': '6a94dba3-2dfc-4ebd-9061-14321e40b4f3',
'password': 'KlGVUtjP5Nb8',
'version_date': '2017-02-27'
});
var apiIndex = 0;
var titleIndices = [];
for(var i=0, apiIndex = 0; i<jsonItem["items"].length; i++){
parameters = {
'url': jsonItem["items"][i]["url"],
'features': {
'metadata': {},
'emotion': {},
'sentiment': {}
}
};
natural_language_understanding.analyze(parameters, function(err, response) {
if (err){
console.log("error: "+err);
}
else{
analytics[apiIndex] = JSON.stringify(response);
console.log(analytics[apiIndex]);
apiIndex++;
}
});
}
}
//communicate with client-side javascript file
http.createServer(function(request, response){
var path = url.parse(request.url).pathname;
if(path=="/poststring"){
var requestData = '';
request.on('data', function (data) {
requestData += data;
if (requestData.length > 1e6)
request.connection.destroy();
});
request.on('end', function () {
console.log('Request data sent: ', requestData);
rss2json(requestData);
});
}else if(path=="/getstring"){
response.writeHead(200, {"Content-Type": "text/plain", 'Access-Control-Allow-Origin' : '*'});
response.end(JSON.stringify(analytics));
}else{
fs.readFile('./index.html', function(err, file) {
if(err) {
console.log(err);
return;
}
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(file, "utf-8");
});
}
}).listen(8001);
console.log('server running...');