-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgarmin-activities.py
284 lines (237 loc) · 12.7 KB
/
garmin-activities.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from datetime import datetime, timezone
from garminconnect import Garmin
from notion_client import Client
from dotenv import load_dotenv
import pytz
import os
# Your local time zone, replace with the appropriate one if needed
local_tz = pytz.timezone('America/Toronto')
ACTIVITY_ICONS = {
"Barre": "https://img.icons8.com/?size=100&id=66924&format=png&color=000000",
"Breathwork": "https://img.icons8.com/?size=100&id=9798&format=png&color=000000",
"Cardio": "https://img.icons8.com/?size=100&id=71221&format=png&color=000000",
"Cycling": "https://img.icons8.com/?size=100&id=47443&format=png&color=000000",
"Hiking": "https://img.icons8.com/?size=100&id=9844&format=png&color=000000",
"Indoor Cardio": "https://img.icons8.com/?size=100&id=62779&format=png&color=000000",
"Indoor Cycling": "https://img.icons8.com/?size=100&id=47443&format=png&color=000000",
"Indoor Rowing": "https://img.icons8.com/?size=100&id=71098&format=png&color=000000",
"Pilates": "https://img.icons8.com/?size=100&id=9774&format=png&color=000000",
"Meditation": "https://img.icons8.com/?size=100&id=9798&format=png&color=000000",
"Rowing": "https://img.icons8.com/?size=100&id=71491&format=png&color=000000",
"Running": "https://img.icons8.com/?size=100&id=k1l1XFkME39t&format=png&color=000000",
"Strength Training": "https://img.icons8.com/?size=100&id=107640&format=png&color=000000",
"Stretching": "https://img.icons8.com/?size=100&id=djfOcRn1m_kh&format=png&color=000000",
"Swimming": "https://img.icons8.com/?size=100&id=9777&format=png&color=000000",
"Treadmill Running": "https://img.icons8.com/?size=100&id=9794&format=png&color=000000",
"Walking": "https://img.icons8.com/?size=100&id=9807&format=png&color=000000",
"Yoga": "https://img.icons8.com/?size=100&id=9783&format=png&color=000000",
# Add more mappings as needed
}
def get_all_activities(garmin, limit=1000):
return garmin.get_activities(0, limit)
def format_activity_type(activity_type, activity_name=""):
# First format the activity type as before
formatted_type = activity_type.replace('_', ' ').title() if activity_type else "Unknown"
# Initialize subtype as the same as the main type
activity_subtype = formatted_type
activity_type = formatted_type
# Map of specific subtypes to their main types
activity_mapping = {
"Barre": "Strength",
"Indoor Cardio": "Cardio",
"Indoor Cycling": "Cycling",
"Indoor Rowing": "Rowing",
"Speed Walking": "Walking",
"Strength Training": "Strength",
"Treadmill Running": "Running"
}
# Special replacement for Rowing V2
if formatted_type == "Rowing V2":
activity_type = "Rowing"
# Special case for Yoga and Pilates
elif formatted_type in ["Yoga", "Pilates"]:
activity_type = "Yoga/Pilates"
activity_subtype = formatted_type
# If the formatted type is in our mapping, update both main type and subtype
if formatted_type in activity_mapping:
activity_type = activity_mapping[formatted_type]
activity_subtype = formatted_type
# Special cases for activity names
if activity_name and "meditation" in activity_name.lower():
return "Meditation", "Meditation"
if activity_name and "barre" in activity_name.lower():
return "Strength", "Barre"
if activity_name and "stretch" in activity_name.lower():
return "Stretching", "Stretching"
return activity_type, activity_subtype
def format_entertainment(activity_name):
return activity_name.replace('ENTERTAINMENT', 'Netflix')
def format_training_message(message):
messages = {
'NO_': 'No Benefit',
'MINOR_': 'Some Benefit',
'RECOVERY_': 'Recovery',
'MAINTAINING_': 'Maintaining',
'IMPROVING_': 'Impacting',
'IMPACTING_': 'Impacting',
'HIGHLY_': 'Highly Impacting',
'OVERREACHING_': 'Overreaching'
}
for key, value in messages.items():
if message.startswith(key):
return value
return message
def format_training_effect(trainingEffect_label):
return trainingEffect_label.replace('_', ' ').title()
def format_pace(average_speed):
if average_speed > 0:
pace_min_km = 1000 / (average_speed * 60) # Convert to min/km
minutes = int(pace_min_km)
seconds = int((pace_min_km - minutes) * 60)
return f"{minutes}:{seconds:02d} min/km"
else:
return ""
def activity_exists(client, database_id, activity_date, activity_type, activity_name):
# Check if an activity already exists in the Notion database and return it if found.
# Handle the activity_type which is now a tuple
if isinstance(activity_type, tuple):
main_type, _ = activity_type
else:
main_type = activity_type[0] if isinstance(activity_type, (list, tuple)) else activity_type
# Determine the correct activity type for the lookup
lookup_type = "Stretching" if "stretch" in activity_name.lower() else main_type
query = client.databases.query(
database_id=database_id,
filter={
"and": [
{"property": "Date", "date": {"equals": activity_date.split('T')[0]}},
{"property": "Activity Type", "select": {"equals": lookup_type}},
{"property": "Activity Name", "title": {"equals": activity_name}}
]
}
)
results = query['results']
return results[0] if results else None
def activity_needs_update(existing_activity, new_activity):
existing_props = existing_activity['properties']
activity_name = new_activity.get('activityName', '').lower()
activity_type, activity_subtype = format_activity_type(
new_activity.get('activityType', {}).get('typeKey', 'Unknown'),
activity_name
)
# Check if 'Subactivity Type' property exists
has_subactivity = (
'Subactivity Type' in existing_props and
existing_props['Subactivity Type'] is not None and
existing_props['Subactivity Type'].get('select') is not None
)
return (
existing_props['Distance (km)']['number'] != round(new_activity.get('distance', 0) / 1000, 2) or
existing_props['Duration (min)']['number'] != round(new_activity.get('duration', 0) / 60, 2) or
existing_props['Calories']['number'] != round(new_activity.get('calories', 0)) or
existing_props['Avg Pace']['rich_text'][0]['text']['content'] != format_pace(new_activity.get('averageSpeed', 0)) or
existing_props['Training Effect']['select']['name'] != format_training_effect(new_activity.get('trainingEffectLabel', 'Unknown')) or
existing_props['Aerobic']['number'] != round(new_activity.get('aerobicTrainingEffect', 0), 1) or
existing_props['Aerobic Effect']['select']['name'] != format_training_message(new_activity.get('aerobicTrainingEffectMessage', 'Unknown')) or
existing_props['Anaerobic']['number'] != round(new_activity.get('anaerobicTrainingEffect', 0), 1) or
existing_props['Anaerobic Effect']['select']['name'] != format_training_message(new_activity.get('anaerobicTrainingEffectMessage', 'Unknown')) or
existing_props['PR']['checkbox'] != new_activity.get('pr', False) or
existing_props['Activity Type']['select']['name'] != activity_type or
(has_subactivity and existing_props['Subactivity Type']['select']['name'] != activity_subtype) or
(not has_subactivity) # If the property doesn't exist, we need an update
)
def create_activity(client, database_id, activity):
# Create a new activity in the Notion database
activity_date = activity.get('startTimeGMT')
activity_name = format_entertainment(activity.get('activityName', 'Unnamed Activity'))
activity_type, activity_subtype = format_activity_type(
activity.get('activityType', {}).get('typeKey', 'Unknown'),
activity_name
)
# Get icon for the activity type
icon_url = ACTIVITY_ICONS.get(activity_subtype if activity_subtype != activity_type else activity_type)
properties = {
"Date": {"date": {"start": activity_date}},
"Activity Type": {"select": {"name": activity_type}},
"Subactivity Type": {"select": {"name": activity_subtype}},
"Activity Name": {"title": [{"text": {"content": activity_name}}]},
"Distance (km)": {"number": round(activity.get('distance', 0) / 1000, 2)},
"Duration (min)": {"number": round(activity.get('duration', 0) / 60, 2)},
"Calories": {"number": round(activity.get('calories', 0))},
"Avg Pace": {"rich_text": [{"text": {"content": format_pace(activity.get('averageSpeed', 0))}}]},
"Training Effect": {"select": {"name": format_training_effect(activity.get('trainingEffectLabel', 'Unknown'))}},
"Aerobic": {"number": round(activity.get('aerobicTrainingEffect', 0), 1)},
"Aerobic Effect": {"select": {"name": format_training_message(activity.get('aerobicTrainingEffectMessage', 'Unknown'))}},
"Anaerobic": {"number": round(activity.get('anaerobicTrainingEffect', 0), 1)},
"Anaerobic Effect": {"select": {"name": format_training_message(activity.get('anaerobicTrainingEffectMessage', 'Unknown'))}},
"PR": {"checkbox": activity.get('pr', False)}
}
page = {
"parent": {"database_id": database_id},
"properties": properties,
}
if icon_url:
page["icon"] = {"type": "external", "external": {"url": icon_url}}
client.pages.create(**page)
def update_activity(client, existing_activity, new_activity):
# Update an existing activity in the Notion database with new data
activity_name = new_activity.get('activityName', 'Unnamed Activity')
activity_type, activity_subtype = format_activity_type(
new_activity.get('activityType', {}).get('typeKey', 'Unknown'),
activity_name
)
# Get icon for the activity type
icon_url = ACTIVITY_ICONS.get(activity_subtype if activity_subtype != activity_type else activity_type)
properties = {
"Activity Type": {"select": {"name": activity_type}},
"Subactivity Type": {"select": {"name": activity_subtype}},
"Distance (km)": {"number": round(new_activity.get('distance', 0) / 1000, 2)},
"Duration (min)": {"number": round(new_activity.get('duration', 0) / 60, 2)},
"Calories": {"number": round(new_activity.get('calories', 0))},
"Avg Pace": {"rich_text": [{"text": {"content": format_pace(new_activity.get('averageSpeed', 0))}}]},
"Training Effect": {"select": {"name": format_training_effect(new_activity.get('trainingEffectLabel', 'Unknown'))}},
"Aerobic": {"number": round(new_activity.get('aerobicTrainingEffect', 0), 1)},
"Aerobic Effect": {"select": {"name": format_training_message(new_activity.get('aerobicTrainingEffectMessage', 'Unknown'))}},
"Anaerobic": {"number": round(new_activity.get('anaerobicTrainingEffect', 0), 1)},
"Anaerobic Effect": {"select": {"name": format_training_message(new_activity.get('anaerobicTrainingEffectMessage', 'Unknown'))}},
"PR": {"checkbox": new_activity.get('pr', False)}
}
update = {
"page_id": existing_activity['id'],
"properties": properties,
}
if icon_url:
update["icon"] = {"type": "external", "external": {"url": icon_url}}
client.pages.update(**update)
def main():
load_dotenv()
# Initialize Garmin and Notion clients using environment variables
garmin_email = os.getenv("GARMIN_EMAIL")
garmin_password = os.getenv("GARMIN_PASSWORD")
notion_token = os.getenv("NOTION_TOKEN")
database_id = os.getenv("NOTION_DB_ID")
# Initialize Garmin client and login
garmin = Garmin(garmin_email, garmin_password)
garmin.login()
client = Client(auth=notion_token)
# Get all activities
activities = get_all_activities(garmin)
# Process all activities
for activity in activities:
activity_date = activity.get('startTimeGMT')
activity_name = format_entertainment(activity.get('activityName', 'Unnamed Activity'))
activity_type, activity_subtype = format_activity_type(
activity.get('activityType', {}).get('typeKey', 'Unknown'),
activity_name
)
# Check if activity already exists in Notion
existing_activity = activity_exists(client, database_id, activity_date, activity_type, activity_name)
if existing_activity:
if activity_needs_update(existing_activity, activity):
update_activity(client, existing_activity, activity)
# print(f"Updated: {activity_type} - {activity_name}")
else:
create_activity(client, database_id, activity)
# print(f"Created: {activity_type} - {activity_name}")
if __name__ == '__main__':
main()