Created
November 11, 2019 06:56
-
-
Save krisboit/29cdc71d06ad05d0ea60fe822bd0d8a8 to your computer and use it in GitHub Desktop.
Voting results
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
const fetch = require('node-fetch') | |
const calc = (pv, pvSR, column, total) => { | |
const Romania = pv ? pv.map(p => p[column]).reduce((a,b)=>a+b, 0) : 0; | |
const Diaspora = pvSR ? pvSR.map(p => p[column]).reduce((a,b)=>a+b, 0) : 0; | |
return { | |
Romania: total ? percent(Romania, total.Romania) : Romania, | |
Diaspora: total ? percent(Diaspora, total.Diaspora) : Diaspora, | |
Total: total ? percent(Romania + Diaspora, total.Total) : Romania + Diaspora | |
} | |
} | |
const calcPrez = (prez, prezSR) => { | |
const Romania = prez.map(p => p.LT).filter(Boolean).reduce((a,b)=>a+b, 0); | |
const Diaspora = prezSR.map(p => p.LT).filter(Boolean).reduce((a,b)=>a+b, 0); | |
return { | |
Romania, | |
Diaspora, | |
Total: Romania + Diaspora | |
} | |
} | |
const percent = (n, t) => { | |
return `${('00' + (n/t * 100).toFixed(2)).slice(-5)}% (${n})` | |
} | |
const result = (pv, pvSR, prez, prezSR, columns, totalColumn) => { | |
let total = calc(pv, pvSR, totalColumn); | |
let totalPrez = calcPrez(prez, prezSR); | |
const results = {}; | |
for(let column of columns) { | |
const r = calc(pv, pvSR, column.column, total); | |
results[column.name] = r; | |
} | |
return { | |
Voturi: { | |
'Voturi': totalPrez, | |
'Voturi numarate': calc(pv, pvSR, totalColumn, totalPrez), | |
}, | |
Rezultate: results | |
} | |
} | |
const update = (async () => { | |
let data = await fetch(`https://prezenta.bec.ro/prezidentiale10112019/data/pv/json//pv_AB.json?_=${Date.now()}`).then(res => res.json()); | |
let dataSR = await fetch(`https://prezenta.bec.ro/prezidentiale10112019/data/pv/json//pv_SR.json?_=${Date.now()}`).then(res => res.json()); | |
let dataPrezSR = await fetch(`https://prezenta.bec.ro/prezidentiale10112019/data/presence/json/presence_SR_now.json?_=${Date.now()}`).then(res => res.json()); | |
let pv = data.pvs.partial.table.PRSD; | |
let pvSR = dataSR.pvs.partial.table.PRSD; | |
let prez = [ | |
{LT: 8683688} | |
]; | |
let prezSR = dataPrezSR.precinct; | |
const r = result(pv, pvSR, prez, prezSR, [ | |
{name: 'Iohannis', column: 'g1'}, | |
{name: 'Paleologu', column: 'g2'}, | |
{name: 'Barna', column: 'g3'}, | |
{name: 'Hunor', column: 'g4'}, | |
{name: 'Dancila', column: 'g5'}, | |
{name: 'Deaconu', column: 'g10'}, | |
{name: 'Cumpanasu', column: 'g14'}, | |
], 'c'); | |
let candidates = []; | |
for(let c of Object.keys(r.Rezultate)) { | |
candidates.push([c, r.Rezultate[c]]); | |
} | |
let orderedCandidates = {} | |
candidates.sort((a,b) => a[1].Total < b[1].Total).map(c => orderedCandidates[c[0]] = c[1]); | |
console.table({ | |
...r.Voturi, | |
"": {}, | |
...orderedCandidates | |
}); | |
}); | |
update(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment