-
Notifications
You must be signed in to change notification settings - Fork 0
/
querystrava.sh
executable file
·499 lines (401 loc) · 17.4 KB
/
querystrava.sh
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/bin/bash
### INITIALIZERS
rm ~/.querystrava/querystrava.log &> /dev/null
rm -r ~/.querystrava/webfiles &> /dev/null
mkdir ~/.querystrava &> /dev/null
touch ~/.querystrava/apiclient.sh
touch ~/.querystrava/setauth.sh
cp -r "$(realpath "$(dirname "$BASH_SOURCE")")/webfiles" ~/.querystrava/
. ~/.querystrava/apiclient.sh
. ~/.querystrava/setauth.sh
OIFS="$IFS"
IFS=' '
### LOGGING
QS_LOG_LEVEL_TRACE=0
QS_LOG_LEVEL_DEBUG=1
QS_LOG_LEVEL_INFO=2
QS_LOG_LEVEL_WARN=3
QS_LOG_LEVEL_ERROR=4
QS_LOG_LEVELS=(TRACE DEBUG INFO WARN ERROR)
QS_LOG_LEVEL_DEFAULT=$QS_LOG_LEVEL_INFO
### FIELDS
QS_AUTH_CALLBACK_PORT=5744
QS_CLIENT_ID=$QS_CLIENT_ID
QS_CLIENT_SECRET=$QS_CLIENT_SECRET
QS_AUTH_TOKEN=$QS_AUTH_TOKEN
QS_AUTH_REFRESH_TOKEN=$QS_AUTH_REFRESH_TOKEN
QS_AUTH_TOKEN_EXPIRY=$QS_AUTH_TOKEN_EXPIRY
### HELPERS
qs_for_each() {
local QS_FOREACH_COMMAND=$1
while read -r line; do
eval $QS_FOREACH_COMMAND $line &
done
}
qs_curl() {
local QS_QUERY_METHOD=$1
local QS_QUERY_URL=$2
. ~/.querystrava/setauth.sh
qs_log "${QS_QUERY_METHOD} ${QS_QUERY_URL}" $QS_LOG_LEVEL_DEBUG
curl -s -X "${QS_QUERY_METHOD}" "${QS_QUERY_URL}" -H "Authorization: Bearer ${QS_AUTH_TOKEN}" | jq . | tee -a ~/.querystrava/querystrava.log
}
qs_log() {
local QS_LOG_STATEMENT_LEVEL=${2:-$QS_LOG_LEVEL_DEFAULT}
local QS_LOG_STATEMENT="${QS_LOG_LEVELS[$QS_LOG_STATEMENT_LEVEL]}: $1"
echo -e "$QS_LOG_STATEMENT" >> ~/.querystrava/querystrava.log
if [[ $QS_LOG_STATEMENT_LEVEL -ge $QS_LOG_LEVEL_DEFAULT ]]; then
echo -e "$QS_LOG_STATEMENT" 1>&2
fi
}
qs_pluralize() {
local QS_PLURALIZATION_COUNT=$1
local QS_PLURALIZATION_SINGULAR=$2
local QS_PLURALIZATION_PLURAL=${3:-"${QS_PLURALIZATION_SINGULAR}s"}
if [[ $QS_PLURALIZATION_COUNT == 1 ]]; then
echo "$QS_PLURALIZATION_SINGULAR"
else
echo "$QS_PLURALIZATION_PLURAL"
fi
}
### AUTH
qs_auth() {
QS_CLIENT_ID=${1:-$QS_CLIENT_ID}
QS_CLIENT_SECRET=${2:-$QS_CLIENT_SECRET}
if [[ -z $QS_CLIENT_ID || -z $QS_CLIENT_SECRET ]]; then
echo "Client ID and secret required"
echo
local QS_API_SETTINGS_URL="https://www.strava.com/settings/api"
echo "Opening browser to: ${QS_API_SETTINGS_URL}"
open $QS_API_SETTINGS_URL
echo
read -p 'Client ID: ' QS_CLIENT_ID
read -p 'Client secret: ' QS_CLIENT_SECRET
echo
echo "
QS_CLIENT_ID=$QS_CLIENT_ID
QS_CLIENT_SECRET=$QS_CLIENT_SECRET
" > ~/.querystrava/apiclient.sh
fi
local QS_AUTH_URL="https://www.strava.com/oauth/authorize?client_id=${QS_CLIENT_ID}&redirect_uri=http://localhost:${QS_AUTH_CALLBACK_PORT}/&response_type=code&scope=read,read_all,activity:read,activity:read_all,profile:read_all"
echo "Opening browser to: ${QS_AUTH_URL}"
open $QS_AUTH_URL
echo "Listening for callback on port ${QS_AUTH_CALLBACK_PORT}..."
local QS_AUTH_CALLBACK_REQUEST=$(echo -e 'HTTP/1.1 200 OK\n\n<h1>Success!</h1>\n<h2>Return to console.</h2>\n' | \
nc -l $QS_AUTH_CALLBACK_PORT | head -n1)
local QS_AUTH_CODE=$(sed -E 's|^.+code=([^&]+)&?[^&]*$|\1|' <<< $QS_AUTH_CALLBACK_REQUEST)
echo "Received callback: ${QS_AUTH_CALLBACK_REQUEST}"
echo
qs_set_auth <<< "$(qs_curl POST "https://www.strava.com/oauth/token?client_id=${QS_CLIENT_ID}&client_secret=${QS_CLIENT_SECRET}&code=${QS_AUTH_CODE}&grant_type=authorization_code")"
}
qs_touch_auth() {
. ~/.querystrava/setauth.sh
if [[ -z "$QS_AUTH_TOKEN_EXPIRY" || `date +%s` > $[QS_AUTH_TOKEN_EXPIRY - 3600] ]]; then
if [[ -z "$QS_AUTH_REFRESH_TOKEN" || "$QS_AUTH_REFRESH_TOKEN" == "null" ]]; then
qs_log "Refresh token is null, must authenticate..." $QS_LOG_LEVEL_WARN
qs_auth
. ~/.querystrava/setauth.sh
else
qs_log "Refreshing auth token"
qs_set_auth <<< "$(qs_curl POST "https://www.strava.com/oauth/token?client_id=${QS_CLIENT_ID}&client_secret=${QS_CLIENT_SECRET}&refresh_token=${QS_AUTH_REFRESH_TOKEN}&grant_type=refresh_token")"
fi
fi
}
qs_set_auth() {
local QS_AUTH_RESPONSE="$(</dev/stdin)"
qs_log "$(tee ~/.querystrava/setauth.sh <<< "
QS_AUTH_TOKEN=$(jq -r '.access_token' <<< $QS_AUTH_RESPONSE)
QS_AUTH_REFRESH_TOKEN=$(jq -r '.refresh_token' <<< $QS_AUTH_RESPONSE)
QS_AUTH_TOKEN_EXPIRY=$(jq -r '.expires_at' <<< $QS_AUTH_RESPONSE)
")"
}
### QUERIES
qs_query_strava() {
local QS_QUERY_URI=$1
local QS_QUERY_METHOD=${2:-GET} # default GET
local QS_QUERY_BASE_URL='https://www.strava.com/api/v3'
qs_touch_auth
local QS_RESPONSE=$(qs_curl "${QS_QUERY_METHOD}" "${QS_QUERY_BASE_URL}${QS_QUERY_URI}")
jq '.' <<< $QS_RESPONSE
}
qs_query_segments_starred() {
local QS_QUERY_PAGE_SIZE=${1:-2} # default 2
qs_query_strava "/segments/starred?per_page=${QS_QUERY_PAGE_SIZE}"
}
qs_query_segment() {
local QS_SEGMENT_ID=$1
qs_query_strava "/segments/${QS_SEGMENT_ID}"
}
qs_query_segment_leaderboard() {
local QS_SEGMENT_ID=$1
qs_query_strava "/segments/${QS_SEGMENT_ID}/leaderboard?context_entries=0&per_page=5"
}
qs_query_segment_efforts() {
local QS_SEGMENT_ID=$1
local QS_QUERY_PAGE_SIZE=${2:-200} # default 200
qs_query_strava "/segments/${QS_SEGMENT_ID}/all_efforts?per_page=${QS_QUERY_PAGE_SIZE}"
}
qs_query_activity() {
local QS_ACTIVITY_ID=$1
qs_query_strava "/activities/${QS_ACTIVITY_ID}?include_all_efforts=true"
}
qs_query_activity_ids() {
local QS_CURRENT_PAGE=1
local QS_RESULTS_PER_PAGE=30
local QS_PAGE_RESULTS_NUM=${QS_RESULTS_PER_PAGE}
local QS_ACTIVITY_IDS=()
qs_log "Retrieving activities for current user"
while [[ $QS_PAGE_RESULTS_NUM == $QS_RESULTS_PER_PAGE ]]; do
local QS_ACTIVITIES=$(qs_query_strava "/athlete/activities?per_page=${QS_RESULTS_PER_PAGE}&page=${QS_CURRENT_PAGE}")
QS_ACTIVITY_IDS+=("$(jq '.[].id' <<< $QS_ACTIVITIES)")
QS_PAGE_RESULTS_NUM=$(jq 'length' <<< $QS_ACTIVITIES)
QS_CURRENT_PAGE=$[QS_CURRENT_PAGE + 1]
done
local QS_ACTIVITY_IDS_COUNT=$(tr ' ' '\n' <<< "${QS_ACTIVITY_IDS[@]}" | wc -l | xargs)
qs_log "Collected $QS_ACTIVITY_IDS_COUNT $(qs_pluralize $QS_ACTIVITY_IDS_COUNT activity activities)"
echo "${QS_ACTIVITY_IDS[@]}"
}
qs_query_discovered_segments() {
local QS_SEGMENT_IDS=()
while read -r activityId; do
local QS_ACTIVITY=$(qs_query_activity $activityId)
QS_SEGMENT_IDS+=($(jq '.segment_efforts[].segment.id' <<< $QS_ACTIVITY))
local QS_SEGMENT_EFFORTS_COUNT=$(jq '.segment_efforts | length' <<< $QS_ACTIVITY)
qs_log "Collected ${QS_SEGMENT_EFFORTS_COUNT} segment $(qs_pluralize $QS_SEGMENT_EFFORTS_COUNT effort) for activity ${activityId}"
done <<< $(qs_query_activity_ids)
local QS_SEGMENT_IDS_UNIQ=$(tr ' ' '\n' <<< ${QS_SEGMENT_IDS[@]} | sort -u)
local QS_SEGMENT_IDS_COUNT=$(wc -l <<< $QS_SEGMENT_IDS_UNIQ | xargs)
qs_log "Discovered $QS_SEGMENT_IDS_COUNT unique $(qs_pluralize $QS_SEGMENT_IDS_COUNT segment)"
echo "$QS_SEGMENT_IDS_UNIQ"
}
### FILTERS
qs_filter_segments_with_efforts() {
jq 'map(select(.athlete_pr_effort != null))'
}
### PROCESSORS
qs_build_segment_board_from_segments() {
local QS_SEGMENTS_INPUT=$(jq '.')
jq '.[].id' <<< $QS_SEGMENTS_INPUT | qs_build_segments_board_from_ids
}
qs_build_segment_board_from_ids() {
echo > ~/.querystrava/segments.html
echo "
<html>
<head>
<title>Building Segment Board...</title>
<link rel=\"shortcut icon\" href=\"./webfiles/images/favicon.png\" type=\"image/png\">
<link rel=\"stylesheet\" href=\"./webfiles/css/vendor/theme.blue.css\" />
<link rel=\"stylesheet\" href=\"./webfiles/css/segmentboard.css\" />
<script type=\"text/javascript\" src=\"./webfiles/js/vendor/jquery-3.3.1.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/vendor/jquery.tablesorter.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/vendor/jquery.tablesorter.widgets.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/tablesorter.init.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/tablesorter.parsers.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/segments.expansion.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/segments.favicon.js\"></script>
<script type=\"text/javascript\" src=\"./webfiles/js/autoreload.js\"></script>
<base target=\"_blank\" />
</head>
<body>
<a id=\"autoReloadToggle\" href=\"#\" onclick=\"toggleAutoReload(!autoReload); return false\">Toggle auto reload</a>
<table id=\"segmentBoard\" class=\"tablesorter\" data-sortList=\"[[3,0],[4,1]]\">
<thead class=\"center\">
<tr>
<th>ID</th>
<th data-sorter=\"stringLength\">⭐</th>
<th>Name (click to expand leaderboard)</th>
<th data-sorter=\"substringBeforeSpace\">Rank</th>
<th data-sorter=\"percent\">💪<br />(%)</th>
<th data-sorter=\"timestamp\">CR Time<br />(MM:SS)</th>
<th data-sorter=\"timestamp\">PR Time<br />(MM:SS)</th>
<th data-sorter=\"timestamp\">Delta<br />(MM:SS)</th>
<th data-sorter=\"substringBeforeSpace\">Delta<br />(%)</th>
<th>Distance<br />(m)</th>
<th data-sorter=\"percent\">Average<br />Grade<br />(%)</th>
<th data-sorter=\"percent\">Maximum<br />Grade<br />(%)</th>
<th>Minimum<br />Elevation<br />(m)</th>
<th>Maximum<br />Elevation<br />(m)</th>
</thead>
<tbody>
" >> ~/.querystrava/segments.html
local QS_CROWN_TOTAL=0;
while read -r segmentId; do
unset "${!QS_SEGMENT@}"
[[ -z $segmentId ]] && continue
qs_log "Processing segment ${segmentId}"
local QS_SEGMENT=$(qs_query_segment $segmentId)
local QS_SEGMENT_LEADERBOARD=$(qs_query_segment_leaderboard $segmentId)
local QS_SEGMENT_LEADERBOARD_ENTRIES=$(jq '.entries' <<< $QS_SEGMENT_LEADERBOARD)
[[ "$QS_SEGMENT_LEADERBOARD_ENTRIES" == "null" ]] && qs_log "Error retrieving segment ${segmentId} leaderboard" $QS_LOG_LEVEL_ERROR && continue
local QS_SEGMENT_URL="https://www.strava.com/segments/${segmentId}"
local QS_SEGMENT_STAR=$([[ 'true' == `jq '.starred' <<< $QS_SEGMENT` ]] && echo ❤️)
local QS_SEGMENT_CR=$(jq '.[0].elapsed_time' <<< $QS_SEGMENT_LEADERBOARD_ENTRIES)
local QS_SEGMENT_PR=$(jq '.athlete_segment_stats.pr_elapsed_time' <<< $QS_SEGMENT)
local QS_SEGMENT_ENTRIES=$(jq '.entry_count' <<< $QS_SEGMENT_LEADERBOARD)
local QS_SEGMENT_ATHLETE_RANK=$(jq "map(select(.elapsed_time == ${QS_SEGMENT_PR})) | .[0].rank" <<< $QS_SEGMENT_LEADERBOARD_ENTRIES)
local QS_SEGMENT_ATHLETE_RANK_IMPRESSIVENESS=$(bc <<< "scale=4; (1 - (${QS_SEGMENT_ATHLETE_RANK} / ${QS_SEGMENT_ENTRIES})) * 100")
local QS_SEGMENT_CR_DELTA=$[QS_SEGMENT_PR - QS_SEGMENT_CR]
if [ "$QS_SEGMENT_ATHLETE_RANK" -eq 1 ]; then
local QS_CROWN_TOTAL=$[QS_CROWN_TOTAL + 1]
local QS_SEGMENT_CROWN="👑"
local QS_SEGMENT_RUNNERUP_TIME=$(jq "map(select(.rank == 2)) | .[0].elapsed_time" <<< $QS_SEGMENT_LEADERBOARD_ENTRIES)
local QS_SEGMENT_CR_DELTA=$[QS_SEGMENT_CR - QS_SEGMENT_RUNNERUP_TIME]
if [[ "$QS_SEGMENT_CR_DELTA" == "$QS_SEGMENT_CR" ]]; then
local QS_SEGMENT_CR_DELTA=0
local QS_SEGMENT_NEGATIVE_DELTA="-"
fi
fi
local QS_SEGMENT_CR_DELTA_PERCENTAGE=$[10000 * QS_SEGMENT_CR_DELTA / QS_SEGMENT_CR]
echo "
<tr id=\"segmentRow-${segmentId}\" class=\"$(qs_generate_segment_row_rank_class $QS_SEGMENT_ATHLETE_RANK)\">
<td><a href=\"${QS_SEGMENT_URL}\">${segmentId}</a></td>
<td class=\"center\">${QS_SEGMENT_STAR}</td>
<td onclick=\"toggleSegmentIframe(${segmentId})\">
$(jq -r '.name' <<< $QS_SEGMENT) ${QS_SEGMENT_CROWN}
</td>
<td>${QS_SEGMENT_ATHLETE_RANK} / ${QS_SEGMENT_ENTRIES}</td>
<td>$(printf "%.2f" ${QS_SEGMENT_ATHLETE_RANK_IMPRESSIVENESS})</td>
<td>$(qs_seconds_to_timestamp $QS_SEGMENT_CR)</td>
<td>$(qs_seconds_to_timestamp $QS_SEGMENT_PR)</td>
<td>$(qs_seconds_to_timestamp $QS_SEGMENT_CR_DELTA) $(qs_generate_segment_delta_flames $QS_SEGMENT_CR_DELTA)</td>
<td>$(printf "%s%.2f" "$QS_SEGMENT_NEGATIVE_DELTA" $(bc <<< "scale=2; ${QS_SEGMENT_CR_DELTA_PERCENTAGE} / 100")) $(qs_generate_segment_delta_percentage_flames $QS_SEGMENT_CR_DELTA_PERCENTAGE)</td>
<td>$(jq '.distance' <<< $QS_SEGMENT)</td>
<td>$(jq '.average_grade' <<< $QS_SEGMENT)</td>
<td>$(jq '.maximum_grade' <<< $QS_SEGMENT)</td>
<td>$(jq '.elevation_low' <<< $QS_SEGMENT)</td>
<td>$(jq '.elevation_high' <<< $QS_SEGMENT)</td>
</tr>
<tr id=\"expansionRow-${segmentId}\" class=\"segmentInfo collapsible hidden\">
<td onclick=\"toggleSegmentIframe(${segmentId})\">X Close</td>
<td colspan=\"99\">
<h3>Personal efforts</h3>
$(qs_build_segment_efforts_board_from_id $segmentId)
<h3>Global Leaderboard</h3>
<iframe id=\"segmentIframe-${segmentId}\" height=\"405\" width=\"800\" frameborder=\"0\" allowtransparency=\"true\" scrolling=\"no\" data-src=\"https://www.strava.com/segments/${segmentId}/embed\"></iframe>
</td>
</tr>
" >> ~/.querystrava/segments.html
done <<< "$(</dev/stdin)"
echo "
</tbody>
</table>
<script type=\"text/javascript\">
disableAutoReload();
setCrownCount(${QS_CROWN_TOTAL});
</script>
</body>
" >> ~/.querystrava/segments.html
open ~/.querystrava/segments.html
}
qs_build_segment_efforts_board_from_id() {
local QS_SEGMENT_ID=$1
# TODO: pass SEGMENT and SEGMENT_EFFORTS into this function
local QS_SEGMENT_EFFORTS=$(qs_query_segment_efforts $QS_SEGMENT_ID)
local QS_SEGMENT_EFFORTS_COUNT=$(jq '. | length' <<< $QS_SEGMENT_EFFORTS)
local QS_SEGMENT_NAME=$(jq -r '.[0].name' <<< $QS_SEGMENT_EFFORTS)
qs_log "Processing ${QS_SEGMENT_EFFORTS_COUNT} $(qs_pluralize $QS_SEGMENT_EFFORTS_COUNT effort) for segment ${QS_SEGMENT_ID} (${QS_SEGMENT_NAME})"
echo "
<table id=\"segmentEfforts-${QS_SEGMENT_ID}\" class=\"tablesorter\" data-sortList=\"[[1,1]]\">
<thead class=\"center\">
<tr>
<th>ID</th>
<th>Date</th>
<th data-sorter=\"timestamp\">Time<br />(MM:SS)</th>
<th>CR Rank</th>
<th>PR Rank</th>
<th>Average Cadence<br />(SPM)</th>
<th>Average HR<br />(BPM)</th>
<th>Max HR<br />(BPM)</th>
</thead>
<tbody>
"
while read -r segmentEffortId; do
unset "${!QS_SEGMENT_EFFORT_@}"
[[ -z $segmentEffortId ]] && continue
qs_log "Processing segment effort ${segmentEffortId}"
local QS_SEGMENT_EFFORT_OBJECT=$(jq ". | map(select(.id == ${segmentEffortId})) | .[0]" <<< ${QS_SEGMENT_EFFORTS})
local QS_SEGMENT_EFFORT_ACTIVITY_ID=$(jq '.activity.id' <<< ${QS_SEGMENT_EFFORT_OBJECT})
local QS_SEGMENT_EFFORT_ACTIVITY_URL="https://www.strava.com/activities/${QS_SEGMENT_EFFORT_ACTIVITY_ID}"
local QS_SEGMENT_EFFORT_URL="${QS_SEGMENT_EFFORT_ACTIVITY_URL}/segments/${segmentEffortId}"
local QS_SEGMENT_EFFORT_DATE=$(date -j -u -f '%Y-%m-%dT%H:%M:%SZ' "$(jq -r '.start_date' <<< $QS_SEGMENT_EFFORT_OBJECT)" +%s)
local QS_SEGMENT_EFFORT_CR=$(jq '.kom_rank' <<< $QS_SEGMENT_EFFORT_OBJECT)
local QS_SEGMENT_EFFORT_PR=$(jq '.pr_rank' <<< $QS_SEGMENT_EFFORT_OBJECT)
local QS_SEGMENT_EFFORT_AVERAGE_CADENCE=$(jq '.average_cadence' <<< $QS_SEGMENT_EFFORT_OBJECT)
local QS_SEGMENT_EFFORT_AVERAGE_HR=$(jq '.average_heartrate' <<< $QS_SEGMENT_EFFORT_OBJECT)
local QS_SEGMENT_EFFORT_MAX_HR=$(jq '.max_heartrate' <<< $QS_SEGMENT_EFFORT_OBJECT)
echo "
<tr id=\"segmentEffortRow-${segmentEffortId}\">
<td><a href=\"${QS_SEGMENT_EFFORT_URL}\">${segmentEffortId}</a></td>
<td><a href=\"${QS_SEGMENT_EFFORT_ACTIVITY_URL}\" class=\"isoDate\">${QS_SEGMENT_EFFORT_DATE}</a></td>
<td>$(qs_seconds_to_timestamp $(jq '.elapsed_time' <<< $QS_SEGMENT_EFFORT_OBJECT))</td>
<td>$([[ "$QS_SEGMENT_EFFORT_CR" != "null" ]] && echo $QS_SEGMENT_EFFORT_CR)</td>
<td>$([[ "$QS_SEGMENT_EFFORT_PR" != "null" ]] && echo $QS_SEGMENT_EFFORT_PR)</td>
<td>$([[ "$QS_SEGMENT_EFFORT_AVERAGE_CADENCE" != "null" ]] && bc <<< "${QS_SEGMENT_EFFORT_AVERAGE_CADENCE} * 2")</td>
<td>$([[ "$QS_SEGMENT_EFFORT_AVERAGE_HR" != "null" ]] && echo $QS_SEGMENT_EFFORT_AVERAGE_HR)</td>
<td>$([[ "$QS_SEGMENT_EFFORT_MAX_HR" != "null" ]] && echo $QS_SEGMENT_EFFORT_MAX_HR)</td>
</tr>
"
done <<< "$(jq '.[].id' <<< $QS_SEGMENT_EFFORTS)"
echo "
</tbody>
</table>
"
}
qs_seconds_to_timestamp() {
local QS_TIME_IN_SECONDS=$1
local QS_NEGATIVE_TIME=''
if [ "$QS_TIME_IN_SECONDS" -le 0 ]; then
QS_TIME_IN_SECONDS=$[QS_TIME_IN_SECONDS * -1]
QS_NEGATIVE_TIME='-'
fi
printf "%s%d:%02d" "$QS_NEGATIVE_TIME" $[QS_TIME_IN_SECONDS / 60] $[QS_TIME_IN_SECONDS % 60]
}
qs_generate_segment_row_rank_class() {
local QS_SEGMENT_ATHLETE_RANK=$1
if [ "$QS_SEGMENT_ATHLETE_RANK" -eq 1 ]; then
echo "king"
elif [ "$QS_SEGMENT_ATHLETE_RANK" -le 5 ]; then
echo "top5"
elif [ "$QS_SEGMENT_ATHLETE_RANK" -le 10 ]; then
echo "top10"
fi
}
qs_generate_segment_delta_flames() {
local QS_SEGMENT_CR_DELTA=$1
echo "<span class=\"nobreak flames\">"
if [ "$QS_SEGMENT_CR_DELTA" -le -30 ]; then
echo "👌👌👌"
elif [ "$QS_SEGMENT_CR_DELTA" -le -20 ]; then
echo "👌👌"
elif [ "$QS_SEGMENT_CR_DELTA" -le -10 ]; then
echo "👌"
elif [ "$QS_SEGMENT_CR_DELTA" -le 0 ]; then
echo ""
elif [ "$QS_SEGMENT_CR_DELTA" -le 10 ]; then
echo "🔥🔥🔥"
elif [ "$QS_SEGMENT_CR_DELTA" -le 20 ]; then
echo "🔥🔥"
elif [ "$QS_SEGMENT_CR_DELTA" -le 30 ]; then
echo "🔥"
fi
echo "</span>"
}
qs_generate_segment_delta_percentage_flames() {
local QS_SEGMENT_CR_DELTA_PERCENTAGE=$1
echo "<span class=\"nobreak flames\">"
if [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le -2500 ]; then
echo "👌👌👌"
elif [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le -1500 ]; then
echo "👌👌"
elif [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le -750 ]; then
echo "👌"
elif [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le 0 ]; then
echo ""
elif [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le 750 ]; then
echo "🔥🔥🔥"
elif [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le 1500 ]; then
echo "🔥🔥"
elif [ "$QS_SEGMENT_CR_DELTA_PERCENTAGE" -le 2500 ]; then
echo "🔥"
fi
echo "</span>"
}