summaryrefslogtreecommitdiff
path: root/strava/sync.go
blob: 762b41aca6e08182b0491fb00a95910b4b4caa95 (plain) (blame)
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
183
184
185
186
package strava

import (
	"fmt"
	"net/url"
	"strconv"
	"time"

	"github.com/martinlehoux/kagamigo/kcore"
	"github.com/tkrajina/gpxgo/gpx"
)

const garminTrackPointExtensionNamespace = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1"

type Activity struct {
	ID                  int64     `json:"id"`
	Name                string    `json:"name"`
	Type                string    `json:"type"`
	SportType           string    `json:"sport_type"`
	StartDate           time.Time `json:"start_date"`
	DistanceM           float64   `json:"distance"`
	MovingTimeS         int64     `json:"moving_time"`
	ElapsedTimeS        int64     `json:"elapsed_time"`
	TotalElevationGainM float64   `json:"total_elevation_gain"`
	AverageSpeedMps     float64   `json:"average_speed"`
}

func (c *Client) List(from, to time.Time) ([]Activity, error) {
	return c.list(from, to, "Ride")
}

func (c *Client) list(from, to time.Time, types ...string) ([]Activity, error) {
	allowed := map[string]bool{}
	for _, t := range types {
		allowed[t] = true
	}
	var all []Activity
	for page := 1; ; page++ {
		q := url.Values{}
		q.Set("per_page", "200")
		q.Set("page", strconv.Itoa(page))
		if !from.IsZero() {
			q.Set("after", strconv.FormatInt(from.Unix(), 10))
		}
		if !to.IsZero() {
			q.Set("before", strconv.FormatInt(to.Unix(), 10))
		}
		var batch []Activity
		err := c.GetJSON("/athlete/activities?"+q.Encode(), &batch)
		if err != nil {
			return nil, kcore.Wrap(err, "failed to list Strava activities")
		}
		if len(batch) == 0 {
			break
		}
		for _, activity := range batch {
			if len(allowed) == 0 || allowed[activity.Type] || allowed[activity.SportType] {
				all = append(all, activity)
			}
		}
		if len(batch) < 200 {
			break
		}
	}
	return all, nil
}

func (c *Client) Get(id int64) (Activity, []byte, error) {
	var activity Activity
	if err := c.GetJSON(fmt.Sprintf("/activities/%d", id), &activity); err != nil {
		return Activity{}, nil, kcore.Wrap(err, "failed to get Strava activity")
	}
	streams, err := c.activityStreams(id)
	if err != nil {
		return Activity{}, nil, err
	}
	gpxData, err := activityGPX(activity, streams)
	if err != nil {
		return Activity{}, nil, err
	}
	return activity, gpxData, nil
}

type activityStreams struct {
	LatLng    [][2]float64
	Altitude  []float64
	Seconds   []float64
	HeartRate []*float64
	Cadence   []*float64
	Power     []*float64
}

type nullableFloatStream struct {
	Data []*float64 `json:"data"`
}

func (c *Client) activityStreams(id int64) (activityStreams, error) {
	path := fmt.Sprintf("/activities/%d/streams?keys=latlng,altitude,time,heartrate,cadence,watts&key_by_type=true", id)
	var streams struct {
		LatLng *struct {
			Data [][2]float64 `json:"data"`
		} `json:"latlng"`
		Altitude *struct {
			Data []float64 `json:"data"`
		} `json:"altitude"`
		Time *struct {
			Data []float64 `json:"data"`
		} `json:"time"`
		HeartRate *nullableFloatStream `json:"heartrate"`
		Cadence   *nullableFloatStream `json:"cadence"`
		Power     *nullableFloatStream `json:"watts"`
	}
	err := c.GetJSON(path, &streams)
	if err != nil {
		return activityStreams{}, kcore.Wrap(err, "failed to fetch Strava streams")
	}
	result := activityStreams{}
	if streams.LatLng != nil {
		result.LatLng = streams.LatLng.Data
	}
	if streams.Altitude != nil {
		result.Altitude = streams.Altitude.Data
	}
	if streams.Time != nil {
		result.Seconds = streams.Time.Data
	}
	if streams.HeartRate != nil {
		result.HeartRate = streams.HeartRate.Data
	}
	if streams.Cadence != nil {
		result.Cadence = streams.Cadence.Data
	}
	if streams.Power != nil {
		result.Power = streams.Power.Data
	}
	return result, nil
}

func activityGPX(activity Activity, streams activityStreams) ([]byte, error) {
	points := make([]gpx.GPXPoint, len(streams.LatLng))
	for i := range streams.LatLng {
		point := gpx.GPXPoint{}
		point.Latitude = streams.LatLng[i][0]
		point.Longitude = streams.LatLng[i][1]
		if i < len(streams.Altitude) {
			point.Elevation = *gpx.NewNullableFloat64(streams.Altitude[i])
		}
		if i < len(streams.Seconds) {
			point.Timestamp = activity.StartDate.Add(time.Duration(streams.Seconds[i]) * time.Second)
		}
		addTrackPointMetric(&point, "hr", streamValue(streams.HeartRate, i))
		addTrackPointMetric(&point, "cad", streamValue(streams.Cadence, i))
		addTrackPointMetric(&point, "watts", streamValue(streams.Power, i))
		points[i] = point
	}
	doc := gpx.GPX{
		Version: "1.1",
		Creator: "biking_home",
		Name:    activity.Name,
		Tracks: []gpx.GPXTrack{{
			Name:     activity.Name,
			Segments: []gpx.GPXTrackSegment{{Points: points}},
		}},
	}
	doc.RegisterNamespace("gpxtpx", garminTrackPointExtensionNamespace)
	xmlData, err := doc.ToXml(gpx.ToXmlParams{})
	if err != nil {
		return nil, kcore.Wrap(err, "failed to build GPX XML")
	}
	return xmlData, nil
}

func streamValue(stream []*float64, i int) *float64 {
	if i >= len(stream) {
		return nil
	}
	return stream[i]
}

func addTrackPointMetric(point *gpx.GPXPoint, name string, value *float64) {
	if value == nil {
		return
	}
	extension := point.Extensions.GetOrCreateNode(gpx.NamespaceURL(garminTrackPointExtensionNamespace), "TrackPointExtension")
	extension.GetOrCreateNode(name).Data = strconv.FormatFloat(*value, 'f', -1, 64)
}