/
wsf.go
79 lines (61 loc) · 1.82 KB
/
wsf.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
package main
import (
"fmt"
"net/http"
"io/ioutil"
"encoding/json"
"regexp"
"time"
"strconv"
"os"
)
type Schedule struct {
ScheduleStart string
ScheduleEnd string
ScheduleID int
ScheduleName string
TerminalCombos []struct {
ArrivingTerminalName string
DepartingTerminalName string
SailingNotes string
Times []struct {
ArrivingTime string
DepartingTime string
VesselName string
VesselPositionNum int
} `json:Times`
} `json:TerminalCombos`
}
func apidate(date string) time.Time {
regex := regexp.MustCompile(`/Date\(([0-9]+)000-0[\d]00\)/`)
time_string := regex.FindStringSubmatch(date)[1]
time_int, _ := strconv.Atoi(time_string)
time := time.Unix(int64(time_int), 0)
return(time)
}
func main() {
wsf_api_key := os.Getenv("WSFAPI")
wsf_api_url := "http://www.wsdot.wa.gov/Ferries/API/Schedule/rest/scheduletoday/5/TRUE"
var wsf_sched_url string
if len(wsf_api_key) > 1 {
wsf_sched_url = fmt.Sprintf("%s?apiaccesscode=%s", wsf_api_url, wsf_api_key)
} else {
fmt.Println("To use this tool you must set the WSFAPI environment variable with your WSF API Access Code.")
fmt.Println("Obtain an access code at http://www.wsdot.wa.gov/traffic/api/")
os.Exit(1)
}
r, _ := http.Get(wsf_sched_url)
b, _ := ioutil.ReadAll(r.Body)
//fmt.Println(string(b))
var s Schedule
json.Unmarshal(b, &s)
fmt.Println(s.ScheduleName)
for _, v := range s.TerminalCombos {
//fmt.Printf("Got: %d - %+v\n", k, v)
fmt.Println(v.DepartingTerminalName, "-->", v.ArrivingTerminalName, v.SailingNotes)
for _, times := range v.Times {
time := apidate(times.DepartingTime)
fmt.Printf(" %2d:%02d -- %s (%d)\n", time.Hour(), time.Minute(), times.VesselName, times.VesselPositionNum)
}
}
}