-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (68 loc) · 2.28 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
// pull in libraries (test)
const express = require('express');
const path = require('path');
const collector = require('./collector');
// load environment vars
require('dotenv').config();
// instances
const app = express();
// request handler
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
// request handler
app.get('/recentArticles', function(req, res) {
collector.getArticles().then((input) => {
if(input.length>10) {
input.splice(10, input.length - 1);
}
res.send(JSON.stringify(input));
}).catch((error) => {
console.log(error);
res.sendStatus(500);
});
return;
});
// request handler
app.get('/json', (req, res) => {
collector.getArticles().then((input) => {
res.send(JSON.stringify(toD3JSON(input)));
}).catch((error) => {
console.log(error);
res.sendStatus(500);
});
return;
});
// convert articles to D3 digestable JSON
function toD3JSON(articles, minCount = 2) {
var coins = {};
articles.forEach((item) => {
Object.keys(item.weighting).forEach((coin) => {
if(!coins[coin]) coins[coin] = [];
coins[coin].push(item);
});
});
var output = {"id":"coins", "children": []};
Object.keys(coins).forEach((coin) => {
var temp = [];
coins[coin].forEach((item) => {
var totalWeight = 0;
Object.keys(item.weighting).forEach((key) => {
totalWeight += item.weighting[key];
});
var score = parseInt((100 / totalWeight) * item.weighting[coin] / 100 * Math.abs(item.score));
temp.push({"id": coin + "." + item._id, "url": item.url, "value": score, "sentiment":(item.score>0?"pos":"neg"), "current": item.current, "past": item.past, "timestamp": item.timestamp});
});
var diff = collector.getRates()[coin];
if(temp.length >= minCount) output.children.push( {"id":coin, "diff": parseInt(diff), "children": temp});
});
return output;
}
// launch webserver
app.listen(3000, () => console.log('Listening on port 3000'));
// schedule article collection
if(!process.env.DISABLE_COLLECTOR) {
collector.collectData();
}
// schedule rate updates
collector.updateRates();