-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest_earthquake.go
85 lines (69 loc) · 2.14 KB
/
latest_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
package gempago
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
func LatestEarthQuake() (*EarthQuakeData, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/autogempa.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"`
Shakemap string `json:"Shakemap"`
} `json:"gempa"`
} `json:"Infogempa"`
}
earthQuakeData EarthQuakeData
)
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
}
earthQuakeData.Date = data.InfoGempa.Gempa.Tanggal
earthQuakeData.Hour = data.InfoGempa.Gempa.Jam
earthQuakeData.DateTime = &data.InfoGempa.Gempa.DateTime
earthQuakeData.Coordinates = data.InfoGempa.Gempa.Coordinates
coords := strings.Split(earthQuakeData.Coordinates, ",")
earthQuakeData.Latitude, _ = strconv.Atoi(strings.TrimSpace(coords[0]))
earthQuakeData.Longitude, _ = strconv.Atoi(strings.TrimSpace(coords[1]))
earthQuakeData.Magnitude, _ = strconv.Atoi(data.InfoGempa.Gempa.Magnitude)
earthQuakeData.Depth = data.InfoGempa.Gempa.Kedalaman
earthQuakeData.Region = data.InfoGempa.Gempa.Wilayah
earthQuakeData.Potential = data.InfoGempa.Gempa.Potensi
earthQuakeData.AffectedRegion = data.InfoGempa.Gempa.Dirasakan
earthQuakeData.Shakemap = fmt.Sprintf("%s/%s", _BMKG, data.InfoGempa.Gempa.Shakemap)
return &earthQuakeData, nil
}