-
Notifications
You must be signed in to change notification settings - Fork 0
/
voters.go
85 lines (67 loc) · 1.77 KB
/
voters.go
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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/charmbracelet/log"
)
type Voters struct {
db *sql.DB
locCode string
count int
postcode string
}
type Person struct {
Gender string `json:"gender"`
Name struct {
Title string `json:"title"`
First string `json:"first"`
Last string `json:"last"`
}
Location struct {
Street struct {
Name string `json:"name"`
}
City string `json:"city"`
Postcode string `json:"postcode"`
}
}
type Results struct {
Results []Person `json:"results"`
}
func voters(db *sql.DB, locCode string, count int, postcode string) *Voters {
return &Voters{
db: db,
locCode: locCode,
count: count,
postcode: postcode,
}
}
func (v *Voters) insert() {
people, _ := http.Get(fmt.Sprintf("https://randomuser.me/api/?nat=gb&results=%d", v.count))
results := &Results{}
json.NewDecoder(people.Body).Decode(results)
log.Infof("Will insert %d voters", v.count)
for i, person := range results.Results {
title := person.Name.Title
first := person.Name.First
last := person.Name.Last
street := person.Location.Street.Name
city := person.Location.City
// postcode := "CH1 2AN"
jn := jurorNumber("0"+v.locCode, strconv.Itoa(i))
query := fmt.Sprintf("INSERT INTO juror_mod.VOTERS (LOC_CODE,PART_NO,REGISTER_LETT,POLL_NUMBER,NEW_MARKER,TITLE,FNAME,LNAME,ADDRESS,ADDRESS4,ZIP,REC_NUM) VALUES ('%s','%s','%d','%d',NULL,'%s','%s','%s','%s','%s','%s',%d);", v.locCode, jn, i, i, title, first, last, street, city, v.postcode, i)
log.Infof("Inserting voter %s", jn)
v.db.Exec(query)
}
waitForVoters.Done()
}
func jurorNumber(locCode, i string) string {
pad := 9 - len(locCode) - len(i)
for i := 0; i < pad; i++ {
locCode = locCode + "0"
}
return locCode + i
}