From 657d135b5c9660d510e5addf155ce758e4d48e14 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Wed, 11 Oct 2023 22:39:06 +0800 Subject: [PATCH] fix: upload to strava type --- run_page/codoon_sync.py | 16 +++++++--------- run_page/utils.py | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/run_page/codoon_sync.py b/run_page/codoon_sync.py index 505e8c70b12..aa0d198975e 100755 --- a/run_page/codoon_sync.py +++ b/run_page/codoon_sync.py @@ -6,29 +6,27 @@ import os import time import urllib.parse +import xml.etree.ElementTree as ET from collections import namedtuple from datetime import datetime, timedelta +import eviltransform import gpxpy +import numpy as np import polyline import requests -import eviltransform from config import ( BASE_TIMEZONE, GPX_FOLDER, - TCX_FOLDER, JSON_FILE, SQL_FILE, + TCX_FOLDER, run_map, start_point, ) from generator import Generator - -from utils import adjust_time_to_utc, adjust_timestamp_to_utc, to_date - -import numpy as np -import xml.etree.ElementTree as ET from tzlocal import get_localzone +from utils import adjust_time_to_utc, adjust_timestamp_to_utc, to_date # struct body FitType = np.dtype( @@ -238,13 +236,13 @@ def tcx_job(run_data): if "points" in run_data: own_points = run_data["points"] # track points # get single bpm - if own_heart_rate != None: + if own_heart_rate is not None: for single_time, single_bpm in own_heart_rate.items(): single_time = adjust_timestamp_to_utc(single_time, str(get_localzone())) # set bpm data fit_array = set_array(fit_array, single_time, single_bpm, None, None, None) # get single track point - if own_points != None: + if own_points is not None: for point in own_points: repeat_flag = False # TODO add elevation information diff --git a/run_page/utils.py b/run_page/utils.py index 4fb8c66b717..b003707284a 100644 --- a/run_page/utils.py +++ b/run_page/utils.py @@ -90,17 +90,28 @@ def get_strava_last_time(client, is_milliseconds=True): return 0 -def upload_file_to_strava(client, file_name, data_type): +def upload_file_to_strava(client, file_name, data_type, force_to_run=True): with open(file_name, "rb") as f: try: - r = client.upload_activity(activity_file=f, data_type=data_type) + if force_to_run: + r = client.upload_activity( + activity_file=f, data_type=data_type, activity_type="run" + ) + else: + r = client.upload_activity(activity_file=f, data_type=data_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) + if force_to_run: + r = client.upload_activity( + activity_file=f, data_type=data_type, activity_type="run" + ) + else: + r = client.upload_activity(activity_file=f, data_type=data_type) print( f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}." )