-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.go
70 lines (58 loc) · 1.92 KB
/
dialog.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
package nyctraintime
import (
"context"
"fmt"
"strconv"
"time"
"github.com/jprobinson/gtfs"
"github.com/jprobinson/gtfs/mta"
)
func (s *service) getNextTrainDialog(ctx context.Context, ft mta.FeedType, line, stop, dir string) string {
return s.getTrainDialog(ctx, ft, "next", line, stop, dir, 0)
}
func (s *service) getFollowingTrainDialog(ctx context.Context, ft mta.FeedType, line, stop, dir string) string {
return s.getTrainDialog(ctx, ft, "following", line, stop, dir, 1)
}
func (s *service) getTrainDialog(ctx context.Context, ft mta.FeedType, name, line, stop, dir string, indx int) string {
feed, err := getFeed(ctx, s.hc, s.key, ft)
if err != nil {
return fmt.Sprintf("Sorry, I'm having problems getting the subway feed. ")
}
stopLine, ok := gtfs.NYCSubwayStopsByName[stop]
if !ok {
return fmt.Sprintf("Sorry, I didn't recognise the stop \"%s\". ", stop)
}
stopID, ok := stopLine[line]
if !ok {
return fmt.Sprintf("Sorry, I didn't recognise \"%s\" as a part of the %s line. ",
stop, line)
}
_, north, south := mta.FeedNextTrainTimes(feed, stopID, line)
var trains []time.Time
if gtfs.NYCSubwayRoutes[line].Northbound == dir || dir == "uptown" || dir == "Northbound" {
trains = north
} else {
trains = south
}
if len(trains) < indx+1 {
return fmt.Sprintf("Sorry, the %s train time is not available for %s bound %s trains at %s. ",
name, dir, line, stop)
}
out := timeSpeak(trains[indx], name, line, stop, dir)
if len(trains) >= indx+2 {
out += timeSpeak(trains[indx+1], "following", line, stop, dir)
}
return out
}
func timeSpeak(t time.Time, name, line, stop, dir string) string {
diff := t.Sub(time.Now().UTC())
mins := strconv.Itoa(int(diff.Minutes()))
secs := strconv.Itoa(int(diff.Seconds()) % 60)
out := fmt.Sprintf("The %s %s train will leave %s towards %s in ",
name, line, stop, dir)
if mins != "0" {
out += mins + " minutes and "
}
out += secs + " seconds. "
return out
}