-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
64 lines (47 loc) · 1.21 KB
/
service.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
package main
import (
"io"
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
// ServiceURL link to service details
const ServiceURL = `https://www.scotrail.co.uk/nre/service-details/`
// Stop - train stop
type Stop struct {
Station string
Status string
Connection *Service
Time string
}
// Service type
type Service []Stop
func serviceDetails(serviceID string) Service {
res, err := http.Get(ServiceURL + serviceID)
if err != nil {
log.Fatalln(err.Error())
}
defer res.Body.Close()
return parseService(res.Body)
}
func parseService(r io.Reader) (service Service) {
doc, _ := goquery.NewDocumentFromReader(r)
doc.Find("ul").ChildrenFiltered("li").Each(func(i int, s *goquery.Selection) {
service = append(service, createStop(s))
})
return
}
func createStop(s *goquery.Selection) Stop {
text := strings.Split(s.Text(), "-")
status := strings.Split(text[1], "at")
t := status[1]
stop := Stop{
Station: strings.Trim(text[0], " \n"), Status: strings.Trim(status[0], " \n"),
Time: t, Connection: new(Service),
}
s.ChildrenFiltered("ul.connection").Each(func(i int, s *goquery.Selection) {
*stop.Connection = append(*stop.Connection, createStop(s))
})
return stop
}