-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreference.go
158 lines (128 loc) · 3.05 KB
/
reference.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
package main
import (
"compress/gzip"
"encoding/xml"
"io/ioutil"
"sync"
"fmt"
"github.com/fatih/color"
"github.com/jlaffaye/ftp"
"github.com/piotrjura/darwingo/config"
)
type CallingPoint struct {
Tpl string `xml:"tpl,attr"`
Act string `xml:"act,attr"`
}
type ArrivingPoint struct {
PlannedArrival string `xml:"pta,attr"`
ActualArrival string `xml:"wta,attr"`
}
type DepartingPoint struct {
PlannedDeparture string `xml:"ptd,attr"`
ActualDeparture string `xml:"wtd,attr"`
}
type IntermediatePoint struct {
*CallingPoint
*ArrivingPoint
*DepartingPoint
}
type OriginPoint struct {
*CallingPoint
*DepartingPoint
}
type DestinationPoint struct {
*CallingPoint
*ArrivingPoint
}
type Journey struct {
Origin OriginPoint `xml:"OR"`
Destination DestinationPoint `xml:"DT"`
IntermediatePoints []IntermediatePoint `xml:"IP"`
}
type Timetable struct {
Journeys []Journey `xml:"Journey"`
}
type Location struct {
Tpl string `xml:"tpl,attr"`
Name string `xml:"locname,attr"`
CRS string `xml:"crs,attr"`
TOC string `xml:"toc,attr"`
}
type Company struct {
TOC string `xml:"toc,attr"`
Name string `xml:"tocname,attr"`
URL string `xml:"url,attr"`
}
type LateReason struct {
Code int `xml:"code,attr"`
Text string `xml:"reasontext,attr"`
}
type TimetableReference struct {
Locations []Location `xml:"LocationRef"`
Companies []Company `xml:"Company"`
LateReasons []LateReason `xml:"LateRunningReasons>Reason"`
}
func downloadXML(file string, wg *sync.WaitGroup, c chan []byte, conf config.FtpConfig) {
color.Blue("Downloading XML: %s...\n", file)
defer wg.Done()
conn := connect(conf)
ftpFile, err := conn.Retr(file)
if err != nil {
panic(err)
}
defer ftpFile.Close()
r, err := gzip.NewReader(ftpFile)
defer r.Close()
xmlBytes, _ := ioutil.ReadAll(r)
color.Green("Downloading %s completed\n", file)
c <- xmlBytes
close(c)
}
func getReferenceFilenames(conf config.FtpConfig) (string, string) {
color.Blue("Fetching timetable & reference file names...")
conn := connect(conf)
files, err := conn.List("")
if err != nil {
panic(err)
}
color.Green("Timetable & reference file names fetched")
return files[0].Name, files[1].Name
}
func connect(config config.FtpConfig) *ftp.ServerConn {
conn, err := ftp.Connect(config.URL)
if err != nil {
panic(err)
}
err = conn.Login(config.User, config.Password)
if err != nil {
panic(err)
}
return conn
}
func parseTimetables(x chan []byte, wg *sync.WaitGroup) Timetable {
defer wg.Done()
d := <-x
color.Blue("Parsing timetable data...")
var timetable Timetable
err := xml.Unmarshal(d, &timetable)
if err != nil {
panic(err)
}
color.Green("Timetable data parsed")
return timetable
}
func parseReference(x chan []byte, wg *sync.WaitGroup) TimetableReference {
defer wg.Done()
d := <-x
color.Blue("Parsing reference data...")
var ref TimetableReference
err := xml.Unmarshal(d, &ref)
if err != nil {
panic(err)
}
for _, reason := range ref.LateReasons {
fmt.Println(reason.Text)
}
color.Green("Reference data parsed")
return ref
}