2
2
import os
3
3
import time
4
4
5
- from config import TCX_FOLDER
6
- from strava_sync import run_strava_sync
7
5
from stravalib .exc import RateLimitTimeout , ActivityUploadFailed
8
6
from tcxreader .tcxreader import TCXReader
9
7
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
+ )
11
15
12
16
13
17
def get_to_generate_files (last_time ):
14
18
"""
15
- reuturn to values one dict for upload
19
+ return to values one dict for upload
16
20
and one sorted list for next time upload
17
21
"""
18
22
file_names = os .listdir (TCX_FOLDER )
@@ -23,10 +27,9 @@ def get_to_generate_files(last_time):
23
27
if i .endswith (".tcx" )
24
28
]
25
29
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 }
27
31
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
30
33
}
31
34
32
35
return sorted (list (tcx_files_dict .keys ())), tcx_files_dict
@@ -57,19 +60,25 @@ def get_to_generate_files(last_time):
57
60
to_upload_time_list , to_upload_dict = get_to_generate_files (last_time )
58
61
index = 1
59
62
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
+ ]
61
70
try :
62
- upload_file_to_strava (client , tcx_file , "tcx" )
71
+ upload_file_to_strava_with_type (client , tcx_file , "tcx" , strava_type )
63
72
except RateLimitTimeout as e :
64
73
timeout = e .timeout
65
74
print (f"Strava API Rate Limit Timeout. Retry in { timeout } seconds" )
66
75
print ()
67
76
time .sleep (timeout )
68
77
# 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 )
70
79
71
80
except ActivityUploadFailed as e :
72
- print (f"Upload faild error { str (e )} " )
81
+ print (f"Upload failed error { str (e )} " )
73
82
# spider rule
74
83
time .sleep (1 )
75
84
0 commit comments