forked from cyberjunky/python-garminconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·355 lines (293 loc) · 11.8 KB
/
example.py
File metadata and controls
executable file
·355 lines (293 loc) · 11.8 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""
🏃♂️ Simple Garmin Connect API Example
=====================================
This example demonstrates the basic usage of python-garminconnect:
- Authentication with email/password
- Token storage and automatic reuse
- MFA (Multi-Factor Authentication) support
- Comprehensive error handling for all API calls
- Basic API calls for user stats
For a comprehensive demo of all available API calls, see demo.py
Dependencies:
pip3 install garth requests
Environment Variables (optional):
export EMAIL=<your garmin email address>
export PASSWORD=<your garmin password>
export GARMINTOKENS=<path to token storage>
"""
import logging
import os
import sys
from datetime import date
from getpass import getpass
from pathlib import Path
import requests
from garth.exc import GarthException, GarthHTTPError
from garminconnect import (
Garmin,
GarminConnectAuthenticationError,
GarminConnectConnectionError,
GarminConnectTooManyRequestsError,
)
# Suppress garminconnect library logging to avoid tracebacks in normal operation
logging.getLogger("garminconnect").setLevel(logging.CRITICAL)
def safe_api_call(api_method, *args, **kwargs):
"""
Safe API call wrapper with comprehensive error handling.
This demonstrates the error handling patterns used throughout the library.
Returns (success: bool, result: Any, error_message: str)
"""
try:
result = api_method(*args, **kwargs)
return True, result, None
except GarthHTTPError as e:
# Handle specific HTTP errors gracefully
error_str = str(e)
status_code = getattr(getattr(e, "response", None), "status_code", None)
if status_code == 400 or "400" in error_str:
return (
False,
None,
"Endpoint not available (400 Bad Request) - Feature may not be enabled for your account",
)
elif status_code == 401 or "401" in error_str:
return (
False,
None,
"Authentication required (401 Unauthorized) - Please re-authenticate",
)
elif status_code == 403 or "403" in error_str:
return (
False,
None,
"Access denied (403 Forbidden) - Account may not have permission",
)
elif status_code == 404 or "404" in error_str:
return (
False,
None,
"Endpoint not found (404) - Feature may have been moved or removed",
)
elif status_code == 429 or "429" in error_str:
return (
False,
None,
"Rate limit exceeded (429) - Please wait before making more requests",
)
elif status_code == 500 or "500" in error_str:
return (
False,
None,
"Server error (500) - Garmin's servers are experiencing issues",
)
elif status_code == 503 or "503" in error_str:
return (
False,
None,
"Service unavailable (503) - Garmin's servers are temporarily unavailable",
)
else:
return False, None, f"HTTP error: {e}"
except FileNotFoundError:
return (
False,
None,
"No valid tokens found. Please login with your email/password to create new tokens.",
)
except GarminConnectAuthenticationError as e:
return False, None, f"Authentication issue: {e}"
except GarminConnectConnectionError as e:
return False, None, f"Connection issue: {e}"
except GarminConnectTooManyRequestsError as e:
return False, None, f"Rate limit exceeded: {e}"
except Exception as e:
return False, None, f"Unexpected error: {e}"
def get_credentials():
"""Get email and password from environment or user input."""
email = os.getenv("EMAIL")
password = os.getenv("PASSWORD")
if not email:
email = input("Login email: ")
if not password:
password = getpass("Enter password: ")
return email, password
def init_api() -> Garmin | None:
"""Initialize Garmin API with authentication and token management."""
# Configure token storage
tokenstore = os.getenv("GARMINTOKENS", "~/.garminconnect")
tokenstore_path = Path(tokenstore).expanduser()
print(f"🔐 Token storage: {tokenstore_path}")
# Check if token files exist
if tokenstore_path.exists():
print("📄 Found existing token directory")
token_files = list(tokenstore_path.glob("*.json"))
if token_files:
print(
f"🔑 Found {len(token_files)} token file(s): {[f.name for f in token_files]}"
)
else:
print("⚠️ Token directory exists but no token files found")
else:
print("📭 No existing token directory found")
# First try to login with stored tokens
try:
print("🔄 Attempting to use saved authentication tokens...")
garmin = Garmin()
garmin.login(str(tokenstore_path))
print("✅ Successfully logged in using saved tokens!")
return garmin
except (
FileNotFoundError,
GarthHTTPError,
GarminConnectAuthenticationError,
GarminConnectConnectionError,
):
print("🔑 No valid tokens found. Requesting fresh login credentials.")
# Loop for credential entry with retry on auth failure
while True:
try:
# Get credentials
email, password = get_credentials()
print("� Logging in with credentials...")
garmin = Garmin(
email=email, password=password, is_cn=False, return_on_mfa=True
)
result1, result2 = garmin.login()
if result1 == "needs_mfa":
print("🔐 Multi-factor authentication required")
mfa_code = input("Please enter your MFA code: ")
print("🔄 Submitting MFA code...")
try:
garmin.resume_login(result2, mfa_code)
print("✅ MFA authentication successful!")
except GarthHTTPError as garth_error:
# Handle specific HTTP errors from MFA
error_str = str(garth_error)
if "429" in error_str and "Too Many Requests" in error_str:
print("❌ Too many MFA attempts")
print("💡 Please wait 30 minutes before trying again")
sys.exit(1)
elif "401" in error_str or "403" in error_str:
print("❌ Invalid MFA code")
print("💡 Please verify your MFA code and try again")
continue
else:
# Other HTTP errors - don't retry
print(f"❌ MFA authentication failed: {garth_error}")
sys.exit(1)
except GarthException as garth_error:
print(f"❌ MFA authentication failed: {garth_error}")
print("💡 Please verify your MFA code and try again")
continue
# Save tokens for future use
garmin.garth.dump(str(tokenstore_path))
print(f"💾 Authentication tokens saved to: {tokenstore_path}")
print("✅ Login successful!")
return garmin
except GarminConnectAuthenticationError:
print("❌ Authentication failed:")
print("💡 Please check your username and password and try again")
# Continue the loop to retry
continue
except (
FileNotFoundError,
GarthHTTPError,
GarminConnectConnectionError,
requests.exceptions.HTTPError,
) as err:
print(f"❌ Connection error: {err}")
print("💡 Please check your internet connection and try again")
return None
except KeyboardInterrupt:
print("\n👋 Cancelled by user")
return None
def display_user_info(api: Garmin):
"""Display basic user information with proper error handling."""
print("\n" + "=" * 60)
print("👤 User Information")
print("=" * 60)
# Get user's full name
success, full_name, error_msg = safe_api_call(api.get_full_name)
if success:
print(f"📝 Name: {full_name}")
else:
print(f"📝 Name: ⚠️ {error_msg}")
# Get user profile number from device info
success, device_info, error_msg = safe_api_call(api.get_device_last_used)
if success and device_info and device_info.get("userProfileNumber"):
user_profile_number = device_info.get("userProfileNumber")
print(f"🆔 Profile Number: {user_profile_number}")
else:
if not success:
print(f"🆔 Profile Number: ⚠️ {error_msg}")
else:
print("🆔 Profile Number: Not available")
def display_daily_stats(api: Garmin):
"""Display today's activity statistics with proper error handling."""
today = date.today().isoformat()
print("\n" + "=" * 60)
print(f"📊 Daily Stats for {today}")
print("=" * 60)
# Get user summary (steps, calories, etc.)
success, summary, error_msg = safe_api_call(api.get_user_summary, today)
if success and summary:
steps = summary.get("totalSteps", 0)
distance = summary.get("totalDistanceMeters", 0) / 1000 # Convert to km
calories = summary.get("totalKilocalories", 0)
floors = summary.get("floorsClimbed", 0)
print(f"👣 Steps: {steps:,}")
print(f"📏 Distance: {distance:.2f} km")
print(f"🔥 Calories: {calories}")
print(f"🏢 Floors: {floors}")
# Fun motivation based on steps
if steps < 5000:
print("🐌 Time to get those legs moving!")
elif steps > 15000:
print("🏃♂️ You're crushing it today!")
else:
print("👍 Nice progress! Keep it up!")
else:
if not success:
print(f"⚠️ Could not fetch daily stats: {error_msg}")
else:
print("⚠️ No activity summary available for today")
# Get hydration data
success, hydration, error_msg = safe_api_call(api.get_hydration_data, today)
if success and hydration and hydration.get("valueInML"):
hydration_ml = int(hydration.get("valueInML", 0))
hydration_goal = hydration.get("goalInML", 0)
hydration_cups = round(hydration_ml / 240, 1) # 240ml = 1 cup
print(f"💧 Hydration: {hydration_ml}ml ({hydration_cups} cups)")
if hydration_goal > 0:
hydration_percent = round((hydration_ml / hydration_goal) * 100)
print(f"🎯 Goal Progress: {hydration_percent}% of {hydration_goal}ml")
else:
if not success:
print(f"💧 Hydration: ⚠️ {error_msg}")
else:
print("💧 Hydration: No data available")
def main():
"""Main example demonstrating basic Garmin Connect API usage."""
print("🏃♂️ Simple Garmin Connect API Example")
print("=" * 60)
# Initialize API with authentication (will only prompt for credentials if needed)
api = init_api()
if not api:
print("❌ Failed to initialize API. Exiting.")
return
# Display user information
display_user_info(api)
# Display daily statistics
display_daily_stats(api)
print("\n" + "=" * 60)
print("✅ Example completed successfully!")
print("💡 For a comprehensive demo of all API features, run: python demo.py")
print("=" * 60)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n🚪 Exiting example. Goodbye! 👋")
except Exception as e:
print(f"\n❌ Unexpected error: {e}")