-
Notifications
You must be signed in to change notification settings - Fork 1
/
election-senate.js
37 lines (30 loc) · 1.7 KB
/
election-senate.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
var { JSDOM } = require('jsdom');
var request = require('request');
var response_url;
module.exports = function (context, req, res) {
request(
'https://floridaelectionwatch.gov/FederalOffices/USSenator',
function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
// console.log('body:', body); // Print the HTML for the Google homepage.
const dom = new JSDOM(body);
const rows = dom.window.document.querySelectorAll('.grid-row')
var repCount = parseInt(rows[0].querySelector('span').textContent.replace(",", "").replace(",", ""))
var repPercent = rows[0].querySelector('.progressbar').textContent
var demCount = parseInt(rows[1].querySelector('span').textContent.replace(",", "").replace(",", ""))
var demPercent = rows[1].querySelector('.progressbar').textContent
var diffCount = Math.abs(repCount - demCount)
var diffPercent = ((diffCount / (repCount + demCount))*100).toFixed(2)
var output = '';
output += 'Voldemort - ' + repPercent + ' - ' + repCount.toLocaleString('us') + "\n"
output += 'Nelson - ' + demPercent + ' - ' + demCount.toLocaleString('us') + "\n"
output += 'Difference - ' + diffPercent + '% - ' + diffCount.toLocaleString('us')
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({
response_type: "in_channel",
text: output,
}));
}
);
}