/
funSpace.go
80 lines (67 loc) · 2 KB
/
funSpace.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
package utils
import (
"fmt"
"log"
)
// SOpenGeneric struct
type SOpenGeneric struct {
Message string `json:"message"`
}
// SOpenIssnow source : http://api.open-notify.org/iss-now.json
type SOpenIssnow struct {
SOpenGeneric
Timestamp int `json:"timestamp"`
IssPosition struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
} `json:"iss_position"`
}
// SOpenAstros source : http://api.open-notify.org/astros.json
type SOpenAstros struct {
SOpenGeneric
Number int `json:"number"`
People []struct {
Name string `json:"name"`
Craft string `json:"craft"`
} `json:"people"`
}
// SGoogleGEO source : http://maps.google.com/maps/api/geocode/json?latlng=4.1108,34.5748
type SGoogleGEO struct {
Results []struct {
FormattedAddress string `json:"formatted_address"`
} `json:"results"`
Status string `json:"status"`
}
// CheckMessage func
func (s *SOpenGeneric) CheckMessage() {
if s.Message != "success" {
log.Fatal("ERROR : Fail to load information")
return
}
}
// GetLatLong func
func (s *SOpenIssnow) GetLatLong() string {
return s.IssPosition.Latitude + "," + s.IssPosition.Longitude
}
// Space main function
func Space() {
var issnow SOpenIssnow
APICall("http://api.open-notify.org/iss-now.json", &issnow)
issnow.CheckMessage()
var geoloc SGoogleGEO
APICall("http://maps.google.com/maps/api/geocode/json?latlng="+SafeParam(issnow.GetLatLong()), &geoloc)
//fmt.Printf("--- Google Response\n%+v\n--- END", geoloc)
var astros SOpenAstros
APICall("http://api.open-notify.org/astros.json", &astros)
astros.CheckMessage()
address := " Unable to load GeoPosition (" + geoloc.Status + ")"
if len(geoloc.Results) > 0 {
address = "-> " + geoloc.Results[0].FormattedAddress
}
fmt.Println("ISS Position Now:")
fmt.Printf("Lat(%v) Long(%v)\n%s\n\n", issnow.IssPosition.Latitude, issnow.IssPosition.Longitude, address)
fmt.Printf("There are %d people in space now:\n", astros.Number)
for _, astro := range astros.People {
fmt.Printf(" - %s on %s\n", astro.Name, astro.Craft)
}
}