-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (97 loc) · 2.47 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
)
const (
arrivalsUrl string = "http://fis.com.mv/xml/arrive.xml"
departuresUrl string = "http://fis.com.mv/xml/depart.xml"
)
type Flights []struct {
UpdateTime string `json:"update_time" xml:"UpdateTime"`
Flight []Flight `json:"flights" xml:"Flight"`
}
type Flight struct {
AirLineID string `json:"update_time" xml:"AirLineID"`
AirLineName string `json:"airline_name" xml:"AirLineName"`
FlightID string `json:"flight_id" xml:"FlightID"`
Route1 string `json:"route_1" xml:"Route1"`
Route2 string `json:"route_2" xml:"Route2"`
Route3 string `json:"route_3" xml:"Route3"`
Route4 string `json:"route_4" xml:"Route4"`
Route5 string `json:"route_5" xml:"Route5"`
Route6 string `json:"route_6" xml:"Route6"`
Scheduled string `json:"scheduled" xml:"Scheduled"`
Estimated string `json:"estimated" xml:"Estimated"`
Status string `json:"status" xml:"Status"`
Gate string `json:"gate" xml:"Gate"`
Terminal string `json:"terminal" xml:"Terminal"`
CheckinArea string `json:"checkin_area" xml:"CheckinArea"`
Date string `json:"date" xml:"Date"`
PrimaryFlight string `json:"primary_flight" xml:"PrimaryFlight"`
CarrierType string `json:"carrier_type" xml:"CarrierType"`
}
func GetArrivals() ([]byte, error) {
resp, err := http.Get(arrivalsUrl)
if err != nil {
panic(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
data := &Flights{}
err = xml.Unmarshal(body, data)
if err != nil {
panic(err)
}
return json.Marshal(data)
}
func GetDepartures() ([]byte, error) {
resp, err := http.Get(departuresUrl)
if err != nil {
panic(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
data := &Flights{}
err = xml.Unmarshal(body, data)
if err != nil {
panic(err)
}
return json.Marshal(data)
}
func main() {
arrivals, err := GetArrivals()
if err != nil {
panic(err)
}
departures, err := GetDepartures()
if err != nil {
panic(err)
}
// Just for the sake of printing...
fmt.Printf("ARRIVALS\n")
fmt.Printf(string(arrivals))
fmt.Printf("\n\n\n")
fmt.Printf("DEPARTURES\n")
fmt.Printf(string(departures) + "\n")
}