This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
157 lines (122 loc) · 2.99 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
package main
import (
"archive/zip"
"context"
"flag"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/golang/glog"
"github.com/metalnem/runtastic/api"
"github.com/metalnem/runtastic/gpx"
"github.com/metalnem/runtastic/tcx"
"github.com/pkg/errors"
)
const usage = `Usage of Runtastic Archiver:
-email string
Email (required)
-password string
Password (required)
-format string
Output format (gpx or tcx)`
var (
email = flag.String("email", "", "")
password = flag.String("password", "", "")
format = flag.String("format", "gpx", "")
tolerance = flag.Int("tolerance", 15, "")
errMissingCredentials = errors.New("Missing email address or password")
errNoActivities = errors.New("There are no activities to backup")
errInvalidFormat = errors.New("Invalid output format")
)
type exporter interface {
Export(api.Activity) error
}
func getCredentials() (string, string, error) {
email := *email
password := *password
if email != "" && password != "" {
return email, password, nil
}
email = os.Getenv("RUNTASTIC_EMAIL")
password = os.Getenv("RUNTASTIC_PASSWORD")
if email != "" && password != "" {
return email, password, nil
}
return "", "", errMissingCredentials
}
func formatTime(t time.Time) string {
return t.Local().Format("2006-01-02 15.04.05")
}
func checkedClose(c io.Closer, err *error) {
if cerr := c.Close(); cerr != nil && *err == nil {
*err = cerr
}
}
func export(activities []api.Activity, exp func(io.Writer) exporter, ext string) (err error) {
filename := fmt.Sprintf("Runtastic %s.%s", formatTime(time.Now()), "zip")
file, err := os.Create(filename)
if err != nil {
return errors.Wrapf(err, "Failed to create file %s", filename)
}
defer checkedClose(file, &err)
zw := zip.NewWriter(file)
defer checkedClose(zw, &err)
for _, activity := range activities {
filename := fmt.Sprintf("Runtastic %s %s.%s", formatTime(activity.EndTime), activity.Type, ext)
header := zip.FileHeader{
Name: filename,
Method: zip.Deflate,
}
header.SetModTime(time.Now())
w, err := zw.CreateHeader(&header)
if err != nil {
return err
}
exp := exp(w)
if err = exp.Export(activity); err != nil {
return err
}
}
return nil
}
func main() {
flag.Parse()
ext := strings.ToLower(*format)
var exp func(io.Writer) exporter
switch ext {
case "gpx":
exp = func(w io.Writer) exporter {
return gpx.NewExporter(w)
}
case "tcx":
exp = func(w io.Writer) exporter {
return tcx.NewExporter(w)
}
default:
glog.Exit(errInvalidFormat)
}
email, password, err := getCredentials()
if err != nil {
fmt.Println(usage)
os.Exit(1)
}
ctx := context.Background()
session, err := api.Login(ctx, email, password)
if err != nil {
glog.Exit(err)
}
session.Options.Tolerance = *tolerance
activities, err := session.GetActivities(ctx)
if err != nil {
glog.Exit(err)
}
if len(activities) == 0 {
glog.Exit(errNoActivities)
}
if err = export(activities, exp, ext); err != nil {
glog.Exit(err)
}
glog.Flush()
}