-
Notifications
You must be signed in to change notification settings - Fork 1
/
uploader.go
59 lines (50 loc) · 1.32 KB
/
uploader.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
package main
import (
"fmt"
"time"
"io"
"log"
strava "github.com/strava/go.strava"
)
// uploader communicates with the Strava API
type uploader struct {
client *strava.Client
service *strava.UploadsService
}
// newUploader initialises an Uploader instance
func newUploader(token string) *uploader {
u := uploader{}
u.client = strava.NewClient(token)
u.service = strava.NewUploadsService(u.client)
return &u
}
// Upload creates a Strava Activity from a file
func (u *uploader) Upload(fname string, f io.Reader) (*int64, error) {
ft := strava.FileDataTypes.FIT
resp, err := u.service.Create(ft, fname, f).Private().Do()
if err != nil {
if e, ok := err.(strava.Error); ok && e.Message == "Authorization Error" {
log.Printf("%s - Auth error. Make sure your token has 'write' permissions.", fname)
} else {
log.Printf("%s - %s", fname, err)
}
return nil, err
}
tries := 0
for {
sum, err := u.service.Get(resp.Id).Do()
if err != nil {
return nil, fmt.Errorf("get uploaded activity summary: %w", err)
}
if sum.Error != "" {
return nil, fmt.Errorf("%s", sum.Error)
}
if sum.Status == "Your activity is still being processed." && tries < 10 {
log.Printf("%s - waiting for activity to be processed", fname)
tries++
time.Sleep(5* time.Second)
} else {
return &sum.ActivityId, nil
}
}
}