-
Notifications
You must be signed in to change notification settings - Fork 0
/
gempa.go
69 lines (56 loc) · 1.31 KB
/
gempa.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
package bmkg
import (
"encoding/xml"
"fmt"
"io/ioutil"
"time"
"net/http"
)
// Infogempa get data infogempa from bmkg
type Infogempa struct {
Gempa []Gempa `xml:"gempa"`
}
// Gempa get gempa data from bmkg
type Gempa struct {
Tanggal string `xml:"Tanggal"`
Jam string `xml:"Jam"`
Point Coordinate `xml:"point"`
Lintang string `xml:"Lintang"`
Bujur string `xml:"Bujur"`
Magnitude string `xml:"Magnitude"`
Kedalaman string `xml:"Kedalaman"`
Symbol string `xml:"_symbol"`
Wilayah string `xml:"Wilayah"`
}
// Coordinate get coordinate data or location form bmkg
type Coordinate struct {
Coordinates string `xml:"coordinates"`
}
// GempaTerkini return gempa data from bmkg
func GempaTerkini(url string) (*Infogempa, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error http bad request: %v", http.StatusBadRequest)
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var info Infogempa
err = xml.Unmarshal(buf, &info)
if err != nil {
return nil, err
}
return &info, nil
}