-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
64 lines (53 loc) · 1.36 KB
/
fetch.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"io"
)
type timepoint struct {
Time time.Time
Precipation float64
}
func parseResponse(r io.ReadCloser) ([]timepoint, error){
defer r.Close()
decoder := json.NewDecoder(r)
var item struct {
Forecasts []struct {
Time string `json:"datetime"`
Precipation float64 `json:"precipation"`
} `json:"forecasts"`
}
if err := decoder.Decode(&item); err != nil {
return []timepoint{}, err
}
var res []timepoint
for _, item := range item.Forecasts {
time, err := time.Parse("2006-01-02T15:04:05", item.Time)
if err != nil {
return []timepoint{}, err
}
res = append(res, timepoint{
Time: time,
Precipation: item.Precipation,
})
}
return res, nil
}
func fetchTwoHours(latitude float64, longitude float64) ([]timepoint, error) {
url := fmt.Sprintf("https://graphdata.buienradar.nl/2.0/forecast/geo/rain/?lat=%f&lon=%f", latitude, longitude)
resp, err := http.Get(url)
if err != nil {
return []timepoint{}, err
}
return parseResponse(resp.Body)
}
func fetchFullDay(latitude float64, longitude float64) ([]timepoint, error) {
url := fmt.Sprintf("https://graphdata.buienradar.nl/2.0/forecast/geo/rain24hour/?lat=%f&lon=%f", latitude, longitude)
resp, err := http.Get(url)
if err != nil {
return []timepoint{}, err
}
return parseResponse(resp.Body)
}