-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (118 loc) · 3.55 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
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
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/jasonlvhit/gocron"
"github.com/olekukonko/tablewriter"
"googlemaps.github.io/maps"
"gopkg.in/yaml.v2"
)
type Config struct {
OriginAddresses []string `yaml:"origins"`
DestinationAddresses []string `yaml:"destinations"`
Routes struct {
To string `yaml:"to"`
From string `yaml:"from"`
}
Cron []struct {
DestinationAddress string `yaml:"destination"`
ToTimes []string `yaml:"to_times"`
FromTimes []string `yaml:"from_times"`
}
ApiKey string `yaml:"apikey"`
}
func (config *Config) load() {
yamlFile, err := ioutil.ReadFile("config.yaml")
if err != nil {
log.Fatalf("ERROR: Reading config file: %s", err)
}
err = yaml.Unmarshal(yamlFile, config)
if err != nil {
log.Fatalf("ERROR: Parsing configuration: %s", err)
}
}
func getDistanceMatrix(client *maps.Client, origins []string, destinations []string) *maps.DistanceMatrixResponse {
request := &maps.DistanceMatrixRequest{
Origins: origins,
Destinations: destinations,
DepartureTime: "now",
Mode: maps.TravelModeDriving,
TrafficModel: maps.TrafficModelBestGuess,
}
resp, err := client.DistanceMatrix(context.Background(), request)
if err != nil {
log.Fatalf("ERROR: %s", err)
}
return resp
}
func printTable(distanceMatrixResponse *maps.DistanceMatrixResponse, origins, destinations []string, reverse bool) {
title := "TO"
if reverse {
title = "FROM"
}
table := tablewriter.NewWriter(os.Stdout)
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetAutoWrapText(false)
table.SetHeader(append([]string{fmt.Sprintf("%s %s", title, time.Now().Format("01/02/2006 15:04"))}, destinations...))
for i, origin := range origins {
tableRow := []string{origin}
for j, _ := range destinations {
from := i
to := j
if reverse {
from = j
to = i
}
tableRow = append(tableRow, fmt.Sprintf("%.1f", distanceMatrixResponse.Rows[from].Elements[to].DurationInTraffic.Minutes()))
}
table.Append(tableRow)
}
table.Render()
fmt.Println("")
}
func printToDestinations(client *maps.Client, config *Config) {
printTable(getDistanceMatrix(client, config.OriginAddresses, config.DestinationAddresses), config.OriginAddresses, config.DestinationAddresses, false)
}
func printFromDestinations(client *maps.Client, config *Config) {
printTable(getDistanceMatrix(client, config.DestinationAddresses, config.OriginAddresses), config.OriginAddresses, config.DestinationAddresses, true)
}
func toMilitaryTime(s string) string {
t, err := time.Parse("03:04pm", s)
if err != nil {
log.Fatalf("ERROR: %s", err)
}
return t.Format("15:04")
}
func main() {
// Load the configuration
var config Config
config.load()
// Instantiate the Maps API client
client, err := maps.NewClient(maps.WithAPIKey(config.ApiKey))
if err != nil {
log.Fatalf("ERROR: %s", err)
}
// Parse arguments
nowPtr := flag.Bool("now", false, "Print the commute matrix now (instead of scheduling it)")
flag.Parse()
if *nowPtr {
printToDestinations(client, &config)
printFromDestinations(client, &config)
} else {
fmt.Println("Commute matrix scheduled...")
for _, schedule := range config.Cron {
for _, toTime := range schedule.ToTimes {
gocron.Every(1).Day().At(toMilitaryTime(toTime)).Do(printToDestinations, client, &config)
}
for _, fromTime := range schedule.FromTimes {
gocron.Every(1).Day().At(toMilitaryTime(fromTime)).Do(printFromDestinations, client, &config)
}
}
<-gocron.Start()
}
}