-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
departures.go
398 lines (352 loc) · 9.75 KB
/
departures.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/fatih/color"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gopkg.in/AlecAivazis/survey.v1"
)
func main() {
// parse the command line arguments
id := flag.String("id", "", "ID of the stop")
filterMode := flag.String("filter-mode", "", "Filter the list for this mode of transporation (Comma separated)")
filterDestination := flag.String("filter-destination", "", "Filter the list for this destination (Comma separated)")
filterLine := flag.String("filter-line", "", "Filter the list for this line (Comma separated)")
width := flag.Int("width", intEnv("WTF_WIDGET_WIDTH"), "Width of the output")
retries := flag.Int("retries", 3, "Number of retries before giving up")
retryPause := flag.Duration("retry-pause", time.Second, "Pause between retries")
min := flag.Int("min", 60, "Number of minutes you want to see the departures for")
forceColor := flag.Bool("force-color", false, "Use this flag to enforce color output even if the terminal does not report support")
search := flag.String("search", "", "Search for the stop name to get the stop ID")
stationName := flag.String("station", "", "Fetch departures for given station. Ignored if ID is provided")
verbose := flag.Bool("verbose", false, "Be verbose and show additional information (mode of transportataion, operator and additional remarks)")
bicycle := flag.Bool("bicycle", false, "Only show connections that allow bicycle conveyance")
flag.Parse()
// ensure valid retry values
if *retries < 0 {
*retries = 0
}
if *retryPause < 0 {
*retryPause = 0
}
var err error
// check if the user just wants to find the station ID
if *search != "" {
stations, err := searchStations(*search)
if err != nil {
fmt.Println("Could not query stations")
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
fmt.Printf("Found %d station(s):\n", len(stations))
for _, s := range stations {
fmt.Printf(" %s - %s\n", s.ID, s.Name)
}
return
}
// search of the station and provide user option to choose
if *id == "" && *stationName != "" {
s, err := promptForStation(*stationName)
if err != nil {
fmt.Println(err)
} else {
*id = s.ID
}
}
// set default id if empty
if *id == "" {
fmt.Println("station ID is empty. Defaulting to: 900000100003")
*id = "900000100003"
}
// set the color mode
color.NoColor = color.NoColor && !*forceColor
// request the departures
var deps []result
for i := 0; i < *retries+1; i++ {
err = getJSON(&deps, "https://v5.vbb.transport.rest/stops/%s/departures?duration=%d", *id, *min)
if err == nil {
break
}
time.Sleep(*retryPause)
}
if err != nil {
fmt.Println("Could not query departures")
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
// initialize the filters
fm := filterSlice(*filterMode)
fd := filterSlice(*filterDestination)
fl := filterSlice(*filterLine)
// calculate the length of the columns
var lenName, lenDir, lenDep, lenRem int
from := time.Now().Add(-2 * time.Minute)
until := time.Now().Add(time.Hour)
filteredDeps := deps[:0] // no need to waste space
for _, dep := range deps {
if dep.When.Before(from) || dep.When.After(until) {
continue
}
// trim unnecessary whitespace
dep.Line.Product = strings.TrimSpace(dep.Line.Product)
dep.Direction = strings.TrimSpace(dep.Direction)
dep.Line.Name = strings.TrimSpace(dep.Line.Name)
// apply filters
if isFiltered(fm, dep.Line.Product) {
continue
}
if isFiltered(fd, dep.Direction) {
continue
}
if isFiltered(fl, dep.Line.Name) {
continue
}
if *bicycle && filterBike(dep) {
continue
}
// the entry survived the filters, append it to the filtered list
filteredDeps = append(filteredDeps, dep)
// update the lengths
lenName = maxStringLen(dep.Line.Name, lenName)
lenDir = maxStringLen(dep.Direction, lenDir)
lenDep = maxStringLen(departureTime(dep), lenDep)
lenRem = maxStringLen("Operator", lenRem)
for _, rem := range dep.Remarks {
lenRem = maxStringLen(rem.Type, lenRem)
}
}
// adjust the column length
if *width > 0 {
lenDir = *width - lenName - lenDep - 2
if lenDir < 1 {
lenDir = 1
}
}
// render the columns
for _, dep := range filteredDeps {
departureColor := color.HiGreenString
if dep.Delay > 0 {
departureColor = color.RedString
} else if dep.Delay < 0 {
departureColor = color.YellowString
}
fmt.Println(
color.WhiteString("%s", leftPad(dep.Line.Name, lenName)),
rightPad(dep.Direction, lenDir),
departureColor("%s", departureTime(dep)),
)
if !*verbose {
continue
}
remarkColor := color.WhiteString
fmt.Println(
remarkColor("%s", leftPad(rightPad("Operator", lenRem), (lenRem+lenName+1))),
":",
dep.Line.Operator.Name,
)
fmt.Println(
remarkColor("%s", leftPad(rightPad("Type", lenRem), (lenRem+lenName+1))),
":",
dep.Line.Product,
)
for _, rem := range dep.Remarks {
if rem.Text != "" {
rem.Type = cases.Title(language.English).String(rem.Type)
if rem.Type == "Warning" {
remarkColor = color.RedString
}
fmt.Println(
remarkColor("%s", leftPad(rightPad(rem.Type, lenRem), (lenRem+lenName+1))),
":",
rem.Text,
)
}
}
}
}
func searchStations(name string) ([]station, error) {
var stations []station
err := getJSON(&stations, "https://v5.vbb.transport.rest/locations?query=%s&poi=false&addresses=false", name)
return stations, err
}
func promptForStation(name string) (*station, error) {
stations, err := searchStations(name)
if err != nil {
return nil, fmt.Errorf("could not query stations")
}
if l := len(stations); l == 0 {
// no stations found
return nil, fmt.Errorf("could not find matching stations")
} else if l == 1 {
return &stations[0], nil
}
// set first result as fallback
fallback := stations[0].Name
// convert to map[string]station to get station after user prompt
var options []string
optionStation := map[string]station{}
for _, s := range stations {
options = append(options, s.Name)
optionStation[s.Name] = s
}
prompt := &survey.Select{
Message: "Choose a station:",
Options: options,
Default: fallback,
}
var choice string
if err = survey.AskOne(prompt, &choice, nil); err != nil {
fmt.Println("Failed to get answer on station list. Defaulting to", fallback)
choice = fallback
}
s := optionStation[choice]
return &s, nil
}
func getJSON(v interface{}, urlFormat string, values ...interface{}) error {
resp, err := http.Get(fmt.Sprintf(urlFormat, values...))
if err != nil {
return err
}
defer resp.Body.Close()
d := json.NewDecoder(resp.Body)
return d.Decode(v)
}
type result struct {
TripID string `json:"tripId"`
Stop struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
Location struct {
Type string `json:"type"`
ID string `json:"id"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
} `json:"location"`
Products struct {
Suburban bool `json:"suburban"`
Subway bool `json:"subway"`
Tram bool `json:"tram"`
Bus bool `json:"bus"`
Ferry bool `json:"ferry"`
Express bool `json:"express"`
Regional bool `json:"regional"`
} `json:"products"`
} `json:"stop"`
When time.Time `json:"when"`
Direction string `json:"direction"`
Line struct {
Type string `json:"type"`
ID string `json:"id"`
FahrtNr string `json:"fahrtNr"`
Name string `json:"name"`
Public bool `json:"public"`
Mode string `json:"mode"`
Product string `json:"product"`
Operator struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
} `json:"operator"`
Symbol string `json:"symbol"`
Nr int `json:"nr"`
Metro bool `json:"metro"`
Express bool `json:"express"`
Night bool `json:"night"`
} `json:"line"`
Remarks []struct {
Type string `json:"type"`
Code string `json:"code"`
Text string `json:"text"`
} `json:"remarks"`
Delay int `json:"delay"`
Platform string `json:"platform"`
}
type station struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
Location struct {
Type string `json:"type"`
ID string `json:"id"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
} `json:"location"`
Products struct {
Suburban bool `json:"suburban"`
Subway bool `json:"subway"`
Tram bool `json:"tram"`
Bus bool `json:"bus"`
Ferry bool `json:"ferry"`
Express bool `json:"express"`
Regional bool `json:"regional"`
} `json:"products"`
}
func leftPad(s string, l int) string {
r := []rune(s)
if len(r) >= l {
return string(r[:l])
}
return strings.Repeat(" ", l-len(r)) + string(r)
}
func rightPad(s string, l int) string {
r := []rune(s)
if len(r) >= l {
return string(r[:l])
}
return string(r) + strings.Repeat(" ", l-len(r))
}
func departureTime(r result) string {
if r.Delay == 0 {
return r.When.Format("15:04")
}
return fmt.Sprintf("%s (%+d)", r.When.Format("15:04"), r.Delay/60)
}
func filterSlice(filter string) []string {
if filter == "" {
return nil
}
fs := strings.Split(strings.ToUpper(filter), ",")
for i, f := range fs {
fs[i] = strings.TrimSpace(f)
}
return fs
}
func isFiltered(filter []string, v string) bool {
if len(filter) == 0 {
return false
}
for _, f := range filter {
if strings.EqualFold(f, v) {
return false
}
}
return true
}
func maxStringLen(s string, l int) int {
c := utf8.RuneCountInString(s)
if c > l {
return c
}
return l
}
func intEnv(key string) int {
i, _ := strconv.Atoi(os.Getenv(key))
return i
}
func filterBike(r result) bool {
for _, rem := range r.Remarks {
if strings.TrimSpace(rem.Code) == "FB" {
return false
}
}
return true
}