Last active
October 30, 2022 20:52
-
-
Save pedropedruzzi/a80e1dccd2c1bf42c16c24bba97611a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://noticias.uol.com.br/eleicoes/2022/apuracao/2turno/votos-por-estado/presidente/ | |
function compute() { | |
const states = document.querySelectorAll('.president-by-state') | |
const estimates = {}; | |
let computedVotes = 0, minComputePercentPerState = 100, minComputePercentState; | |
for (const state of states) { | |
const stateName = state.querySelector('.president-by-state-label').innerText; | |
const stateVoters = Number(state.querySelector('.voters').innerText.replace(/\D/g, '')); | |
const computedPercent = parseFloat(state.querySelector('.president-by-state-apuration .percentage').innerText.replace(',', '.')); | |
computedVotes += computedPercent * stateVoters / 100; | |
minComputePercentPerState = Math.min(minComputePercentPerState, computedPercent); | |
minComputePercentState = stateName; | |
//console.log({ stateName, stateVoters }); | |
const candidates = state.querySelectorAll('.president-by-state-list .candidate'); | |
for (const candidate of candidates) { | |
const name = candidate.querySelector('.name').innerText; | |
const percentage = parseFloat(candidate.querySelector('.percentage').innerText.replace(',', '.')); | |
//console.log({ name, percentage }); | |
const votes = percentage * stateVoters / 100; | |
estimates[name] = (estimates[name] ?? 0) + votes; | |
} | |
} | |
const totalVotes = Object.values(estimates).reduce((a, b) => a + b, 0); | |
const computedPercent = computedVotes * 100 / totalVotes; | |
console.log({ totalVotes, computedVotes, computedPercent, estimates }); | |
const result = Object.entries(estimates) | |
.map(([name, votes]) => ({ name, percentage: Number((votes * 100 / totalVotes).toFixed(2)) })) | |
.sort((a, b) => b.percentage - a.percentage); | |
console.log(`${(new Date()).toLocaleString()} Projeção pelos percentuais por estado (com ${computedPercent.toFixed(2)}% dos votos apurados; sendo ${minComputePercentState} com apenas ${minComputePercentPerState}%). ${result.slice(0, 4).map(x => `${x.name}: ${x.percentage}%`).join(' | ')}`); | |
} | |
setInterval(compute, 60000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment