Created
April 3, 2021 10:09
-
-
Save arp242/09ec9e51422e8b3aac91b4ae36a552dc 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
17 maart 2021 | |
Rechts 65 | |
VVD 34 | |
PVV 17 | |
FvD 8 | |
JA21 3 | |
SGP 3 | |
Links 41 | |
SP 9 | |
PvdA 9 | |
GL 8 | |
PvdD 6 | |
CU 5 | |
DENK 3 | |
BIJ1 1 | |
Midden 44 | |
D66 24 | |
CDA 15 | |
Volt 3 | |
50PLUS 1 | |
BBB 1 | |
15 maart 2017 | |
Rechts 58 | |
VVD 33 | |
PVV 20 | |
SGP 3 | |
FvD 2 | |
Links 50 | |
GL 14 | |
SP 14 | |
PvdA 9 | |
CU 5 | |
PvdD 5 | |
DENK 3 | |
Midden 42 | |
CDA 19 | |
D66 19 | |
50PLUS 4 | |
12 september 2012 | |
Rechts 59 | |
VVD 41 | |
PVV 15 | |
SGP 3 | |
Links 64 | |
PvdA 38 | |
SP 15 | |
CU 5 | |
GL 4 | |
PvdD 2 | |
Midden 27 | |
CDA 13 | |
D66 12 | |
50PLUS 2 | |
9 juni 2010 | |
Rechts 57 | |
VVD 31 | |
PVV 24 | |
SGP 2 | |
Links 62 | |
PvdA 30 | |
SP 15 | |
GL 10 | |
CU 5 | |
PvdD 2 | |
Midden 31 | |
CDA 21 | |
D66 10 | |
22 november 2006 | |
Rechts 33 | |
VVD 22 | |
PVV 9 | |
SGP 2 | |
Links 73 | |
PvdA 33 | |
SP 25 | |
GL 7 | |
CU 6 | |
PvdD 2 | |
Midden 44 | |
CDA 41 | |
D66 3 | |
22 januari 2003 | |
Rechts 38 | |
VVD 28 | |
LPF 8 | |
SGP 2 | |
Links 62 | |
PvdA 42 | |
SP 9 | |
GL 8 | |
CU 3 | |
Midden 50 | |
CDA 44 | |
D66 6 | |
15 mei 2002 | |
Rechts 54 | |
LPF 26 | |
VVD 24 | |
SGP 2 | |
LN 2 | |
Links 46 | |
PvdA 23 | |
GL 10 | |
SP 9 | |
CU 4 | |
Midden 50 | |
CDA 43 | |
D66 7 | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io" | |
"net/http" | |
"regexp" | |
"strconv" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
"zgo.at/zli" | |
) | |
func get(url string) string { | |
resp, err := http.Get(url) | |
zli.F(err) | |
defer resp.Body.Close() | |
bb, err := io.ReadAll(resp.Body) | |
zli.F(err) | |
b := string(bb) | |
if resp.StatusCode != 200 { | |
zli.Fatalf("%d %s: %s", resp.StatusCode, resp.Status, b) | |
} | |
return b | |
} | |
type Uitslag struct { | |
Info struct { | |
Date string `json:"Date"` | |
} `json:"Info"` | |
StemRegio struct { | |
Partij []Partij `json:"Partij"` | |
} `json:"StemRegio"` | |
} | |
type jsonInt int | |
func (j *jsonInt) UnmarshalText(v []byte) error { | |
i, err := strconv.Atoi(string(v)) | |
*j = jsonInt(i) | |
return err | |
} | |
type Partij struct { | |
AantalZetels jsonInt `json:"AantalZetels"` | |
Naam string `json:"Naam"` | |
} | |
type Verdeling struct { | |
Datum string | |
Rechts []Partij | |
Midden []Partij | |
Links []Partij | |
} | |
var nameMap = map[string]string{ | |
"PVV (Partij voor de Vrijheid)": "PVV", | |
"Staatkundig Gereformeerde Partij (SGP)": "SGP", | |
"Forum voor Democratie": "FvD", | |
"SP (Socialistische Partij)": "SP", | |
"Partij van de Arbeid (P.v.d.A.)": "PvdA", | |
"Partij voor de Dieren": "PvdD", | |
"Democraten 66 (D66)": "D66", | |
"Christen Democratisch Appèl (CDA)": "CDA", | |
"GROENLINKS": "GL", | |
"ChristenUnie": "CU", | |
} | |
func main() { | |
page := get("https://www.verkiezingsuitslagen.nl/verkiezingen?s=Tweede+Kamer&v=&t=") | |
pages := regexp.MustCompile(`TK[0-9]+`).FindAllString(page, -1) | |
var v []Verdeling | |
for _, p := range pages { | |
if !strings.HasPrefix(p, "TK20") { // Vanaf 2000 | |
continue | |
} | |
data := get("https://www.verkiezingsuitslagen.nl/verkiezingen/detail/" + p) | |
doc, err := goquery.NewDocumentFromReader(strings.NewReader(data)) | |
zli.F(err) | |
js := doc.Find(`#UitslagData`).Text() | |
var u Uitslag | |
err = json.Unmarshal([]byte(js), &u) | |
zli.F(err) | |
vv := Verdeling{Datum: u.Info.Date} | |
for _, p := range u.StemRegio.Partij { | |
if p.AantalZetels == 0 { | |
continue | |
} | |
n, ok := nameMap[p.Naam] | |
if ok { | |
p.Naam = n | |
} | |
switch p.Naam { | |
case "VVD", "SGP", "LN", "JA21", "LPF", "PVV", "FvD": | |
vv.Rechts = append(vv.Rechts, p) | |
case "PvdA", "GL", "CU", "PvdD", "BIJ1", "SP", "DENK": | |
vv.Links = append(vv.Links, p) | |
case "CDA", "D66", "50PLUS", "Volt", "BBB": | |
vv.Midden = append(vv.Midden, p) | |
default: | |
zli.Errorf("niet verdeeld: %q", p.Naam) | |
} | |
} | |
v = append(v, vv) | |
} | |
showP := func(n string, pp []Partij) { | |
t := 0 | |
b := new(strings.Builder) | |
for _, p := range pp { | |
fmt.Fprintln(b, " ", p.Naam, strings.Repeat(" ", 16-len(p.Naam)), p.AantalZetels) | |
t += int(p.AantalZetels) | |
} | |
fmt.Println(" ", n, " ", t) | |
fmt.Println(b.String()) | |
} | |
for _, vv := range v { | |
zli.Colorln(vv.Datum, zli.Bold) | |
showP("Rechts", vv.Rechts) | |
showP("Links", vv.Links) | |
showP("Midden", vv.Midden) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool script! Dank voor het delen.