-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_2_strava.py
81 lines (67 loc) · 2.32 KB
/
fit_2_strava.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from stravalib.client import Client
from stravalib.exc import RateLimitExceeded, RateLimitTimeout, ActivityUploadFailed
from config import (
FIT_OUTPUT_DIR,
STRAVA_CLIENT_ID,
STRAVA_CLIENT_SECRET,
STRAVA_REFRESH_TOKEN,
STRAVA_ACCESS_TOKEN,
)
import time
import os
def upload_file_to_strava(client, file_name, data_type):
with open(file_name, "rb") as f:
try:
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)
print(
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}."
)
def main():
client_id = STRAVA_CLIENT_ID
client_secret = STRAVA_CLIENT_SECRET
refresh_token = STRAVA_REFRESH_TOKEN
access_token = STRAVA_ACCESS_TOKEN
folder_path = FIT_OUTPUT_DIR
client = Client()
if len(access_token) != 40:
refresh_response = client.refresh_access_token(
client_id=client_id,
client_secret=client_secret,
refresh_token=refresh_token,
)
client.access_token = refresh_response["access_token"]
else:
client.access_token = access_token
print(
"For {id}, I now have an access token {token}".format(
id=client_id, token=client.access_token
)
)
# get file list
files = os.listdir(folder_path)
for file_name in files:
print("======================\n", file_name)
if not file_name.endswith(".fit"):
continue
file_name = os.path.join(folder_path, file_name)
try:
upload_file_to_strava(client, file_name, "fit")
except RateLimitTimeout as e:
timeout = e.timeout
print(f"Strava API Rate Limit Timeout. Retry in {timeout} seconds\n")
time.sleep(timeout)
# try previous again
upload_file_to_strava(client, file_name, "fit")
except ActivityUploadFailed as e:
print(f"Upload faild error {str(e)}")
# spider rule
time.sleep(5)
if __name__ == "__main__":
main()