-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·98 lines (79 loc) · 2.58 KB
/
index.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
#! /usr/bin/env node
'use strict';
const request = require('request');
const zlib = require('zlib');
const rightpad = require('right-pad');
const charm = require('charm')(process);
function perc(n) {
return (Math.round(n*10)/10) + '%';
}
function spaceTime(n) {
let out = '';
for(let i = 0; i < n; i++) {
out += ' ';
}
return out;
}
function graphSplit(text, ns, sum) {
const len = text.length;
const widths = ns.map(n => Math.round((n / sum) * len));
var x = 0;
for (let i = 0; i < widths.length; i++) { x += widths[i]; }
widths[0] += len - x;
var index = 0;
var results = [];
for (let i = 0; i < widths.length; i++) {
const limit = index + widths[i];
results.push(text.substring(index, limit));
index = limit;
}
return results;
}
const dataRegexp = /race\.stateData\s*=\s*([^;]+)/g;
const stream = request({
method: 'GET',
headers: {'accept-encoding': 'gzip'},
uri: 'http://projects.fivethirtyeight.com/2016-election-forecast/',
}).pipe(zlib.createGunzip());
let html = '';
stream.on('data', function(chunk) {
html += chunk;
});
function printGraph(width, latest, model, metric) {
const h = latest.D.models[model][metric];
const t = latest.R.models[model][metric];
const j = latest.L.models[model][metric];
const modelLabel = rightpad(model + ':' + metric, 14, ' ');
const f = function (name, n) { return rightpad(name + ': ' + perc(n), 14); };
const prefix = ' ' + modelLabel + ' ';
const sep = ' ';
const label = prefix + f('Hillary', h) + sep + f('Trump', t) + sep + f('Johnson', j);
const graph = rightpad(label, width, ' ');
const blue = metric === 'winprob' ? 'blue' : 17;
const red = metric === 'winprob' ? 'red' : 52;
const yellow = metric === 'winprob' ? 'yellow' : 136;
const graphParts = graphSplit(graph, [h, t, j], h + t + j);
charm.foreground('white');
charm.background(blue).write(graphParts[0]);
charm.background(red).write(graphParts[1]);
charm.background(yellow).write(graphParts[2]);
charm.display('reset');
}
stream.on('end', function() {
const width = process.stdout.columns;
const data = JSON.parse(dataRegexp.exec(html)[0].replace('race.stateData = ', ''));
const latest = data.forecasts.latest;
const display = function (model, suffix) {
charm.display('reset');
printGraph(width, latest, model, 'forecast');
process.stdout.write('\n');
charm.display('bright');
printGraph(width, latest, model, 'winprob');
process.stdout.write(suffix);
}
display('now', '\n\n');
display('polls', '\n\n');
display('plus', '\n');
charm.display('reset');
process.exit();
});