Skip to content

Instantly share code, notes, and snippets.

@bwbroersma
Created July 29, 2022 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwbroersma/7ae3c6d2211a14220bb12343dc2529ab to your computer and use it in GitHub Desktop.
Save bwbroersma/7ae3c6d2211a14220bb12343dc2529ab to your computer and use it in GitHub Desktop.
De 'is het electoraal logisch dat er ontbindingsverkiezingen komen'-AWK script.
BEGIN {
FS=";";
}
function abs(v) {
return v < 0 ? -v : v;
}
function cp(i1, v1, i2, v2) {
if (v1 > v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}
FNR != NR {
if (FNR == 1) next;
projected[toupper($1)] = $6;
next;
}
{
name[toupper($1)] = $1;
seats[toupper($1)] = $2;
}
END {
for (j in projected) {
if (!(j in seats)) {
seats[j] = 0;
name[j] = j;
}
}
n = asorti(seats, dest, "cp");
for (ii in dest) {
i = dest[ii];
diff = projected[i] - seats[i];
printf "%16s %#2i ⇒ %#2i ", name[i], seats[i], projected[i];
if (diff == 0) {
print "";
} else {
if (diff > 0) {
c = "\033[1;42m▲\033[0m";
} else {
c = "\033[1;41m▼\033[0m";
};
printf "%-*s [%s%]\n", (16 + abs(diff) * 11),
gensub(/ /, c, "g", sprintf("%*s", abs(diff), "")),
seats[i] == 0 ? " + ∞ " : sprintf("%+#5i", diff * 100 / seats[i]);
};
if (seats[i] < projected[i]) {
gt += seats[i];
shift += diff;
}
if (seats[i] == projected[i]) eq += seats[i];
if (seats[i] > projected[i]) lt += seats[i];
}
printf "%#16s %#2i winnen %+#2i zetels, %#2i blijven gelijk, %#2i verliezen zetels.\n",
"Peilingwijzer:", gt, shift, eq, lt, loss;
}
@bwbroersma
Copy link
Author

See my twitter 🧵 thread.
Usable with:

awk -f tk-vs-peilingwijzer.awk \
<(curl --compressed -GsSfA '' 'https://gegevensmagazijn.tweedekamer.nl/OData/v4/2.0/Fractie' \
  -d '$select=Afkorting,AantalZetels' --data-urlencode '$filter=AantalZetels gt 0 and DatumInactief eq null' \
  | jq -r '.value|map("\(.Afkorting);\(.AantalZetels)")[]') \
<(curl -LsSfA '' -o - 'https://d1bjgq97if6urz.cloudfront.net/Public/Peilingwijzer/PW2021/Last/Cijfers_Peilingwijzer.xlsx' \
  | ssconvert --import-type=Gnumeric_Excel:xlsx -T Gnumeric_stf:stf_assistant -O 'separator=; format=raw' fd://0 fd://1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment