-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecents_earthquake.go
91 lines (74 loc) · 1.9 KB
/
recents_earthquake.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
package gempago
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
func RecentsEarthQuake() ([]*EarthQuakeData, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/gempaterkini.json", _BMKG), nil)
if err != nil {
return nil, err
}
var (
data struct {
InfoGempa struct {
Gempa []struct {
Tanggal string `json:"Tanggal"`
Jam string `json:"Jam"`
DateTime time.Time `json:"DateTime"`
Coordinates string `json:"Coordinates"`
Lintang string `json:"Lintang"`
Bujur string `json:"Bujur"`
Magnitude string `json:"Magnitude"`
Kedalaman string `json:"Kedalaman"`
Wilayah string `json:"Wilayah"`
Potensi string `json:"Potensi"`
Dirasakan string `json:"Dirasakan"`
} `json:"gempa"`
} `json:"Infogempa"`
}
earthQuakeData = make([]*EarthQuakeData, 0)
)
client := http.Client{
Timeout: 5 * time.Second,
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, errors.New("http status code not 200")
}
rb, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(rb, &data); err != nil {
return nil, err
}
for _, v := range data.InfoGempa.Gempa {
coords := strings.Split(v.Coordinates, ",")
lat, _ := strconv.Atoi(strings.TrimSpace(coords[0]))
long, _ := strconv.Atoi(strings.TrimSpace(coords[1]))
magnitude, _ := strconv.Atoi(v.Magnitude)
earthQuakeData = append(earthQuakeData, &EarthQuakeData{
Date: v.Tanggal,
Hour: v.Jam,
DateTime: &v.DateTime,
Coordinates: v.Coordinates,
Latitude: lat,
Longitude: long,
Magnitude: magnitude,
Depth: v.Kedalaman,
Region: v.Wilayah,
Potential: v.Potensi,
AffectedRegion: v.Dirasakan,
})
}
return earthQuakeData, nil
}