-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistRoutes.sh
executable file
·57 lines (46 loc) · 1.95 KB
/
listRoutes.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
function listRoutes() {
#GET /athletes/{ID}/routes
# read -p 'Athlete ID : ' ATHLETE_ID #Commented out, because : we cannot request routes from an another athlete. Routes must be added to the currently authorized athlete by starring them.
#GET /athlete
FULL_RESPONSE=$(curl https://www.strava.com/api/v3/athlete?access_token=${ACCESS_TOKEN} | python -m json.tool)
ATHLETE_ID=$(echo $FULL_RESPONSE | jq -r .id)
echo "Listing routes for self (athlete id : ${ATHLETE_ID})"
OUT_DIR="allRoutes"
rm -rf "$OUT_DIR"
mkdir -p "$OUT_DIR"
pushd "$OUT_DIR"
PAGE="0"
while [ true ];
do
let "PAGE+=1"
QUERY_PARAMETERS="&per_page=200" #30 is the default value
if [ ! -z "$PAGE" ];
then
QUERY_PARAMETERS="$QUERY_PARAMETERS&page=${PAGE}"
fi
FULL_RESPONSE=$(curl https://www.strava.com/api/v3/athletes/${ATHLETE_ID}/routes?access_token=${ACCESS_TOKEN}${QUERY_PARAMETERS} | python -m json.tool)
COUNT=$(echo $FULL_RESPONSE | jq '. | length')
echo "GET athletes/${ATHLETE_ID}/routes?access_token=${ACCESS_TOKEN}${QUERY_PARAMETERS} FULL_RESPONSE : \n $FULL_RESPONSE"
echo "size : ${COUNT}"
echo "$FULL_RESPONSE" >> "$PAGE.json"
if [ "$COUNT" -le 0 ];
then
echo "no more pages"
break
else
echo "loading more pages..."
fi
done
OUT_FILE="all.json"
jq -s '[.[][]]' *.json > $OUT_FILE
echo "All results can be found under $OUT_DIR/$OUT_FILE"
popd #OUT_DIR
echo ""
echo "Exporting all routes to GPX : "
jq -c '.[]|{id: .id, name: .name}' $OUT_DIR/$OUT_FILE | while read item; do
ROUTE_NAME=$(echo "$item" | jq -r .name)
ROUTE_ID=$(echo "$item" | jq .id)
echo "Exporting route with name : ${ROUTE_NAME}, id : ${ROUTE_ID} : to $OUT_DIR/${ROUTE_NAME} (strava route id : $ROUTE_ID).gpx"
curl https://www.strava.com/api/v3/routes/${ROUTE_ID}/export_gpx?access_token=${ACCESS_TOKEN} -o "$OUT_DIR/${ROUTE_NAME} (strava route id : $ROUTE_ID).gpx"
done
}