-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
162 lines (127 loc) · 4.79 KB
/
main.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// const _ = require('lodash');
import got from 'got';
import _ from 'lodash';
import * as cheerio from 'cheerio';
import { CANDIDATE_NAMES, loadHistoricDatasets, predictFromDatasets, readFile, writeFile, humanizePrediction, getSizeClass } from './utils.js';
const getObecToOkresMap = (dataset) => {
const map = {};
dataset.forEach((line) => {
map[line.l2] = line.l1;
});
return map;
}
const loadUrlCached = async (kolo, davka) => {
const filename = `./cache/kolo-${kolo}/davka-${davka}.xml`;
const url = `https://www.volby.cz/pls/prez2023/vysledky_okrsky?kolo=${kolo}&davka=${davka}`;
try {
const data = await readFile(filename, {encoding: 'utf8'});
return data;
} catch(err) {
}
console.log('Downloading data', {kolo, davka});
const { body } = await got.get(url, {retry: {limit: 3}});
const $ = cheerio.load(body, { xmlMode: true});
const chyba = $('CHYBA').attr('KOD_CHYBY');
if (chyba) {
console.log('Error loading', { chyba });
return null;
}
await writeFile(filename, body);
return body;
}
const loadXMLBatch = async (kolo, davka, { obecToOkresMap, numCandidates }) => {
const body = await loadUrlCached(kolo, davka);
// No more data
if (body === null) {
return null;
}
const $ = cheerio.load(body, {xmlMode: true});
const $davka = $('DAVKA');
const okrskyCelkem = parseInt($davka.attr("OKRSKY_CELKEM"));
const okrzkyZprac = parseInt($davka.attr("OKRSKY_ZPRAC"));
const data = [];
$('OKRSEK').each((i, okrsek) => {
const $okrsek = $(okrsek);
if ($okrsek.attr('OPAKOVANE') === '1') return;
const $ucast = $okrsek.find('UCAST_OKRSEK');
const totalVoters = parseInt($ucast.attr('ZAPSANI_VOLICI'))
const totalVotes = parseInt($ucast.attr('VYDANE_OBALKY'))
const validVotes = parseInt($ucast.attr('PLATNE_HLASY'));
const votesRaw = {};
$okrsek.find('HLASY_OKRSEK').each((_, hlasy) => {
const $hlasy = $(hlasy);
const candidate = parseInt($hlasy.attr('PORADOVE_CISLO'));
const count = parseInt($hlasy.attr('HLASY'));
votesRaw[candidate] = count;
});
const votes = [];
for(let candidate = 1; candidate <= numCandidates; candidate++) {
votes.push(votesRaw[candidate] || 0);
}
// // Add invalid votes
votes.push(totalVotes - validVotes);
const l3 = parseInt($okrsek.attr('CIS_OKRSEK'));
const l2 = parseInt($okrsek.attr('CIS_OBEC'));
const l1 = obecToOkresMap[l2];
const id = `${l1}-${l2}-${l3}`;
data.push({
id,
totalVoters,
totalVotes,
votes,
l1,
l2,
sizeClass: getSizeClass(totalVoters),
votesRatio: votes.map(x => totalVotes === 0 ? 0 : x / totalVotes),
});
});
const firstRoundOkrsky = Math.floor(okrskyCelkem / 2);
return { data, meta: {
okrskyCelkem: okrskyCelkem - firstRoundOkrsky,
okrzkyZprac: okrzkyZprac - firstRoundOkrsky,
percentage: (okrzkyZprac - firstRoundOkrsky) / (okrskyCelkem - firstRoundOkrsky),
} };
}
const loadAllAvailableData = async (kolo, maxBatch, options) => {
const data = [];
let meta = null;
for(let i = 1; i <= maxBatch; i++) {
const batch = await loadXMLBatch(kolo, i, options);
// We are done
if (!batch) break;
data.push(...batch.data);
meta = batch.meta;
}
return {data, meta};
}
const main = async() => {
// Load historic datasets
const historicDatasets = await loadHistoricDatasets();
// Get map of obec to okres
const obecToOkresMap = getObecToOkresMap(historicDatasets.firstRound2023);
const {data, meta} = await loadAllAvailableData(2, 50, { obecToOkresMap, numCandidates: 9 });
if (data.length === 0) {
console.log('No results so far');
return
}
const prediction = predictFromDatasets(
[ historicDatasets.secondRound2018, historicDatasets.firstRound2023 ],
data
);
console.log(prediction);
console.log('----------------------------------------------------');
console.log('Zpracovano okrsku');
console.log(Math.floor(meta.percentage * 10000) / 100);
console.log('Odhad ucasti');
console.log(Math.floor((prediction.totalVotes / prediction.totalVoters) * 10000) / 100);
console.log('Odhad vysledku');
console.log(humanizePrediction(prediction, CANDIDATE_NAMES.FIRST_ROUND_2023).filter(x => ['Pavel', 'Babiš', 'Neplatné hlasy'].includes(x[0])));
};
// TODO:
// - download and unzip fresh data from volby.cz/opendata/opendata
// - run the prediction
// - send the prediction results to dataset
// - chart of historic data
(async () => {
await main();
})();