-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkodetimen.go
132 lines (112 loc) · 3.17 KB
/
kodetimen.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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
)
type RegisteredSchool struct {
School
Registered bool
}
type LKKSchool struct {
Previous interface{} `json:"_previous"`
Locality string `json:"locality"`
Hash string `json:"_hash"`
Ts int64 `json:"_ts"`
ID string `json:"_id"`
PostDate string `json:"post_date"`
Updated int `json:"_updated"`
Level string `json:"level"`
County string `json:"county"`
Deleted bool `json:"_deleted"`
Address string `json:"address"`
PosLat string `json:"pos_lat"`
Students interface{} `json:"students"`
School string `json:"school"`
Year int `json:"year"`
PosLong string `json:"pos_long"`
}
func getRegisteredSchools() ([]RegisteredSchool, error) {
registeredSchools, err := getSchools()
if err != nil {
fmt.Println(err)
return []RegisteredSchool{}, err
}
allNorwegianSchools, err := parseSchoolLocations()
if err != nil {
return []RegisteredSchool{}, err
}
schools := []RegisteredSchool{}
for _, school := range allNorwegianSchools {
reg := false
for i, registered := range registeredSchools {
if registered.School != "" && strings.Contains(school.Name, registered.School) && registered.Locality == school.MunicipalityName {
reg = true
// remove from slice so we don't add it later on
registeredSchools[i].School = ""
break
}
}
schools = append(schools, RegisteredSchool{school, reg})
}
// Add all schools registered for the hour of code but are not in the
// dataset from kartvertket.
for _, school := range registeredSchools {
if school.School != "" {
school.PosLat = strings.TrimPrefix(school.PosLat, "~f")
school.PosLong = strings.TrimPrefix(school.PosLong, "~f")
lat, err := strconv.ParseFloat(school.PosLat, 64)
if err != nil {
continue
}
long, _ := strconv.ParseFloat(school.PosLong, 64)
if err != nil {
continue
}
p := Point{"", lat, long}
s := School{Point: p, Name: school.School}
schools = append(schools, RegisteredSchool{s, true})
}
}
return schools, nil
}
func getSchools() ([]LKKSchool, error) {
resp, err := http.Get("https://open.sesam.io/api/api/datasets/skoler_med_kodetimen/entities")
if err != nil {
return []LKKSchool{},
errors.Wrap(err, "Could not fetch data from open.sesam.io")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []LKKSchool{}, errors.Wrap(err, "Could not read http body")
}
var schools []LKKSchool
err = json.Unmarshal(body, &schools)
if err != nil {
return []LKKSchool{}, errors.Wrap(err, "Could not unmarshal json from open.sesam.io")
}
return schools, nil
}
func inSlice(str string, slice []string) bool {
for _, a := range slice {
if str == a {
return true
}
}
return false
}
func sortSchools(schools []RegisteredSchool) []RegisteredSchool {
var registered, notRegistered []RegisteredSchool
for _, school := range schools {
if school.Registered {
registered = append(registered, school)
} else {
notRegistered = append(notRegistered, school)
}
}
return append(notRegistered, registered...)
}