-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
87 lines (70 loc) · 1.65 KB
/
main.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
package main
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
for _, district := range []string{"801", "802", "803", "804", "805", "807", "809", "811", "813", "814", "816"} {
fmt.Println(district)
resp, err := http.Get("http://ratings.food.gov.uk/OpenDataFiles/FHRS" + district + "en-GB.xml")
if err != nil {
fmt.Println(err)
continue
}
defer resp.Body.Close()
var fhrsData = FHRSEstablishment{}
err = xml.NewDecoder(resp.Body).Decode(&fhrsData)
if err != nil {
fmt.Println(err)
continue
}
if fhrsData.Header.ReturnCode != "Success" {
fmt.Println(fhrsData.Header.ReturnCode)
continue
}
file, err := os.Create("data/" + district + ".csv")
if err != nil {
fmt.Println(err)
break
}
defer file.Close()
for _, establishment := range fhrsData.EstablishmentCollection.EstablishmentDetail {
if establishment.PostCode == "" {
continue
}
if establishment.Geocode.Latitude == "" {
continue
}
if establishment.Geocode.Longitude == "" {
continue
}
var postcode = strings.Trim(establishment.PostCode, " ")
postcode = strings.ToUpper(postcode)
var inwards = postcode[len(postcode)-3:]
var outwards = strings.Trim(postcode[0:len(postcode)-3], " ")
if inwards[0:1] == "O" {
inwards = "0" + inwards[1:]
}
postcode = outwards + " " + inwards
if len(postcode) > 8 {
continue
}
if len(postcode) < 6 {
continue
}
_, err = io.WriteString(file,
postcode+","+
establishment.Geocode.Latitude+","+
establishment.Geocode.Longitude+"\n")
}
err = file.Sync()
if err != nil {
fmt.Println(err)
break
}
}
}