-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
64 lines (55 loc) · 1.28 KB
/
main.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 brigades
import (
"errors"
"net/http"
"os"
"github.com/MKuranowski/WarsawGTFS/realtime/gtfs"
)
// Options represents options for creating brigades.json
type Options struct {
JSONTarget string
Apikey string
ThrowAPIErrors bool
}
// Main auto-magically creates brigades data
func Main(client *http.Client, gtfs *gtfs.Gtfs, opts Options) error {
// Create an API object
api := &ttableAPI{
Key: opts.Apikey,
Client: client,
Respones: make(map[routeStopPair]mapTimeBrigade),
ForwardErrors: opts.ThrowAPIErrors,
}
// Try to open stop_times.txt
file := gtfs.GetZipFileByName("stop_times.txt")
if file == nil {
return errors.New("gtfs file is missing stop_times.txt`1")
}
reader, err := file.Open()
if err != nil {
return err
}
defer reader.Close()
// Match data
logPrint("Matching data", false)
data, err := Match(api, gtfs, reader)
if err != nil {
return err
}
// Marshall it to JSON
logPrint("Marshalling data to JSON", false)
dataJSON, err := data.MarshalJSON()
if err != nil {
return err
}
// Create a target file
logPrint("Writing to JSON", false)
f, err := os.Create(opts.JSONTarget)
if err != nil {
return err
}
defer f.Close()
// Write JSON to the target file
_, err = f.Write(dataJSON)
return err
}