-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_cloud_data.py
140 lines (112 loc) · 3.95 KB
/
get_cloud_data.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
import datetime
import json
import logging
import os
# import sys
import requests
# import pwinput
# import readchar
import zipfile
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("garmin_email")
password = os.getenv("garmin_password")
tokenstore = os.getenv("GARMINTOKENS") or "~/.garminconnect"
api = None
# Example selections and settings
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
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)
print(json.dumps(output, indent=4))
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():
global email, password
# """Get user credentials."""
# email = input("Login e-mail: ")
# password = pwinput.pwinput(prompt='Password: ')
return email, password
def init_api(email, password):
"""Initialize Garmin API with your credentials."""
try:
print(
f"Trying to login to Garmin Connect using token data from '{tokenstore}'...\n"
)
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, password)
garmin.login()
# Save tokens for next login
garmin.garth.dump(tokenstore)
except (FileNotFoundError, GarthHTTPError, GarminConnectAuthenticationError, requests.exceptions.HTTPError) as err:
logger.error(err)
return None
return garmin
def get_last_activity(api=None):
if not api:
api = init_api(email, password)
fit_file_name = '10241888920_ACTIVITY.fit'
# Get last activity
last_activity = api.get_last_activity()
return api, last_activity
def download_activity(api, activity_id):
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_id)}.zip"
with open(output_file, "wb") as fb:
fb.write(zip_data)
print(f"Activity data downloaded to file {output_file}")
with zipfile.ZipFile('./' + str(activity_id) + '.zip', 'r') as zip_ref:
zip_ref.extractall('./')
return str(activity_id) + '_ACTIVITY.fit'
# for i in range(10):
# # Init API
# if not api:
# api = init_api(email, password)
# else:
# break
#
# api, last_activity = get_last_activity(api)
# activity_id = last_activity["activityId"]
# fit_file_name = download_activity(api,activity_id)
# print(fit_file_name)