Skip to content

Commit 0602a90

Browse files
committed
Provide another way to upload all kinds of sport type of TCX file to Strava.
1 parent 5d84bd4 commit 0602a90

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

run_page/tcx_to_strava_sync.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
import os
33
import time
44

5-
from config import TCX_FOLDER
6-
from strava_sync import run_strava_sync
75
from stravalib.exc import RateLimitTimeout, ActivityUploadFailed
86
from tcxreader.tcxreader import TCXReader
97

10-
from utils import make_strava_client, get_strava_last_time, upload_file_to_strava
8+
from config import TCX_FOLDER, STRAVA_GARMIN_TYPE_DICT
9+
from strava_sync import run_strava_sync
10+
from utils import (
11+
make_strava_client,
12+
get_strava_last_time,
13+
upload_file_to_strava_with_type,
14+
)
1115

1216

1317
def get_to_generate_files(last_time):
1418
"""
15-
reuturn to values one dict for upload
19+
return to values one dict for upload
1620
and one sorted list for next time upload
1721
"""
1822
file_names = os.listdir(TCX_FOLDER)
@@ -23,10 +27,9 @@ def get_to_generate_files(last_time):
2327
if i.endswith(".tcx")
2428
]
2529
tcx_files_dict = {
26-
int(i[0].trackpoints[0].time.timestamp()): i[1]
30+
int(i[0].start_time.timestamp()): {"file": i[1], "type": i[0].activity_type}
2731
for i in tcx_files
28-
if len(i[0].trackpoints) > 0
29-
and int(i[0].trackpoints[0].time.timestamp()) > last_time
32+
if i[0].start_time and int(i[0].start_time.timestamp()) > last_time
3033
}
3134

3235
return sorted(list(tcx_files_dict.keys())), tcx_files_dict
@@ -57,19 +60,25 @@ def get_to_generate_files(last_time):
5760
to_upload_time_list, to_upload_dict = get_to_generate_files(last_time)
5861
index = 1
5962
for i in to_upload_time_list:
60-
tcx_file = to_upload_dict.get(i)
63+
tcx_file = to_upload_dict.get(i)["file"]
64+
type = to_upload_dict.get(i)["type"].lower()
65+
if type not in STRAVA_GARMIN_TYPE_DICT.values():
66+
continue
67+
strava_type = list(STRAVA_GARMIN_TYPE_DICT.keys())[
68+
list(STRAVA_GARMIN_TYPE_DICT.values()).index(type)
69+
]
6170
try:
62-
upload_file_to_strava(client, tcx_file, "tcx")
71+
upload_file_to_strava_with_type(client, tcx_file, "tcx", strava_type)
6372
except RateLimitTimeout as e:
6473
timeout = e.timeout
6574
print(f"Strava API Rate Limit Timeout. Retry in {timeout} seconds")
6675
print()
6776
time.sleep(timeout)
6877
# try previous again
69-
upload_file_to_strava(client, tcx_file, "tcx")
78+
upload_file_to_strava_with_type(client, tcx_file, "tcx", strava_type)
7079

7180
except ActivityUploadFailed as e:
72-
print(f"Upload faild error {str(e)}")
81+
print(f"Upload failed error {str(e)}")
7382
# spider rule
7483
time.sleep(1)
7584

run_page/utils.py

+20
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,23 @@ def upload_file_to_strava(client, file_name, data_type, force_to_run=True):
117117
print(
118118
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}."
119119
)
120+
121+
122+
def upload_file_to_strava_with_type(client, file_name, data_type, type):
123+
with open(file_name, "rb") as f:
124+
try:
125+
r = client.upload_activity(
126+
activity_file=f, data_type=data_type, activity_type=type
127+
)
128+
except RateLimitExceeded as e:
129+
timeout = e.timeout
130+
print()
131+
print(f"Strava API Rate Limit Exceeded. Retry after {timeout} seconds")
132+
print()
133+
time.sleep(timeout)
134+
r = client.upload_activity(
135+
activity_file=f, data_type=data_type, activity_type=type
136+
)
137+
print(
138+
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}, type: {type}."
139+
)

0 commit comments

Comments
 (0)