-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.go
115 lines (91 loc) · 2.29 KB
/
board.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"reflect"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
// BoardURL link to html version of live board
const BoardURL = `https://www.scotrail.co.uk/nre/live-boards/%s/lazy`
// Entry on live board
type Entry struct {
ID string `json:"id"`
Platform string `json:"platform"`
Destination string `json:"destination"`
Departs string `json:"departs"`
Arrives string `json:"arrives"`
Expected string `json:"expected"`
Origin string `json:"origin"`
Operator string `json:"operator"`
}
// Board type
type Board struct {
Station Station `json:"station"`
Services []Entry `json:"services"`
timestamp time.Time
}
// Boards map of recently fetched boards
// used for caching requests
var Boards = make(map[string]Board)
func (b *Board) hasStation(id string) (bool, int) {
for i, v := range b.Services {
if strings.Compare(v.ID, id) == 0 {
return true, i
}
}
return false, 0
}
func getBoard(station string) Board {
b, ok := Boards[station]
if ok && time.Now().Before(b.timestamp.Add(time.Second*30)) {
return b
}
res, err := http.Get(fmt.Sprintf(BoardURL, station))
if err != nil {
log.Fatalln(err.Error())
}
if res.StatusCode != 200 {
log.Fatalln(res.Status)
}
defer res.Body.Close()
b = parseBoard(res.Body)
b.Station, _ = Stations[station]
Boards[station] = b
return b
}
func parseBoard(r io.Reader) (b Board) {
doc, _ := goquery.NewDocumentFromReader(r)
b.Services = make([]Entry, 0)
doc.Find("tr.service").Each(func(i int, s *goquery.Selection) {
var e = new(Entry)
id, ok := s.Attr("data-id")
if ok {
if ok, idx := b.hasStation(id); ok {
e = &(b.Services)[idx]
}
}
s.ChildrenFiltered("td").Each(func(i int, s *goquery.Selection) {
switch s.AttrOr("data-label", "") {
case "Expected", "Origin", "Destination", "Operator":
label := s.AttrOr("data-label", "")
reflect.ValueOf(e).Elem().FieldByName(label).SetString(s.Text())
case "Platform":
e.Platform = s.Text()
case "Arrives", "Departs":
t := s.Text()
label := s.AttrOr("data-label", "")
reflect.ValueOf(e).Elem().FieldByName(label).Set(reflect.ValueOf(t))
}
})
if len(e.ID) < 1 {
e.ID = id
b.Services = append(b.Services, *e)
}
})
b.timestamp = time.Now()
return
}