From 0602a90b92defda7f6c590a7146d2fbcb0a5804b Mon Sep 17 00:00:00 2001 From: wangwenqi Date: Tue, 23 Apr 2024 00:47:30 +0800 Subject: [PATCH] Provide another way to upload all kinds of sport type of TCX file to Strava. --- run_page/tcx_to_strava_sync.py | 31 ++++++++++++++++++++----------- run_page/utils.py | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/run_page/tcx_to_strava_sync.py b/run_page/tcx_to_strava_sync.py index 54227d06711..8936bfe7785 100755 --- a/run_page/tcx_to_strava_sync.py +++ b/run_page/tcx_to_strava_sync.py @@ -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) @@ -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 @@ -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) diff --git a/run_page/utils.py b/run_page/utils.py index de6945ee48a..c66091e46a4 100644 --- a/run_page/utils.py +++ b/run_page/utils.py @@ -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}." + )