forked from yihong0618/running_page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
119 lines (97 loc) · 3.75 KB
/
utils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
import time
from datetime import datetime
import pytz
try:
from rich import print
except:
pass
from generator import Generator
from stravalib.client import Client
from stravalib.exc import RateLimitExceeded
def adjust_time(time, tz_name):
tc_offset = datetime.now(pytz.timezone(tz_name)).utcoffset()
return time + tc_offset
def adjust_time_to_utc(time, tz_name):
tc_offset = datetime.now(pytz.timezone(tz_name)).utcoffset()
return time - tc_offset
def adjust_timestamp_to_utc(timestamp, tz_name):
tc_offset = datetime.now(pytz.timezone(tz_name)).utcoffset()
delta = int(tc_offset.total_seconds())
return int(timestamp) - delta
def to_date(ts):
# TODO use https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat
# once we decide to move on to python v3.7+
ts_fmts = ["%Y-%m-%dT%H:%M:%S", "%Y-%m-%dT%H:%M:%S.%f"]
for ts_fmt in ts_fmts:
try:
# performance with using exceptions
# shouldn't be an issue since it's an offline cmdline tool
return datetime.strptime(ts, ts_fmt)
except ValueError:
print(
f"Warning: Can not execute strptime {ts} with ts_fmt {ts_fmt}, try next one..."
)
pass
raise ValueError(f"cannot parse timestamp {ts} into date with fmts: {ts_fmts}")
def make_activities_file(sql_file, data_dir, json_file, file_suffix="gpx"):
generator = Generator(sql_file)
generator.sync_from_data_dir(data_dir, file_suffix=file_suffix)
activities_list = generator.load()
with open(json_file, "w") as f:
json.dump(activities_list, f)
def make_strava_client(client_id, client_secret, refresh_token):
client = Client()
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"]
return client
def get_strava_last_time(client, is_milliseconds=True):
"""
if there is no activities cause exception return 0
"""
try:
activity = None
activities = client.get_activities(limit=10)
activities = list(activities)
activities.sort(key=lambda x: x.start_date, reverse=True)
# for else in python if you don't know please google it.
for a in activities:
if a.type == "Run":
activity = a
break
else:
return 0
end_date = activity.start_date + activity.elapsed_time
last_time = int(datetime.timestamp(end_date))
if is_milliseconds:
last_time = last_time * 1000
return last_time
except Exception as e:
print(f"Something wrong to get last time err: {str(e)}")
return 0
def upload_file_to_strava(client, file_name, data_type, force_to_run=True):
with open(file_name, "rb") as f:
try:
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)
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}."
)