-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_notify.go
99 lines (93 loc) · 2.17 KB
/
open_notify.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
92
93
94
95
96
97
98
99
package open_notify
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var URLS = struct{ PEOPLE, ISS_NOW string }{
"http://api.open-notify.org/astros.json",
"http://api.open-notify.org/iss-now.json",
}
type __ISSLocation struct {
Message string `json:"message"`
Timestamp int64 `json:"timestamp"`
Location struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
} `json:"iss_position"`
}
type ISSLocation struct {
Message string
DateTime time.Time
Location struct {
Latitude float64
Longitude float64
}
}
type PeopleInSpace struct {
Number uint `json:"number"`
Message string `json:"message"`
People []struct {
Name string `json:"name"`
Craft string `json:"craft"`
} `json:"people"`
}
func get(url string) (data []byte, err error) {
var resp *http.Response
resp, err = http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return data, nil
}
// Gets the current location of ISS (International Space Station)
func GetISSLocation() (*ISSLocation, error) {
data, err := get(URLS.ISS_NOW)
if err != nil {
return nil, err
}
temp_iss_loc := new(__ISSLocation)
err = json.Unmarshal(data, &temp_iss_loc)
if err != nil {
return nil, err
}
lat, err := strconv.ParseFloat(temp_iss_loc.Location.Latitude, 64)
if err != nil {
return nil, err
}
long, err := strconv.ParseFloat(temp_iss_loc.Location.Longitude, 64)
if err != nil {
return nil, err
}
return &ISSLocation{
Message: temp_iss_loc.Message,
Location: struct {
Latitude float64
Longitude float64
}{
Latitude: lat,
Longitude: long,
},
DateTime: time.Unix(temp_iss_loc.Timestamp, 0),
}, nil
}
// Get the current number of people in space. It also returns the names and spacecraft those people are on.
func GetPeopleInSpace() (*PeopleInSpace, error) {
data, err := get(URLS.PEOPLE)
if err != nil {
return nil, err
}
ppl := new(PeopleInSpace)
err = json.Unmarshal(data, &ppl)
if err != nil {
return nil, err
}
return ppl, nil
}