-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (119 loc) · 2.52 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"sort"
"time"
pb "github.com/GlennTatum/GoMTA/mta"
"github.com/golang/protobuf/proto"
"io"
"log"
"net/http"
)
type Client struct {
accessToken string
http *http.Client
}
type Message struct {
header Header
stopTimeUpdate StopTimeUpdate
}
type Header struct {
message pb.FeedMessage
}
type StopTimeUpdate struct {
message pb.FeedMessage
}
func NewClient(token string) *Client {
base := &http.Client{
Timeout: time.Second * 10,
}
return &Client{
accessToken: token,
http: base,
}
}
func (c *Client) recieve(url string) []byte {
client := c.http
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add("x-api-key", c.accessToken)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return body
}
func (c *Client) getFeed(url string) Message {
resp := c.recieve(url)
feed := &pb.FeedMessage{}
err := proto.Unmarshal(resp, feed)
if err != nil {
log.Fatal(err)
}
return Message{header: Header{*feed}, stopTimeUpdate: StopTimeUpdate{*feed}}
}
type TripId struct {
tripid string
}
type Stop struct {
stop string
}
type ArrivalTime struct {
arrivalTime int
}
type DateTime struct {
datetime string
}
type FormattedStopTimeUpdate struct {
datetime DateTime
stop Stop
}
func UnixtoDateTime(t int) string {
datetime := time.Unix(int64(t), 0)
return datetime.String()
}
func (stc *StopTimeUpdate) filter(stopid string) []FormattedStopTimeUpdate {
feed := stc.message
ul := make(map[ArrivalTime]Stop)
for _, entity := range feed.Entity {
if entity.TripUpdate != nil {
tripUpdate := entity.TripUpdate
stopTimeUpdate := tripUpdate.StopTimeUpdate
for _, s := range stopTimeUpdate {
if *s.StopId == stopid {
ul[ArrivalTime{int(*s.Arrival.Time)}] = Stop{*s.StopId}
}
}
}
}
ol := make([]FormattedStopTimeUpdate, 0, len(ul))
keys := make([]int, 0, len(ul))
for k := range ul {
keys = append(keys, k.arrivalTime)
}
sort.Ints(keys)
for i, _ := range keys {
ol = append(ol, FormattedStopTimeUpdate{DateTime{UnixtoDateTime(keys[i])}, Stop{stopid}})
}
return ol
}
func main() {
mtaclient := NewClient(
"ACCESS_KEY",
)
mta := mtaclient.getFeed("https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-ace")
x := mta.stopTimeUpdate.filter("A02N")
for _, item := range x {
fmt.Println(item)
}
}