This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
182 lines (133 loc) · 3.95 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/alexflint/go-arg"
"github.com/pieterclaerhout/export-komoot/komoot"
"github.com/pieterclaerhout/go-log"
"github.com/pieterclaerhout/go-waitgroup"
)
type args struct {
Email string `help:"Your Komoot email address"`
Password string `help:"Your Komoot password"`
Filter string `help:"Filter tours with name matching this pattern"`
Format string `help:"The format to export as: gpx or fit" default:"gpx"`
To string `help:"The path to export to"`
FullDownload bool `help:"If specified, all data is redownloaded" default:"false"`
Concurrency int `help:"The number of simultaneous downloads" default:"16"`
TourType string `help:"The type of tours to download" default:"tour_planned"`
}
func main() {
var args args
arg.MustParse(&args)
log.PrintTimestamp = true
log.PrintColors = true
start := time.Now()
defer func() { log.Info("Elapsed:", time.Since(start)) }()
format := "gpx"
if args.Format == "fit" {
format = "fit"
}
client := komoot.NewClient(args.Email, args.Password)
userID, err := client.Login()
log.CheckError(err)
fullDstPath, _ := filepath.Abs(args.To)
log.Info("Exporting:", args.Email)
log.Info(" to:", fullDstPath)
log.Info(" format:", format)
err = os.MkdirAll(args.To, 0777)
log.CheckError(err)
log.Info("Komoot User ID:", userID)
tours, resp, err := client.Tours(userID, args.Filter, args.TourType)
log.CheckError(err)
if len(tours) == 0 {
log.Info("No tours need to be downloaded")
return
}
log.Info("Found", len(tours), "planned tours")
allTours := []komoot.Tour{}
if !args.FullDownload {
log.Info("Incremental download, checking what has changed")
changedTours := []komoot.Tour{}
for _, tour := range tours {
allTours = append(allTours, tour)
dstPath := filepath.Join(args.To, tour.Filename(format))
if !fileExists(dstPath) {
changedTours = append(changedTours, tour)
}
}
tours = changedTours
if len(tours) == 0 {
log.Info("No tours need to be downloaded")
} else {
log.Info("Found", len(tours), "which need to be downloaded")
}
} else {
allTours = tours
}
if len(tours) > 0 {
log.Info("Downloading with a concurrency of", args.Concurrency)
wg := waitgroup.NewWaitGroup(args.Concurrency)
var downloadCount int
for _, tour := range tours {
tourToDownload := tour
label := fmt.Sprintf("%10d | %-7s | %-15s | %s", tour.ID, tour.Status, tour.FormattedSport(), tour.Name)
wg.Add(func() {
if err := func() error {
r, err := client.Coordinates(tourToDownload)
if err != nil {
return err
}
var out []byte
if format == "fit" {
out, err = r.Fit()
if err != nil {
return err
}
} else {
out = r.GPX()
}
dstPath := filepath.Join(args.To, tourToDownload.Filename(format))
if err = saveTourFile(out, dstPath, tourToDownload); err != nil {
return err
}
log.Info("Downloaded:", label)
return nil
}(); err != nil {
log.Error("Downloaded:", label, "|", err)
}
downloadCount++
})
}
wg.Wait()
log.Info("Downloaded", downloadCount, "tours")
}
allTourNames := map[string]bool{}
for _, tour := range allTours {
allTourNames[tour.Filename(format)] = true
}
items, err := filepath.Glob(filepath.Join(args.To, "*."+format))
log.CheckError(err)
for _, item := range items {
if _, exists := allTourNames[filepath.Base(item)]; exists {
continue
}
log.Info("Deleting:", filepath.Base(item))
os.Remove(item)
}
log.Info("Saving tour list")
dstPath := filepath.Join(args.To, "tours.json")
err = saveFormattedJSON(resp, dstPath)
log.CheckError(err)
var out bytes.Buffer
err = json.NewEncoder(&out).Encode(allTours)
log.CheckError(err)
log.Info("Saving parsed tour list")
dstPath = filepath.Join(args.To, "tours_parsed.json")
err = saveFormattedJSON(out.Bytes(), dstPath)
log.CheckError(err)
}