forked from strava/go.strava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segments.go
378 lines (312 loc) · 9.75 KB
/
segments.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package strava
import (
"encoding/json"
"fmt"
"time"
)
type SegmentDetailed struct {
SegmentSummary
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
TotalElevationGain float64 `json:"total_elevation_gain"`
Map struct {
Id string `json:"id"`
Polyline Polyline `json:"polyline"`
} `json:"map"`
EffortCount int `json:"effort_count"`
AthleteCount int `json:"athlete_count"`
StarCount int `json:"star_count"`
Hazardous bool `json:"hazardous"`
}
type SegmentSummary struct {
Id int64 `json:"id"`
Name string `json:"name"`
ActivityType ActivityType `json:"activity_type"`
Distance float64 `json:"distance"`
AverageGrade float64 `json:"average_grade"`
MaximumGrade float64 `json:"maximum_grade"`
ElevationHigh float64 `json:"elevation_high"`
ElevationLow float64 `json:"elevation_low"`
ClimbCategory ClimbCategory `json:"climb_category"`
StartLocation Location `json:"start_latlng"`
EndLocation Location `json:"end_latlng"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"`
Private bool `json:"private"`
Starred bool `json:"starred"`
}
type PersonalSegmentSummary struct {
SegmentSummary
AthletePR struct {
Id int64 `json:"id"`
ElapsedTime int `json:"elapsed_time"`
Distance float64 `json:"distance"`
StartDate time.Time `json:"start_date"`
StartDateLocal time.Time `json:"start_date_local"`
IsKOM bool `json:"is_kom"`
} `json:"athlete_pr_effort"`
StarredDate time.Time `json:"starred_date"`
}
type SegmentLeaderboard struct {
EntryCount int `json:"entry_count"`
Entries []*SegmentLeaderboardEntry `json:"entries"`
}
type SegmentLeaderboardEntry struct {
AthleteName string `json:"athlete_name"`
AthleteId int64 `json:"athlete_id"`
AthleteGender Gender `json:"athlete_gender"`
AverageHeartrate float64 `json:"average_hr"`
AveragePower float64 `json:"average_watts"`
Distance float64 `json:"distance"`
ElapsedTime int `json:"elapsed_time"`
MovingTime int `json:"moving_time"`
StartDate time.Time `json:"start_date"`
StartDateLocal time.Time `json:"start_date_local"`
ActivityId int64 `json:"activity_id"`
EffortId int64 `json:"effort_id"`
Rank int `json:"rank"`
AthleteProfile string `json:"athlete_profile"`
}
type segmentExplorer struct {
Segments []*SegmentExplorerSegment `json:"segments"`
}
type SegmentExplorerSegment struct {
Id int64 `json:"id"`
Name string `json:"name"`
ClimbCategory ClimbCategory `json:"climb_category"`
AverageGrade float64 `json:"avg_grade"`
StartLocation Location `json:"start_latlng"`
EndLocation Location `json:"end_latlng"`
ElevationDifference float64 `json:"elev_difference"`
Distance float64 `json:"distance"`
Polyline Polyline `json:"points"`
}
type ClimbCategory int
var ClimbCategories = struct {
NotCategorized ClimbCategory
Category4 ClimbCategory
Category3 ClimbCategory
Category2 ClimbCategory
Category1 ClimbCategory
HorsCategorie ClimbCategory
}{0, 1, 2, 3, 4, 5}
type AgeGroup string
var AgeGroups = struct {
From0to24 AgeGroup
From25to34 AgeGroup
From35to44 AgeGroup
From45to54 AgeGroup
From55to64 AgeGroup
From65plus AgeGroup
}{"0_24", "25_34", "35_44", "45_54", "55_64", "65_plus"}
type WeightClass string
var WeightClasses = struct {
From0To125Pounds WeightClass
From125To149Pounds WeightClass
From150To164Pounds WeightClass
From165To179Pounds WeightClass
From180To199Pounds WeightClass
From200PlusPounds WeightClass
From0To54Kilograms WeightClass
From55To64Kilograms WeightClass
From65To74Kilograms WeightClass
From75To84Kilograms WeightClass
From85To94Kilograms WeightClass
From95PlusKilograms WeightClass
}{"0_124", "125_149", "150_164", "165_179", "180_199", "200_plus", "0_54", "55_64", "65_74", "75_84", "85_94", "95_plus"}
type DateRange string
var DateRanges = struct {
ThisYear DateRange
ThisMonth DateRange
ThisWeek DateRange
Today DateRange
}{"this_year", "this_month", "this_week", "today"}
type SegmentsService struct {
client *Client
}
func NewSegmentsService(client *Client) *SegmentsService {
return &SegmentsService{client}
}
/*********************************************************/
type SegmentsGetCall struct {
service *SegmentsService
id int64
}
func (s *SegmentsService) Get(segmentId int64) *SegmentsGetCall {
return &SegmentsGetCall{
service: s,
id: segmentId,
}
}
func (s *SegmentsGetCall) Do() (*SegmentDetailed, error) {
data, err := s.service.client.run("GET", fmt.Sprintf("/segments/%d", s.id), nil)
if err != nil {
return nil, err
}
var segment SegmentDetailed
err = json.Unmarshal(data, &segment)
if err != nil {
return nil, err
}
return &segment, nil
}
/*********************************************************/
type SegmentsListEffortsCall struct {
service *SegmentsService
id int64
ops map[string]interface{}
}
func (s *SegmentsService) ListEfforts(segmentId int64) *SegmentsListEffortsCall {
return &SegmentsListEffortsCall{
service: s,
id: segmentId,
ops: make(map[string]interface{}),
}
}
func (c *SegmentsListEffortsCall) AthleteId(athleteId int64) *SegmentsListEffortsCall {
c.ops["athlete_id"] = athleteId
return c
}
func (c *SegmentsListEffortsCall) DateRange(startDateLocal, endDateLocal time.Time) *SegmentsListEffortsCall {
c.ops["start_date_local"] = startDateLocal.UTC().Format(timeFormat)
c.ops["end_date_local"] = endDateLocal.UTC().Format(timeFormat)
return c
}
func (c *SegmentsListEffortsCall) Page(page int) *SegmentsListEffortsCall {
c.ops["page"] = page
return c
}
func (c *SegmentsListEffortsCall) PerPage(perPage int) *SegmentsListEffortsCall {
c.ops["per_page"] = perPage
return c
}
func (c *SegmentsListEffortsCall) Do() ([]*SegmentEffortSummary, error) {
data, err := c.service.client.run("GET", fmt.Sprintf("/segments/%d/all_efforts", c.id), c.ops)
if err != nil {
return nil, err
}
efforts := make([]*SegmentEffortSummary, 0)
err = json.Unmarshal(data, &efforts)
if err != nil {
return nil, err
}
return efforts, nil
}
/*********************************************************/
type SegmentsGetLeaderboardCall struct {
service *SegmentsService
id int64
ops map[string]interface{}
}
func (s *SegmentsService) GetLeaderboard(segmentId int64) *SegmentsGetLeaderboardCall {
return &SegmentsGetLeaderboardCall{
service: s,
id: segmentId,
ops: make(map[string]interface{}),
}
}
func (c *SegmentsGetLeaderboardCall) Gender(gender Gender) *SegmentsGetLeaderboardCall {
c.ops["gender"] = gender
return c
}
func (c *SegmentsGetLeaderboardCall) AgeGroup(ageGroup AgeGroup) *SegmentsGetLeaderboardCall {
c.ops["age_group"] = ageGroup
return c
}
func (c *SegmentsGetLeaderboardCall) WeightClass(class WeightClass) *SegmentsGetLeaderboardCall {
c.ops["weight_class"] = class
return c
}
func (c *SegmentsGetLeaderboardCall) Following() *SegmentsGetLeaderboardCall {
c.ops["following"] = true
return c
}
func (c *SegmentsGetLeaderboardCall) ClubId(clubId int64) *SegmentsGetLeaderboardCall {
c.ops["club_id"] = clubId
return c
}
func (c *SegmentsGetLeaderboardCall) DateRange(dateRange DateRange) *SegmentsGetLeaderboardCall {
c.ops["date_range"] = dateRange
return c
}
func (c *SegmentsGetLeaderboardCall) ContextEntries(count int) *SegmentsGetLeaderboardCall {
c.ops["context_entries"] = count
return c
}
func (c *SegmentsGetLeaderboardCall) Page(page int) *SegmentsGetLeaderboardCall {
c.ops["page"] = page
return c
}
func (c *SegmentsGetLeaderboardCall) PerPage(perPage int) *SegmentsGetLeaderboardCall {
c.ops["per_page"] = perPage
return c
}
func (c *SegmentsGetLeaderboardCall) Do() (*SegmentLeaderboard, error) {
data, err := c.service.client.run("GET", fmt.Sprintf("/segments/%d/leaderboard", c.id), c.ops)
if err != nil {
return nil, err
}
var leaderboard SegmentLeaderboard
err = json.Unmarshal(data, &leaderboard)
if err != nil {
return nil, err
}
return &leaderboard, nil
}
/*********************************************************/
type SegmentsExplorerCall struct {
service *SegmentsService
ops map[string]interface{}
}
func (s *SegmentsService) Explore(south, west, north, east float64) *SegmentsExplorerCall {
call := &SegmentsExplorerCall{
service: s,
ops: make(map[string]interface{}),
}
call.ops["bounds"] = fmt.Sprintf("%f,%f,%f,%f", south, west, north, east)
return call
}
func (c *SegmentsExplorerCall) ActivityType(activityType string) *SegmentsExplorerCall {
c.ops["activity_type"] = activityType
return c
}
func (c *SegmentsExplorerCall) MinimumCategory(cat int) *SegmentsExplorerCall {
c.ops["min_cat"] = cat
return c
}
func (c *SegmentsExplorerCall) MaximumCategory(cat int) *SegmentsExplorerCall {
c.ops["max_cat"] = cat
return c
}
func (c *SegmentsExplorerCall) Do() ([]*SegmentExplorerSegment, error) {
data, err := c.service.client.run("GET", "/segments/explore", c.ops)
if err != nil {
return nil, err
}
var explorer segmentExplorer
err = json.Unmarshal(data, &explorer)
if err != nil {
return nil, err
}
return explorer.Segments, nil
}
/*********************************************************/
func (c ClimbCategory) Id() int {
return int(c)
}
func (c ClimbCategory) String() string {
switch c.Id() {
case 1:
return "Category 4"
case 2:
return "Category 3"
case 3:
return "Category 2"
case 4:
return "Category 1"
case 5:
return "Hors Categorie"
}
return "Not Categorized"
}