Skip to content

Commit

Permalink
Provide another way to upload all kinds of sport type of TCX file to …
Browse files Browse the repository at this point in the history
…Strava.
  • Loading branch information
Vensent committed Apr 22, 2024
1 parent 5d84bd4 commit 0602a90
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
31 changes: 20 additions & 11 deletions run_page/tcx_to_strava_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
import os
import time

from config import TCX_FOLDER
from strava_sync import run_strava_sync
from stravalib.exc import RateLimitTimeout, ActivityUploadFailed
from tcxreader.tcxreader import TCXReader

from utils import make_strava_client, get_strava_last_time, upload_file_to_strava
from config import TCX_FOLDER, STRAVA_GARMIN_TYPE_DICT
from strava_sync import run_strava_sync
from utils import (
make_strava_client,
get_strava_last_time,
upload_file_to_strava_with_type,
)


def get_to_generate_files(last_time):
"""
reuturn to values one dict for upload
return to values one dict for upload
and one sorted list for next time upload
"""
file_names = os.listdir(TCX_FOLDER)
Expand All @@ -23,10 +27,9 @@ def get_to_generate_files(last_time):
if i.endswith(".tcx")
]
tcx_files_dict = {
int(i[0].trackpoints[0].time.timestamp()): i[1]
int(i[0].start_time.timestamp()): {"file": i[1], "type": i[0].activity_type}
for i in tcx_files
if len(i[0].trackpoints) > 0
and int(i[0].trackpoints[0].time.timestamp()) > last_time
if i[0].start_time and int(i[0].start_time.timestamp()) > last_time
}

return sorted(list(tcx_files_dict.keys())), tcx_files_dict
Expand Down Expand Up @@ -57,19 +60,25 @@ def get_to_generate_files(last_time):
to_upload_time_list, to_upload_dict = get_to_generate_files(last_time)
index = 1
for i in to_upload_time_list:
tcx_file = to_upload_dict.get(i)
tcx_file = to_upload_dict.get(i)["file"]
type = to_upload_dict.get(i)["type"].lower()
if type not in STRAVA_GARMIN_TYPE_DICT.values():
continue
strava_type = list(STRAVA_GARMIN_TYPE_DICT.keys())[
list(STRAVA_GARMIN_TYPE_DICT.values()).index(type)
]
try:
upload_file_to_strava(client, tcx_file, "tcx")
upload_file_to_strava_with_type(client, tcx_file, "tcx", strava_type)
except RateLimitTimeout as e:
timeout = e.timeout
print(f"Strava API Rate Limit Timeout. Retry in {timeout} seconds")
print()
time.sleep(timeout)
# try previous again
upload_file_to_strava(client, tcx_file, "tcx")
upload_file_to_strava_with_type(client, tcx_file, "tcx", strava_type)

except ActivityUploadFailed as e:
print(f"Upload faild error {str(e)}")
print(f"Upload failed error {str(e)}")
# spider rule
time.sleep(1)

Expand Down
20 changes: 20 additions & 0 deletions run_page/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,23 @@ def upload_file_to_strava(client, file_name, data_type, force_to_run=True):
print(
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}."
)


def upload_file_to_strava_with_type(client, file_name, data_type, type):
with open(file_name, "rb") as f:
try:
r = client.upload_activity(
activity_file=f, data_type=data_type, activity_type=type
)
except RateLimitExceeded as e:
timeout = e.timeout
print()
print(f"Strava API Rate Limit Exceeded. Retry after {timeout} seconds")
print()
time.sleep(timeout)
r = client.upload_activity(
activity_file=f, data_type=data_type, activity_type=type
)
print(
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}, type: {type}."
)

0 comments on commit 0602a90

Please sign in to comment.