forked from andrewl/wherearetheyflyingto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destinationfinder_flightaware.go
45 lines (37 loc) · 1.43 KB
/
destinationfinder_flightaware.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
// Package destinationfinder provides functionality for
// determining where a flight with a given callsign is
// destined.
package destinationfinder
import (
"errors"
"io/ioutil"
"net/http"
"strings"
)
type FlightAwareDestinationFinder struct {
}
/**
* Retrieves the lat long of the destination (as a simple string, we're not interested in doing
* any real processing with this, just using it as an index. Uses the flightaware website as
* a datasource, and parses some js embedded in the page. As such this is potentially
* brittle, but the function defintion should stand, even if we were to plugin a different
* data source.
**/
func (destination_finder *FlightAwareDestinationFinder) GetDestinationFromCallsign(callsign string) (lat_long string, err error) {
flight_url := "http://flightaware.com/live/flight/" + callsign
resp, err := http.Get(flight_url)
if err != nil {
return "", errors.New("Failed to retrieve flight details from " + flight_url)
}
defer resp.Body.Close()
flightaware_html, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("Failed to retrieve flight details from " + flight_url)
}
if strings.Index(string(flightaware_html), "destinationPoint") == -1 {
return "", errors.New("Failed to destinationPoint in html " + flight_url)
}
tmp_strings := strings.Split(string(flightaware_html), "destinationPoint\":[")
lat_long = strings.Split(tmp_strings[1], "]")[0]
return lat_long, nil
}