Repository navigation
Expand file tree
/
Copy pathelection.js
More file actions
133 lines (113 loc) · 4.03 KB
/
Copy pathelection.js
File metadata and controls
133 lines (113 loc) · 4.03 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
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
const https = require('https');
let url = "https://www.wsj.com/election-results-2020-data/president.json";
var careAbout = [ "PA", "AZ", "NV", "NC", "GA" ];
var res = null;
var pollRateSeconds = 60;
function getUpdate() {
https.get(url,(res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json = JSON.parse(body);
checkForChanges(json);
setTimeout(getUpdate, pollRateSeconds * 1000);
} catch (error) {
console.error(error.message);
};
});
}).on("error", (error) => {
console.error(error.message);
});
}
function checkForChanges(json) {
if (res && res.created == json.meta.created)
return;
var changed = false;
var first = res == null;
careAbout.forEach(function (state) {
var sData = json.data[state];
if (res == null || res[state] == null) {
console.log(state, "NEW");
} else if (sData.tv != res[state].tv) {
console.log(state, "UPDATE", " - ", timestamp());
changed = true;
} else {
return;
}
if (first) {
printState(sData, null);
// Init first time through (messy because we are caching as a hash instead of an array)
res = res || {};
res[state] = {}
res[state].tv = sData.tv;
res[state].votes = {};
sData.cand.forEach(function (candData) { res[state].votes[candData.name] = candData.votes });
} else {
printState(sData, res[state]);
res[state].tv = sData.tv;
}
});
res.created = json.meta.created;
if (first || changed)
console.log();
}
function printState(newData, oldData) {
if (oldData != null) {
var delta = newData.tv - oldData.tv
console.log(" Total Votes:", comma(newData.tv), (delta > 0 ? "+" : "") + comma(delta));
} else {
console.log(" Total Votes:", comma(newData.tv));
}
var newFirst, newSecond;
var oldFirst, oldSecond;
newData.cand.forEach(function (candData) {
if (oldData != null) {
var diff = candData.votes - oldData.votes[candData.name];
console.log(" " + candData.name + ":", comma(candData.votes), (diff > 0 ? "(+" : "(") + comma(diff) + ")");
if (newFirst == null) {
newFirst = candData.votes;
oldFirst = oldData.votes[candData.name];
} else if (newSecond == null) {
newSecond = candData.votes;
oldSecond = oldData.votes[candData.name];
}
oldData.votes[candData.name] = candData.votes;
} else {
console.log(" " + candData.name + ":", comma(candData.votes));
if (newFirst == null) {
newFirst = candData.votes;
} else if (newSecond == null) {
newSecond = candData.votes;
}
}
})
if (oldData != null) {
var diff = newFirst - newSecond - oldFirst + oldSecond;
console.log(" Lead By:", comma(newFirst - newSecond), (diff > 0 ? "(+" : "(") + comma(diff) + ")");
} else {
console.log(" Lead By:", comma(newFirst - newSecond));
}
}
function comma(x) {
x = x.toString();
var pattern = /(-?\d+)(\d{3})/;
while (pattern.test(x))
x = x.replace(pattern, "$1,$2");
return x;
}
function timestamp() {
let date = new Date();
let day = intTwoChars(date.getDate());
let month = intTwoChars(date.getMonth() + 1);
let hours = intTwoChars(date.getHours());
let minutes = intTwoChars(date.getMinutes());
let seconds = intTwoChars(date.getSeconds());
return `${month}/${day} ${hours}:${minutes}:${seconds}`;
}
function intTwoChars(i) {
return (`0${i}`).slice(-2);
}
getUpdate();