Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions backend/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,18 @@ async function getData() {
});
const body = await response.text();
const dom = new JSDOM(body);
let data = Array.from(dom.window.document.querySelectorAll("td"))
.map((td) => td.textContent)
.filter((text) => text !== "Röster");
const rawData = Array.from(dom.window.document.querySelectorAll("td"))
.map((td) => td.textContent);

// remove "Section","Votes",
if (data[0] === "Section" && data[1] === "Votes") {
data.shift();
data.shift();
}

// remove null
if (data[1] == "Status" || data[1] == "You voted 260420") {
data.shift();
data.shift();
}

// rewrite the data array to an object
data = data.reduce((acc, curr, i) => {
if (i % 2 === 0) {
acc[curr] = Number.parseInt(data[i + 1]);
let data = {};
// the data is in the format "A", "47", "AE", "3", etc. so we loop through the array and check if the next element is a number, if it is we add it to the data object
// maybe it would be better to also have a valid list of keys
for (let i = 0; i < rawData.length - 1; i++) {
if (/^\d+$/.test(rawData[i + 1])) {
data[rawData[i]] = Number.parseInt(rawData[i + 1]);
i++;
}
return acc;
}, {});
}

// check if data is empty
if (data.length === 0) {
Expand Down
1 change: 1 addition & 0 deletions backend/history/2026.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ getData().then((result) => {
data = result;
});

// create the history folder if it doesn't exist
fs.mkdir("./history", { recursive: true }, (err) => {
if (err) {
console.error("Error creating folder:", err);
}
});

// read history from ./history/year.json
let history = {};
fs.readFile(`./history/${year}.json`, "utf8", (err, jsonString) => {
Expand Down
Loading