-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
example.py
executable file
·931 lines (837 loc) · 38.9 KB
/
example.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
#!/usr/bin/env python3
"""
pip3 install garth requests readchar
export EMAIL=<your garmin email>
export PASSWORD=<your garmin password>
"""
import datetime
from datetime import timezone
import json
import logging
import os
import sys
from getpass import getpass
import readchar
import requests
from garth.exc import GarthHTTPError
from garminconnect import (
Garmin,
GarminConnectAuthenticationError,
GarminConnectConnectionError,
GarminConnectTooManyRequestsError,
)
# Configure debug logging
# logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load environment variables if defined
email = os.getenv("EMAIL")
password = os.getenv("PASSWORD")
tokenstore = os.getenv("GARMINTOKENS") or "~/.garminconnect"
tokenstore_base64 = os.getenv("GARMINTOKENS_BASE64") or "~/.garminconnect_base64"
api = None
# Example selections and settings
# Let's say we want to scrape all activities using switch menu_option "p". We change the values of the below variables, IE startdate days, limit,...
today = datetime.date.today()
startdate = today - datetime.timedelta(days=7) # Select past week
start = 0
limit = 100
start_badge = 1 # Badge related calls calls start counting at 1
activitytype = "" # Possible values are: cycling, running, swimming, multi_sport, fitness_equipment, hiking, walking, other
activityfile = "MY_ACTIVITY.fit" # Supported file types are: .fit .gpx .tcx
weight = 89.6
weightunit = "kg"
# workout_example = """
# {
# 'workoutId': "random_id",
# 'ownerId': "random",
# 'workoutName': 'Any workout name',
# 'description': 'FTP 200, TSS 1, NP 114, IF 0.57',
# 'sportType': {'sportTypeId': 2, 'sportTypeKey': 'cycling'},
# 'workoutSegments': [
# {
# 'segmentOrder': 1,
# 'sportType': {'sportTypeId': 2, 'sportTypeKey': 'cycling'},
# 'workoutSteps': [
# {'type': 'ExecutableStepDTO', 'stepOrder': 1,
# 'stepType': {'stepTypeId': 3, 'stepTypeKey': 'interval'}, 'childStepId': None,
# 'endCondition': {'conditionTypeId': 2, 'conditionTypeKey': 'time'}, 'endConditionValue': 60,
# 'targetType': {'workoutTargetTypeId': 2, 'workoutTargetTypeKey': 'power.zone'},
# 'targetValueOne': 95, 'targetValueTwo': 105},
# {'type': 'ExecutableStepDTO', 'stepOrder': 2,
# 'stepType': {'stepTypeId': 3, 'stepTypeKey': 'interval'}, 'childStepId': None,
# 'endCondition': {'conditionTypeId': 2, 'conditionTypeKey': 'time'}, 'endConditionValue': 120,
# 'targetType': {'workoutTargetTypeId': 2, 'workoutTargetTypeKey': 'power.zone'},
# 'targetValueOne': 114, 'targetValueTwo': 126}
# ]
# }
# ]
# }
# """
menu_options = {
"1": "Get full name",
"2": "Get unit system",
"3": f"Get activity data for '{today.isoformat()}'",
"4": f"Get activity data for '{today.isoformat()}' (compatible with garminconnect-ha)",
"5": f"Get body composition data for '{today.isoformat()}' (compatible with garminconnect-ha)",
"6": f"Get body composition data for from '{startdate.isoformat()}' to '{today.isoformat()}' (to be compatible with garminconnect-ha)",
"7": f"Get stats and body composition data for '{today.isoformat()}'",
"8": f"Get steps data for '{today.isoformat()}'",
"9": f"Get heart rate data for '{today.isoformat()}'",
"0": f"Get training readiness data for '{today.isoformat()}'",
"-": f"Get daily step data for '{startdate.isoformat()}' to '{today.isoformat()}'",
"/": f"Get body battery data for '{startdate.isoformat()}' to '{today.isoformat()}'",
"!": f"Get floors data for '{startdate.isoformat()}'",
"?": f"Get blood pressure data for '{startdate.isoformat()}' to '{today.isoformat()}'",
".": f"Get training status data for '{today.isoformat()}'",
"a": f"Get resting heart rate data for {today.isoformat()}'",
"b": f"Get hydration data for '{today.isoformat()}'",
"c": f"Get sleep data for '{today.isoformat()}'",
"d": f"Get stress data for '{today.isoformat()}'",
"e": f"Get respiration data for '{today.isoformat()}'",
"f": f"Get SpO2 data for '{today.isoformat()}'",
"g": f"Get max metric data (like vo2MaxValue and fitnessAge) for '{today.isoformat()}'",
"h": "Get personal record for user",
"i": "Get earned badges for user",
"j": f"Get adhoc challenges data from start '{start}' and limit '{limit}'",
"k": f"Get available badge challenges data from '{start_badge}' and limit '{limit}'",
"l": f"Get badge challenges data from '{start_badge}' and limit '{limit}'",
"m": f"Get non completed badge challenges data from '{start_badge}' and limit '{limit}'",
"n": f"Get activities data from start '{start}' and limit '{limit}'",
"o": "Get last activity",
"p": f"Download activities data by date from '{startdate.isoformat()}' to '{today.isoformat()}'",
"r": f"Get all kinds of activities data from '{start}'",
"s": f"Upload activity data from file '{activityfile}'",
"t": "Get all kinds of Garmin device info",
"u": "Get active goals",
"v": "Get future goals",
"w": "Get past goals",
"y": "Get all Garmin device alarms",
"x": f"Get Heart Rate Variability data (HRV) for '{today.isoformat()}'",
"z": f"Get progress summary from '{startdate.isoformat()}' to '{today.isoformat()}' for all metrics",
"A": "Get gear, the defaults, activity types and statistics",
"B": f"Get weight-ins from '{startdate.isoformat()}' to '{today.isoformat()}'",
"C": f"Get daily weigh-ins for '{today.isoformat()}'",
"D": f"Delete all weigh-ins for '{today.isoformat()}'",
"E": f"Add a weigh-in of {weight}{weightunit} on '{today.isoformat()}'",
"F": f"Get virtual challenges/expeditions from '{startdate.isoformat()}' to '{today.isoformat()}'",
"G": f"Get hill score data from '{startdate.isoformat()}' to '{today.isoformat()}'",
"H": f"Get endurance score data from '{startdate.isoformat()}' to '{today.isoformat()}'",
"I": f"Get activities for date '{today.isoformat()}'",
"J": "Get race predictions",
"K": f"Get all day stress data for '{today.isoformat()}'",
"L": f"Add body composition for '{today.isoformat()}'",
"M": "Set blood pressure '120,80,80,notes='Testing with example.py'",
"N": "Get user profile/settings",
"O": f"Reload epoch data for {today.isoformat()}",
"P": "Get workouts 0-100, get and download last one to .FIT file",
# "Q": "Upload workout from json data",
"R": "Get solar data from your devices",
"S": "Get pregnancy summary data",
"T": "Add hydration data",
"U": f"Get Fitness Age data for {today.isoformat()}",
"V": f"Get daily wellness events data for {startdate.isoformat()}",
"W": "Get userprofile settings",
"Z": "Remove stored login tokens (logout)",
"q": "Exit",
}
def display_json(api_call, output):
"""Format API output for better readability."""
dashed = "-" * 20
header = f"{dashed} {api_call} {dashed}"
footer = "-" * len(header)
print(header)
if isinstance(output, (int, str, dict, list)):
print(json.dumps(output, indent=4))
else:
print(output)
print(footer)
def display_text(output):
"""Format API output for better readability."""
dashed = "-" * 60
header = f"{dashed}"
footer = "-" * len(header)
print(header)
print(json.dumps(output, indent=4))
print(footer)
def get_credentials():
"""Get user credentials."""
email = input("Login e-mail: ")
password = getpass("Enter password: ")
return email, password
def init_api(email, password):
"""Initialize Garmin API with your credentials."""
try:
# Using Oauth1 and OAuth2 token files from directory
print(
f"Trying to login to Garmin Connect using token data from directory '{tokenstore}'...\n"
)
# Using Oauth1 and Oauth2 tokens from base64 encoded string
# print(
# f"Trying to login to Garmin Connect using token data from file '{tokenstore_base64}'...\n"
# )
# dir_path = os.path.expanduser(tokenstore_base64)
# with open(dir_path, "r") as token_file:
# tokenstore = token_file.read()
garmin = Garmin()
garmin.login(tokenstore)
except (FileNotFoundError, GarthHTTPError, GarminConnectAuthenticationError):
# Session is expired. You'll need to log in again
print(
"Login tokens not present, login with your Garmin Connect credentials to generate them.\n"
f"They will be stored in '{tokenstore}' for future use.\n"
)
try:
# Ask for credentials if not set as environment variables
if not email or not password:
email, password = get_credentials()
garmin = Garmin(
email=email, password=password, is_cn=False, prompt_mfa=get_mfa
)
garmin.login()
# Save Oauth1 and Oauth2 token files to directory for next login
garmin.garth.dump(tokenstore)
print(
f"Oauth tokens stored in '{tokenstore}' directory for future use. (first method)\n"
)
# Encode Oauth1 and Oauth2 tokens to base64 string and safe to file for next login (alternative way)
token_base64 = garmin.garth.dumps()
dir_path = os.path.expanduser(tokenstore_base64)
with open(dir_path, "w") as token_file:
token_file.write(token_base64)
print(
f"Oauth tokens encoded as base64 string and saved to '{dir_path}' file for future use. (second method)\n"
)
except (
FileNotFoundError,
GarthHTTPError,
GarminConnectAuthenticationError,
requests.exceptions.HTTPError,
) as err:
logger.error(err)
return None
return garmin
def get_mfa():
"""Get MFA."""
return input("MFA one-time code: ")
def print_menu():
"""Print examples menu."""
for key in menu_options.keys():
print(f"{key} -- {menu_options[key]}")
print("Make your selection: ", end="", flush=True)
def switch(api, i):
"""Run selected API call."""
# Exit example program
if i == "q":
print("Be active, generate some data to fetch next time ;-) Bye!")
sys.exit()
# Skip requests if login failed
if api:
try:
print(f"\n\nExecuting: {menu_options[i]}\n")
# USER BASICS
if i == "1":
# Get full name from profile
display_json("api.get_full_name()", api.get_full_name())
elif i == "2":
# Get unit system from profile
display_json("api.get_unit_system()", api.get_unit_system())
# USER STATISTIC SUMMARIES
elif i == "3":
# Get activity data for 'YYYY-MM-DD'
display_json(
f"api.get_stats('{today.isoformat()}')",
api.get_stats(today.isoformat()),
)
elif i == "4":
# Get activity data (to be compatible with garminconnect-ha)
display_json(
f"api.get_user_summary('{today.isoformat()}')",
api.get_user_summary(today.isoformat()),
)
elif i == "5":
# Get body composition data for 'YYYY-MM-DD' (to be compatible with garminconnect-ha)
display_json(
f"api.get_body_composition('{today.isoformat()}')",
api.get_body_composition(today.isoformat()),
)
elif i == "6":
# Get body composition data for multiple days 'YYYY-MM-DD' (to be compatible with garminconnect-ha)
display_json(
f"api.get_body_composition('{startdate.isoformat()}', '{today.isoformat()}')",
api.get_body_composition(startdate.isoformat(), today.isoformat()),
)
elif i == "7":
# Get stats and body composition data for 'YYYY-MM-DD'
display_json(
f"api.get_stats_and_body('{today.isoformat()}')",
api.get_stats_and_body(today.isoformat()),
)
# USER STATISTICS LOGGED
elif i == "8":
# Get steps data for 'YYYY-MM-DD'
display_json(
f"api.get_steps_data('{today.isoformat()}')",
api.get_steps_data(today.isoformat()),
)
elif i == "9":
# Get heart rate data for 'YYYY-MM-DD'
display_json(
f"api.get_heart_rates('{today.isoformat()}')",
api.get_heart_rates(today.isoformat()),
)
elif i == "0":
# Get training readiness data for 'YYYY-MM-DD'
display_json(
f"api.get_training_readiness('{today.isoformat()}')",
api.get_training_readiness(today.isoformat()),
)
elif i == "/":
# Get daily body battery data for 'YYYY-MM-DD' to 'YYYY-MM-DD'
display_json(
f"api.get_body_battery('{startdate.isoformat()}, {today.isoformat()}')",
api.get_body_battery(startdate.isoformat(), today.isoformat()),
)
# Get daily body battery event data for 'YYYY-MM-DD'
display_json(
f"api.get_body_battery_events('{startdate.isoformat()}, {today.isoformat()}')",
api.get_body_battery_events(startdate.isoformat()),
)
elif i == "?":
# Get daily blood pressure data for 'YYYY-MM-DD' to 'YYYY-MM-DD'
display_json(
f"api.get_blood_pressure('{startdate.isoformat()}, {today.isoformat()}')",
api.get_blood_pressure(startdate.isoformat(), today.isoformat()),
)
elif i == "-":
# Get daily step data for 'YYYY-MM-DD'
display_json(
f"api.get_daily_steps('{startdate.isoformat()}, {today.isoformat()}')",
api.get_daily_steps(startdate.isoformat(), today.isoformat()),
)
elif i == "!":
# Get daily floors data for 'YYYY-MM-DD'
display_json(
f"api.get_floors('{today.isoformat()}')",
api.get_floors(today.isoformat()),
)
elif i == ".":
# Get training status data for 'YYYY-MM-DD'
display_json(
f"api.get_training_status('{today.isoformat()}')",
api.get_training_status(today.isoformat()),
)
elif i == "a":
# Get resting heart rate data for 'YYYY-MM-DD'
display_json(
f"api.get_rhr_day('{today.isoformat()}')",
api.get_rhr_day(today.isoformat()),
)
elif i == "b":
# Get hydration data 'YYYY-MM-DD'
display_json(
f"api.get_hydration_data('{today.isoformat()}')",
api.get_hydration_data(today.isoformat()),
)
elif i == "c":
# Get sleep data for 'YYYY-MM-DD'
display_json(
f"api.get_sleep_data('{today.isoformat()}')",
api.get_sleep_data(today.isoformat()),
)
elif i == "d":
# Get stress data for 'YYYY-MM-DD'
display_json(
f"api.get_stress_data('{today.isoformat()}')",
api.get_stress_data(today.isoformat()),
)
elif i == "e":
# Get respiration data for 'YYYY-MM-DD'
display_json(
f"api.get_respiration_data('{today.isoformat()}')",
api.get_respiration_data(today.isoformat()),
)
elif i == "f":
# Get SpO2 data for 'YYYY-MM-DD'
display_json(
f"api.get_spo2_data('{today.isoformat()}')",
api.get_spo2_data(today.isoformat()),
)
elif i == "g":
# Get max metric data (like vo2MaxValue and fitnessAge) for 'YYYY-MM-DD'
display_json(
f"api.get_max_metrics('{today.isoformat()}')",
api.get_max_metrics(today.isoformat()),
)
elif i == "h":
# Get personal record for user
display_json("api.get_personal_record()", api.get_personal_record())
elif i == "i":
# Get earned badges for user
display_json("api.get_earned_badges()", api.get_earned_badges())
elif i == "j":
# Get adhoc challenges data from start and limit
display_json(
f"api.get_adhoc_challenges({start},{limit})",
api.get_adhoc_challenges(start, limit),
) # 1=start, 100=limit
elif i == "k":
# Get available badge challenges data from start and limit
display_json(
f"api.get_available_badge_challenges({start_badge}, {limit})",
api.get_available_badge_challenges(start_badge, limit),
) # 1=start, 100=limit
elif i == "l":
# Get badge challenges data from start and limit
display_json(
f"api.get_badge_challenges({start_badge}, {limit})",
api.get_badge_challenges(start_badge, limit),
) # 1=start, 100=limit
elif i == "m":
# Get non completed badge challenges data from start and limit
display_json(
f"api.get_non_completed_badge_challenges({start_badge}, {limit})",
api.get_non_completed_badge_challenges(start_badge, limit),
) # 1=start, 100=limit
# ACTIVITIES
elif i == "n":
# Get activities data from start and limit
display_json(
f"api.get_activities({start}, {limit})",
api.get_activities(start, limit),
) # 0=start, 1=limit
elif i == "o":
# Get last activity
display_json("api.get_last_activity()", api.get_last_activity())
elif i == "p":
# Get activities data from startdate 'YYYY-MM-DD' to enddate 'YYYY-MM-DD', with (optional) activitytype
# Possible values are: cycling, running, swimming, multi_sport, fitness_equipment, hiking, walking, other
activities = api.get_activities_by_date(
startdate.isoformat(), today.isoformat(), activitytype
)
# Download activities
for activity in activities:
activity_start_time = datetime.datetime.strptime(
activity["startTimeLocal"], "%Y-%m-%d %H:%M:%S"
).strftime(
"%d-%m-%Y"
) # Format as DD-MM-YYYY, for creating unique activity names for scraping
activity_id = activity["activityId"]
activity_name = activity["activityName"]
display_text(activity)
print(
f"api.download_activity({activity_id}, dl_fmt=api.ActivityDownloadFormat.GPX)"
)
gpx_data = api.download_activity(
activity_id, dl_fmt=api.ActivityDownloadFormat.GPX
)
output_file = f"./{str(activity_name)}_{str(activity_start_time)}_{str(activity_id)}.gpx"
with open(output_file, "wb") as fb:
fb.write(gpx_data)
print(f"Activity data downloaded to file {output_file}")
print(
f"api.download_activity({activity_id}, dl_fmt=api.ActivityDownloadFormat.TCX)"
)
tcx_data = api.download_activity(
activity_id, dl_fmt=api.ActivityDownloadFormat.TCX
)
output_file = f"./{str(activity_name)}_{str(activity_start_time)}_{str(activity_id)}.tcx"
with open(output_file, "wb") as fb:
fb.write(tcx_data)
print(f"Activity data downloaded to file {output_file}")
print(
f"api.download_activity({activity_id}, dl_fmt=api.ActivityDownloadFormat.ORIGINAL)"
)
zip_data = api.download_activity(
activity_id, dl_fmt=api.ActivityDownloadFormat.ORIGINAL
)
output_file = f"./{str(activity_name)}_{str(activity_start_time)}_{str(activity_id)}.zip"
with open(output_file, "wb") as fb:
fb.write(zip_data)
print(f"Activity data downloaded to file {output_file}")
print(
f"api.download_activity({activity_id}, dl_fmt=api.ActivityDownloadFormat.CSV)"
)
csv_data = api.download_activity(
activity_id, dl_fmt=api.ActivityDownloadFormat.CSV
)
output_file = f"./{str(activity_name)}_{str(activity_start_time)}_{str(activity_id)}.csv"
with open(output_file, "wb") as fb:
fb.write(csv_data)
print(f"Activity data downloaded to file {output_file}")
elif i == "r":
# Get activities data from start and limit
activities = api.get_activities(start, limit) # 0=start, 1=limit
# Get activity splits
first_activity_id = activities[0].get("activityId")
display_json(
f"api.get_activity_splits({first_activity_id})",
api.get_activity_splits(first_activity_id),
)
# Get activity typed splits
display_json(
f"api.get_activity_typed_splits({first_activity_id})",
api.get_activity_typed_splits(first_activity_id),
)
# Get activity split summaries for activity id
display_json(
f"api.get_activity_split_summaries({first_activity_id})",
api.get_activity_split_summaries(first_activity_id),
)
# Get activity weather data for activity
display_json(
f"api.get_activity_weather({first_activity_id})",
api.get_activity_weather(first_activity_id),
)
# Get activity hr timezones id
display_json(
f"api.get_activity_hr_in_timezones({first_activity_id})",
api.get_activity_hr_in_timezones(first_activity_id),
)
# Get activity details for activity id
display_json(
f"api.get_activity_details({first_activity_id})",
api.get_activity_details(first_activity_id),
)
# Get gear data for activity id
display_json(
f"api.get_activity_gear({first_activity_id})",
api.get_activity_gear(first_activity_id),
)
# Activity data for activity id
display_json(
f"api.get_activity({first_activity_id})",
api.get_activity(first_activity_id),
)
# Get exercise sets in case the activity is a strength_training
if activities[0]["activityType"]["typeKey"] == "strength_training":
display_json(
f"api.get_activity_exercise_sets({first_activity_id})",
api.get_activity_exercise_sets(first_activity_id),
)
elif i == "s":
try:
# Upload activity from file
display_json(
f"api.upload_activity({activityfile})",
api.upload_activity(activityfile),
)
except FileNotFoundError:
print(f"File to upload not found: {activityfile}")
# DEVICES
elif i == "t":
# Get Garmin devices
devices = api.get_devices()
display_json("api.get_devices()", devices)
# Get device last used
device_last_used = api.get_device_last_used()
display_json("api.get_device_last_used()", device_last_used)
# Get settings per device
for device in devices:
device_id = device["deviceId"]
display_json(
f"api.get_device_settings({device_id})",
api.get_device_settings(device_id),
)
# Get primary training device information
primary_training_device = api.get_primary_training_device()
display_json(
"api.get_primary_training_device()", primary_training_device
)
elif i == "R":
# Get solar data from Garmin devices
devices = api.get_devices()
display_json("api.get_devices()", devices)
# Get device last used
device_last_used = api.get_device_last_used()
display_json("api.get_device_last_used()", device_last_used)
# Get settings per device
for device in devices:
device_id = device["deviceId"]
display_json(
f"api.get_device_solar_data({device_id}, {today.isoformat()})",
api.get_device_solar_data(device_id, today.isoformat()),
)
# GOALS
elif i == "u":
# Get active goals
goals = api.get_goals("active")
display_json('api.get_goals("active")', goals)
elif i == "v":
# Get future goals
goals = api.get_goals("future")
display_json('api.get_goals("future")', goals)
elif i == "w":
# Get past goals
goals = api.get_goals("past")
display_json('api.get_goals("past")', goals)
# ALARMS
elif i == "y":
# Get Garmin device alarms
alarms = api.get_device_alarms()
for alarm in alarms:
alarm_id = alarm["alarmId"]
display_json(f"api.get_device_alarms({alarm_id})", alarm)
elif i == "x":
# Get Heart Rate Variability (hrv) data
display_json(
f"api.get_hrv_data({today.isoformat()})",
api.get_hrv_data(today.isoformat()),
)
elif i == "z":
# Get progress summary
for metric in [
"elevationGain",
"duration",
"distance",
"movingDuration",
]:
display_json(
f"api.get_progress_summary_between_dates({today.isoformat()})",
api.get_progress_summary_between_dates(
startdate.isoformat(), today.isoformat(), metric
),
)
# GEAR
elif i == "A":
last_used_device = api.get_device_last_used()
display_json("api.get_device_last_used()", last_used_device)
userProfileNumber = last_used_device["userProfileNumber"]
gear = api.get_gear(userProfileNumber)
display_json("api.get_gear()", gear)
display_json(
"api.get_gear_defaults()", api.get_gear_defaults(userProfileNumber)
)
display_json("api.get()", api.get_activity_types())
for gear in gear:
uuid = gear["uuid"]
name = gear["displayName"]
display_json(
f"api.get_gear_stats({uuid}) / {name}", api.get_gear_stats(uuid)
)
# WEIGHT-INS
elif i == "B":
# Get weigh-ins data
display_json(
f"api.get_weigh_ins({startdate.isoformat()}, {today.isoformat()})",
api.get_weigh_ins(startdate.isoformat(), today.isoformat()),
)
elif i == "C":
# Get daily weigh-ins data
display_json(
f"api.get_daily_weigh_ins({today.isoformat()})",
api.get_daily_weigh_ins(today.isoformat()),
)
elif i == "D":
# Delete weigh-ins data for today
display_json(
f"api.delete_weigh_ins({today.isoformat()}, delete_all=True)",
api.delete_weigh_ins(today.isoformat(), delete_all=True),
)
elif i == "E":
# Add a weigh-in
display_json(
f"api.add_weigh_in(weight={weight}, unitKey={weightunit})",
api.add_weigh_in(weight=weight, unitKey=weightunit),
)
# Add a weigh-in with timestamps
yesterday = today - datetime.timedelta(days=1) # Get yesterday's date
weigh_in_date = datetime.datetime.strptime(yesterday.isoformat(), "%Y-%m-%d")
local_timestamp = weigh_in_date.strftime('%Y-%m-%dT%H:%M:%S')
gmt_timestamp = weigh_in_date.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S')
display_json(
f"api.add_weigh_in_with_timestamps(weight={weight}, unitKey={weightunit}, dateTimestamp={local_timestamp}, gmtTimestamp={gmt_timestamp})",
api.add_weigh_in_with_timestamps(
weight=weight,
unitKey=weightunit,
dateTimestamp=local_timestamp,
gmtTimestamp=gmt_timestamp
)
)
# CHALLENGES/EXPEDITIONS
elif i == "F":
# Get virtual challenges/expeditions
display_json(
f"api.get_inprogress_virtual_challenges({startdate.isoformat()}, {today.isoformat()})",
api.get_inprogress_virtual_challenges(
startdate.isoformat(), today.isoformat()
),
)
elif i == "G":
# Get hill score data
display_json(
f"api.get_hill_score({startdate.isoformat()}, {today.isoformat()})",
api.get_hill_score(startdate.isoformat(), today.isoformat()),
)
elif i == "H":
# Get endurance score data
display_json(
f"api.get_endurance_score({startdate.isoformat()}, {today.isoformat()})",
api.get_endurance_score(startdate.isoformat(), today.isoformat()),
)
elif i == "I":
# Get activities for date
display_json(
f"api.get_activities_fordate({today.isoformat()})",
api.get_activities_fordate(today.isoformat()),
)
elif i == "J":
# Get race predictions
display_json("api.get_race_predictions()", api.get_race_predictions())
elif i == "K":
# Get all day stress data for date
display_json(
f"api.get_all_day_stress({today.isoformat()})",
api.get_all_day_stress(today.isoformat()),
)
elif i == "L":
# Add body composition
weight = 70.0
percent_fat = 15.4
percent_hydration = 54.8
visceral_fat_mass = 10.8
bone_mass = 2.9
muscle_mass = 55.2
basal_met = 1454.1
active_met = None
physique_rating = None
metabolic_age = 33.0
visceral_fat_rating = None
bmi = 22.2
display_json(
f"api.add_body_composition({today.isoformat()}, {weight}, {percent_fat}, {percent_hydration}, {visceral_fat_mass}, {bone_mass}, {muscle_mass}, {basal_met}, {active_met}, {physique_rating}, {metabolic_age}, {visceral_fat_rating}, {bmi})",
api.add_body_composition(
today.isoformat(),
weight=weight,
percent_fat=percent_fat,
percent_hydration=percent_hydration,
visceral_fat_mass=visceral_fat_mass,
bone_mass=bone_mass,
muscle_mass=muscle_mass,
basal_met=basal_met,
active_met=active_met,
physique_rating=physique_rating,
metabolic_age=metabolic_age,
visceral_fat_rating=visceral_fat_rating,
bmi=bmi,
),
)
elif i == "M":
# Set blood pressure values
display_json(
"api.set_blood_pressure(120, 80, 80, notes=`Testing with example.py`)",
api.set_blood_pressure(
120, 80, 80, notes="Testing with example.py"
),
)
elif i == "N":
# Get user profile
display_json("api.get_user_profile()", api.get_user_profile())
elif i == "O":
# Reload epoch data for date
display_json(
f"api.request_reload({today.isoformat()})",
api.request_reload(today.isoformat()),
)
# WORKOUTS
elif i == "P":
workouts = api.get_workouts()
# Get workout 0-100
display_json("api.get_workouts()", api.get_workouts())
# Get last fetched workout
workout_id = workouts[-1]["workoutId"]
workout_name = workouts[-1]["workoutName"]
display_json(
f"api.get_workout_by_id({workout_id})",
api.get_workout_by_id(workout_id),
)
# Download last fetched workout
print(f"api.download_workout({workout_id})")
workout_data = api.download_workout(workout_id)
output_file = f"./{str(workout_name)}.fit"
with open(output_file, "wb") as fb:
fb.write(workout_data)
print(f"Workout data downloaded to file {output_file}")
# elif i == "Q":
# display_json(
# f"api.upload_workout({workout_example})",
# api.upload_workout(workout_example))
# DAILY EVENTS
elif i == "V":
# Get all day wellness events for 7 days ago
display_json(
f"api.get_all_day_events({today.isoformat()})",
api.get_all_day_events(startdate.isoformat()),
)
# WOMEN'S HEALTH
elif i == "S":
# Get pregnancy summary data
display_json("api.get_pregnancy_summary()", api.get_pregnancy_summary())
# Additional related calls:
# get_menstrual_data_for_date(self, fordate: str): takes a single date and returns the Garmin Menstrual Summary data for that date
# get_menstrual_calendar_data(self, startdate: str, enddate: str) takes two dates and returns summaries of cycles that have days between the two days
elif i == "T":
# Add hydration data for today
value_in_ml = 240
raw_date = datetime.date.today()
cdate = str(raw_date)
raw_ts = datetime.datetime.now()
timestamp = datetime.datetime.strftime(raw_ts, "%Y-%m-%dT%H:%M:%S.%f")
display_json(
f"api.add_hydration_data(value_in_ml={value_in_ml},cdate='{cdate}',timestamp='{timestamp}')",
api.add_hydration_data(
value_in_ml=value_in_ml, cdate=cdate, timestamp=timestamp
),
)
elif i == "U":
# Get fitness age data
display_json(
f"api.get_fitnessage_data({today.isoformat()})",
api.get_fitnessage_data(today.isoformat()),
)
elif i == "W":
# Get userprofile settings
display_json(
"api.get_userprofile_settings()", api.get_userprofile_settings()
)
elif i == "Z":
# Remove stored login tokens for Garmin Connect portal
tokendir = os.path.expanduser(tokenstore)
print(f"Removing stored login tokens from: {tokendir}")
try:
for root, dirs, files in os.walk(tokendir, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
print(f"Directory {tokendir} removed")
except FileNotFoundError:
print(f"Directory not found: {tokendir}")
api = None
except (
GarminConnectConnectionError,
GarminConnectAuthenticationError,
GarminConnectTooManyRequestsError,
requests.exceptions.HTTPError,
GarthHTTPError,
) as err:
logger.error(err)
except KeyError:
# Invalid menu option chosen
pass
else:
print("Could not login to Garmin Connect, try again later.")
# Main program loop
while True:
# Display header and login
print("\n*** Garmin Connect API Demo by cyberjunky ***\n")
# Init API
if not api:
api = init_api(email, password)
if api:
# Display menu
print_menu()
option = readchar.readkey()
switch(api, option)
else:
api = init_api(email, password)