forked from pe-st/garmin-connect-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcexport3.py
executable file
·515 lines (458 loc) · 23.3 KB
/
gcexport3.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
File: gcexport.py
Original author: Kyle Krafka (https://github.com/kjkjava/)
Date: April 28, 2015
Fork author: Michael P (https://github.com/moderation/)
Date: February 15, 2018
Description: Use this script to export your fitness data from Garmin Connect.
See README.md for more information.
Activity & event types:
https://connect.garmin.com/modern/main/js/properties/event_types/event_types.properties
https://connect.garmin.com/modern/main/js/properties/activity_types/activity_types.properties
"""
from datetime import datetime, timedelta
from getpass import getpass
from os import mkdir, remove, stat
from os.path import isdir, isfile
from subprocess import call
from sys import argv
from xml.dom.minidom import parseString
import argparse
import http.cookiejar
import json
import re
import urllib.error
import urllib.parse
import urllib.request
import zipfile
SCRIPT_VERSION = '2.0.0'
CURRENT_DATE = datetime.now().strftime('%Y-%m-%d')
ACTIVITIES_DIRECTORY = './' + CURRENT_DATE + '_garmin_connect_export'
PARSER = argparse.ArgumentParser()
# TODO: Implement verbose and/or quiet options.
# PARSER.add_argument('-v', '--verbose', help="increase output verbosity", action="store_true")
PARSER.add_argument('--version', help="print version and exit", action="store_true")
PARSER.add_argument('--username', help="your Garmin Connect username (otherwise, you will be \
prompted)", nargs='?')
PARSER.add_argument('--password', help="your Garmin Connect password (otherwise, you will be \
prompted)", nargs='?')
PARSER.add_argument('-c', '--count', nargs='?', default="1", help="number of recent activities to \
download, or 'all' (default: 1)")
PARSER.add_argument('-f', '--format', nargs='?', choices=['gpx', 'tcx', 'original'], \
default="gpx", help="export format; can be 'gpx', 'tcx', or 'original' (default: 'gpx')")
PARSER.add_argument('-d', '--directory', nargs='?', default=ACTIVITIES_DIRECTORY, help="the \
directory to export to (default: './YYYY-MM-DD_garmin_connect_export')")
PARSER.add_argument('-u', '--unzip', help="if downloading ZIP files (format: 'original'), unzip \
the file and removes the ZIP file", action="store_true")
ARGS = PARSER.parse_args()
if ARGS.version:
print(argv[0] + ", version " + SCRIPT_VERSION)
exit(0)
COOKIE_JAR = http.cookiejar.CookieJar()
OPENER = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(COOKIE_JAR))
# print(COOKIE_JAR)
def hhmmss_from_seconds(sec):
"""Helper function that converts seconds to HH:MM:SS time format."""
if isinstance(sec, (float)):
formatted_time = str(timedelta(seconds=int(sec))).zfill(8)
else:
formatted_time = "0.000"
return formatted_time
def kmh_from_mps(mps):
"""Helper function that converts meters per second (mps) to km/h."""
return str(mps * 3.6)
def write_to_file(filename, content, mode):
"""Helper function that persists content to file."""
write_file = open(filename, mode)
write_file.write(content)
write_file.close()
# url is a string, post is a dictionary of POST parameters, headers is a dictionary of headers.
def http_req(url, post=None, headers=None):
"""Helper function that makes the HTTP requests."""
request = urllib.request.Request(url)
# Tell Garmin we're some supported browser.
request.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, \
like Gecko) Chrome/54.0.2816.0 Safari/537.36')
if headers:
for header_key, header_value in headers.items():
request.add_header(header_key, header_value)
if post:
post = urllib.parse.urlencode(post)
post = post.encode('utf-8') # Convert dictionary to POST parameter string.
# print("request.headers: " + str(request.headers) + " COOKIE_JAR: " + str(COOKIE_JAR))
# print("post: " + str(post) + "request: " + str(request))
response = OPENER.open((request), data=post)
if response.getcode() == 204:
# For activities without GPS coordinates, there is no GPX download (204 = no content).
# Write an empty file to prevent redownloading it.
print('Writing empty file since there was no GPX activity data...')
return ''
elif response.getcode() != 200:
raise Exception('Bad return code (' + str(response.getcode()) + ') for: ' + url)
# print(response.getcode())
return response.read()
print('Welcome to Garmin Connect Exporter!')
# Create directory for data files.
if isdir(ARGS.directory):
print('Warning: Output directory already exists. Will skip already-downloaded files and \
append to the CSV file.')
USERNAME = ARGS.username if ARGS.username else input('Username: ')
PASSWORD = ARGS.password if ARGS.password else getpass()
# Maximum number of activities you can request at once. Set and enforced by Garmin.
LIMIT_MAXIMUM = 1000
WEBHOST = "https://connect.garmin.com"
REDIRECT = "https://connect.garmin.com/post-auth/login"
BASE_URL = "http://connect.garmin.com/en-US/signin"
SSO = "https://sso.garmin.com/sso"
CSS = "https://static.garmincdn.com/com.garmin.connect/ui/css/gauth-custom-v1.2-min.css"
DATA = {
'service': REDIRECT,
'webhost': WEBHOST,
'source': BASE_URL,
'redirectAfterAccountLoginUrl': REDIRECT,
'redirectAfterAccountCreationUrl': REDIRECT,
'gauthHost': SSO,
'locale': 'en_US',
'id': 'gauth-widget',
'cssUrl': CSS,
'clientId': 'GarminConnect',
'rememberMeShown': 'true',
'rememberMeChecked': 'false',
'createAccountShown': 'true',
'openCreateAccount': 'false',
'usernameShown': 'false',
'displayNameShown': 'false',
'consumeServiceTicket': 'false',
'initialFocus': 'true',
'embedWidget': 'false',
'generateExtraServiceTicket': 'false'
}
print(urllib.parse.urlencode(DATA))
# URLs for various services.
URL_GC_LOGIN = 'https://sso.garmin.com/sso/login?' + urllib.parse.urlencode(DATA)
URL_GC_POST_AUTH = 'https://connect.garmin.com/modern/activities?'
URL_GC_SEARCH = 'https://connect.garmin.com/proxy/activity-search-service-1.2/json/activities?'
URL_GC_LIST = \
'https://connect.garmin.com/modern/proxy/activitylist-service/activities/search/activities?'
URL_GC_ACTIVITY = 'https://connect.garmin.com/modern/proxy/activity-service/activity/'
URL_GC_ACTIVITY_DETAIL = \
'https://connect.garmin.com/modern/proxy/activity-service-1.3/json/activityDetails/'
URL_GC_GPX_ACTIVITY = \
'https://connect.garmin.com/modern/proxy/download-service/export/gpx/activity/'
URL_GC_TCX_ACTIVITY = \
'https://connect.garmin.com/modern/proxy/download-service/export/tcx/activity/'
URL_GC_ORIGINAL_ACTIVITY = 'http://connect.garmin.com/proxy/download-service/files/activity/'
# Initially, we need to get a valid session cookie, so we pull the login page.
print('Request login page')
http_req(URL_GC_LOGIN)
print('Finish login page')
# Now we'll actually login.
# Fields that are passed in a typical Garmin login.
POST_DATA = {
'username': USERNAME,
'password': PASSWORD,
'embed': 'true',
'lt': 'e1s1',
'_eventId': 'submit',
'displayNameRequired': 'false'
}
print('Post login data')
LOGIN_RESPONSE = http_req(URL_GC_LOGIN, POST_DATA).decode()
print('Finish login post')
# extract the ticket from the login response
PATTERN = re.compile(r".*\?ticket=([-\w]+)\";.*", re.MULTILINE|re.DOTALL)
MATCH = PATTERN.match(LOGIN_RESPONSE)
if not MATCH:
raise Exception('Did not get a ticket in the login response. Cannot log in. Did \
you enter the correct username and password?')
LOGIN_TICKET = MATCH.group(1)
print('login ticket=' + LOGIN_TICKET)
print("Request authentication URL: " + URL_GC_POST_AUTH + 'ticket=' + LOGIN_TICKET)
http_req(URL_GC_POST_AUTH + 'ticket=' + LOGIN_TICKET)
print('Finished authentication')
# We should be logged in now.
if not isdir(ARGS.directory):
mkdir(ARGS.directory)
CSV_FILENAME = ARGS.directory + '/activities.csv'
CSV_EXISTED = isfile(CSV_FILENAME)
CSV_FILE = open(CSV_FILENAME, 'a')
# Write header to CSV file
if not CSV_EXISTED:
CSV_FILE.write('Activity name,\
Description,\
Begin timestamp,\
Duration (h:m:s),\
Moving duration (h:m:s),\
Distance (km),\
Average speed (km/h),\
Average moving speed (km/h),\
Max. speed (km/h),\
Elevation loss uncorrected (m),\
Elevation gain uncorrected (m),\
Elevation min. uncorrected (m),\
Elevation max. uncorrected (m),\
Min. heart rate (bpm),\
Max. heart rate (bpm),\
Average heart rate (bpm),\
Calories,\
Avg. cadence (rpm),\
Max. cadence (rpm),\
Strokes,\
Avg. temp (°C),\
Min. temp (°C),\
Max. temp (°C),\
Map,\
End timestamp,\
Begin timestamp (ms),\
End timestamp (ms),\
Device,\
Activity type,\
Event type,\
Time zone,\
Begin latitude (°DD),\
Begin longitude (°DD),\
End latitude (°DD),\
End longitude (°DD),\
Elevation gain corrected (m),\
Elevation loss corrected (m),\
Elevation max. corrected (m),\
Elevation min. corrected (m),\
Sample count\n')
DOWNLOAD_ALL = False
if ARGS.count == 'all':
# If the user wants to download all activities, first download one,
# then the result of that request will tell us how many are available
# so we will modify the variables then.
TOTAL_TO_DOWNLOAD = 1
DOWNLOAD_ALL = True
else:
TOTAL_TO_DOWNLOAD = int(ARGS.count)
TOTAL_DOWNLOADED = 0
# This while loop will download data from the server in multiple chunks, if necessary.
while TOTAL_DOWNLOADED < TOTAL_TO_DOWNLOAD:
# Maximum chunk size 'limit_maximum' ... 400 return status if over maximum. So download
# maximum or whatever remains if less than maximum.
# As of 2018-03-06 I get return status 500 if over maximum
if TOTAL_TO_DOWNLOAD - TOTAL_DOWNLOADED > LIMIT_MAXIMUM:
NUM_TO_DOWNLOAD = LIMIT_MAXIMUM
else:
NUM_TO_DOWNLOAD = TOTAL_TO_DOWNLOAD - TOTAL_DOWNLOADED
SEARCH_PARAMS = {'start': TOTAL_DOWNLOADED, 'limit': NUM_TO_DOWNLOAD}
# Query Garmin Connect
print("Making activity request ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(URL_GC_SEARCH + urllib.parse.urlencode(SEARCH_PARAMS))
RESULT = http_req(URL_GC_SEARCH + urllib.parse.urlencode(SEARCH_PARAMS))
print("Finished activity request ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
# Persist JSON
write_to_file(ARGS.directory + '/activities.json', RESULT.decode(), 'a')
JSON_RESULTS = json.loads(RESULT) # TODO: Catch possible exceptions here.
if DOWNLOAD_ALL:
# Modify TOTAL_TO_DOWNLOAD based on how many activities the server reports.
TOTAL_TO_DOWNLOAD = int(JSON_RESULTS['results']['totalFound'])
# Do it only once.
DOWNLOAD_ALL = False
# Pull out just the list of activities.
ACTIVITIES = JSON_RESULTS['results']['activities']
# print(ACTIVITIES)
print("Activity list URL: " + URL_GC_LIST + urllib.parse.urlencode(SEARCH_PARAMS))
ACTIVITY_LIST = http_req(URL_GC_LIST + urllib.parse.urlencode(SEARCH_PARAMS))
write_to_file(ARGS.directory + '/activity_list.json', ACTIVITY_LIST.decode(), 'a')
# LIST = json.loads(ACTIVITY_LIST)
# print(LIST)
# Process each activity.
for a in ACTIVITIES:
# Display which entry we're working on.
print('Garmin Connect activity: [' + str(a['activity']['activityId']) + ']', end=' ')
print(a['activity']['activityName'])
print('\t' + a['activity']['uploadDate']['display'] + ',', end=' ')
if ARGS.format == 'gpx':
data_filename = ARGS.directory + '/' + str(a['activity']['activityId']) + \
'_activity.gpx'
download_url = URL_GC_GPX_ACTIVITY + str(a['activity']['activityId']) + '?full=true'
print(download_url)
file_mode = 'w'
elif ARGS.format == 'tcx':
data_filename = ARGS.directory + '/' + str(a['activity']['activityId']) + \
'_activity.tcx'
download_url = URL_GC_TCX_ACTIVITY + str(a['activity']['activityId']) + '?full=true'
file_mode = 'w'
elif ARGS.format == 'original':
data_filename = ARGS.directory + '/' + str(a['activity']['activityId']) + \
'_activity.zip'
fit_filename = ARGS.directory + '/' + str(a['activity']['activityId']) + '_activity.fit'
download_url = URL_GC_ORIGINAL_ACTIVITY + str(a['activity']['activityId'])
file_mode = 'wb'
else:
raise Exception('Unrecognized format.')
if isfile(data_filename):
print('\tData file already exists; skipping...')
continue
# Regardless of unzip setting, don't redownload if the ZIP or FIT file exists.
if ARGS.format == 'original' and isfile(fit_filename):
print('\tFIT data file already exists; skipping...')
continue
# Download the data file from Garmin Connect. If the download fails (e.g., due to timeout),
# this script will die, but nothing will have been written to disk about this activity, so
# just running it again should pick up where it left off.
print('\tDownloading file...', end=' ')
try:
data = http_req(download_url)
except urllib.error.HTTPError as errs:
# Handle expected (though unfortunate) error codes; die on unexpected ones.
if errs.code == 500 and ARGS.format == 'tcx':
# Garmin will give an internal server error (HTTP 500) when downloading TCX files
# if the original was a manual GPX upload. Writing an empty file prevents this file
# from being redownloaded, similar to the way GPX files are saved even when there
# are no tracks. One could be generated here, but that's a bit much. Use the GPX
# format if you want actual data in every file, as I believe Garmin provides a GPX
# file for every activity.
print('Writing empty file since Garmin did not generate a TCX file for this \
activity...', end=' ')
data = ''
elif errs.code == 404 and ARGS.format == 'original':
# For manual activities (i.e., entered in online without a file upload), there is
# no original file. # Write an empty file to prevent redownloading it.
print('Writing empty file since there was no original activity data...', end=' ')
data = ''
else:
raise Exception('Failed. Got an unexpected HTTP error (' + str(errs.code) + \
download_url +').')
# Persist file
write_to_file(data_filename, data.decode(), file_mode)
print("Activity summary URL: " + URL_GC_ACTIVITY + str(a['activity']['activityId']))
ACTIVITY_SUMMARY = http_req(URL_GC_ACTIVITY + str(a['activity']['activityId']))
write_to_file(ARGS.directory + '/' + str(a['activity']['activityId']) + \
'_activity_summary.json', ACTIVITY_SUMMARY.decode(), 'a')
JSON_SUMMARY = json.loads(ACTIVITY_SUMMARY)
# print(JSON_SUMMARY)
print("Activity details URL: " + URL_GC_ACTIVITY_DETAIL + str(a['activity']['activityId']))
ACTIVITY_DETAIL = http_req(URL_GC_ACTIVITY_DETAIL + str(a['activity']['activityId']))
write_to_file(ARGS.directory + '/' + str(a['activity']['activityId']) + \
'_activity_detail.json', ACTIVITY_DETAIL.decode(), 'a')
JSON_DETAIL = json.loads(ACTIVITY_DETAIL)
# print(JSON_DETAIL)
# Write stats to CSV.
empty_record = ','
csv_record = ''
csv_record += empty_record if 'activityName' not in a['activity'] else '"' + \
a['activity']['activityName'].replace('"', '""') + '",'
csv_record += empty_record if 'activityDescription' not in a['activity'] else '"' + \
a['activity']['activityDescription'].replace('"', '""') + '",'
csv_record += empty_record if 'startTimeLocal' not in JSON_SUMMARY['summaryDTO'] \
else '"' + JSON_SUMMARY['summaryDTO']['startTimeLocal'] + '",'
csv_record += empty_record if 'elapsedDuration' not in JSON_SUMMARY['summaryDTO'] \
else hhmmss_from_seconds(JSON_SUMMARY['summaryDTO']['elapsedDuration']) + ','
csv_record += empty_record if 'movingDuration' not in JSON_SUMMARY['summaryDTO'] \
else hhmmss_from_seconds(JSON_SUMMARY['summaryDTO']['movingDuration']) + ','
csv_record += empty_record if 'distance' not in JSON_SUMMARY['summaryDTO'] \
else "{0:.5f}".format(JSON_SUMMARY['summaryDTO']['distance']/1000) + ','
csv_record += empty_record if 'averageSpeed' not in JSON_SUMMARY['summaryDTO'] \
else kmh_from_mps(JSON_SUMMARY['summaryDTO']['averageSpeed']) + ','
csv_record += empty_record if 'averageMovingSpeed' not in JSON_SUMMARY['summaryDTO'] \
else kmh_from_mps(JSON_SUMMARY['summaryDTO']['averageMovingSpeed']) + ','
csv_record += empty_record if 'maxSpeed' not in JSON_SUMMARY['summaryDTO'] \
else kmh_from_mps(JSON_SUMMARY['summaryDTO']['maxSpeed']) + ','
csv_record += empty_record if 'elevationLoss' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['elevationLoss']) + ','
csv_record += empty_record if 'elevationGain' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['elevationGain']) + ','
csv_record += empty_record if 'minElevation' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['minElevation']) + ','
csv_record += empty_record if 'maxElevation' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['maxElevation']) + ','
csv_record += empty_record if 'minHR' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'maxHR' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['maxHR']) + ','
csv_record += empty_record if 'averageHR' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['averageHR']) + ','
csv_record += empty_record if 'calories' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['calories']) + ','
csv_record += empty_record if 'averageBikeCadence' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['averageBikeCadence']) + ','
csv_record += empty_record if 'maxBikeCadence' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['maxBikeCadence']) + ','
csv_record += empty_record if 'totalNumberOfStrokes' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['totalNumberOfStrokes']) + ','
csv_record += empty_record if 'averageTemperature' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['averageTemperature']) + ','
csv_record += empty_record if 'minTemperature' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['minTemperature']) + ','
csv_record += empty_record if 'maxTemperature' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['maxTemperature']) + ','
csv_record += empty_record if 'activityId' not in a['activity'] else \
'"https://connect.garmin.com/modern/activity/' + str(a['activity']['activityId']) + '",'
csv_record += empty_record if 'endTimestamp' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'beginTimestamp' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'endTimestamp' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'device' not in a['activity'] else \
a['activity']['device']['display'] + ' ' + a['activity']['device']['version'] + ','
csv_record += empty_record if 'activityType' not in a['activity'] else \
a['activity']['activityType']['display'] + ','
csv_record += empty_record if 'eventType' not in a['activity'] else \
a['activity']['eventType']['display'] + ','
csv_record += empty_record if 'activityTimeZone' not in a['activity'] else \
a['activity']['activityTimeZone']['display'] + ','
csv_record += empty_record if 'startLatitude' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['startLatitude']) + ','
csv_record += empty_record if 'startLongitude' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['startLongitude']) + ','
csv_record += empty_record if 'endLatitude' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['endLatitude']) + ','
csv_record += empty_record if 'endLongitude' not in JSON_SUMMARY['summaryDTO'] \
else str(JSON_SUMMARY['summaryDTO']['endLongitude']) + ','
csv_record += empty_record if 'gainCorrectedElevation' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'lossCorrectedElevation' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'maxCorrectedElevation' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'minCorrectedElevation' not in JSON_SUMMARY['summaryDTO'] \
else ',' # no longer available in JSON
csv_record += empty_record if 'metricsCount' not in \
JSON_DETAIL['com.garmin.activity.details.json.ActivityDetails'] else \
str(JSON_DETAIL['com.garmin.activity.details.json.ActivityDetails']['metricsCount']) \
+ ','
csv_record += '\n'
CSV_FILE.write(csv_record)
if ARGS.format == 'gpx' and data:
# Validate GPX data. If we have an activity without GPS data (e.g., running on a
# treadmill), Garmin Connect still kicks out a GPX (sometimes), but there is only
# activity information, no GPS data. N.B. You can omit the XML parse (and the
# associated log messages) to speed things up.
gpx = parseString(data)
if gpx.getElementsByTagName('trkpt'):
print('Done. GPX data saved.')
else:
print('Done. No track points found.')
elif ARGS.format == 'original':
# Even manual upload of a GPX file is zipped, but we'll validate the extension.
if ARGS.unzip and data_filename[-3:].lower() == 'zip':
print("Unzipping and removing original files...", end=' ')
print('Filesize is: ' + str(stat(data_filename).st_size))
if stat(data_filename).st_size > 0:
zip_file = open(data_filename, 'rb')
z = zipfile.ZipFile(zip_file)
for name in z.namelist():
z.extract(name, ARGS.directory)
zip_file.close()
else:
print('Skipping 0Kb zip file.')
remove(data_filename)
print('Done.')
else:
# TODO: Consider validating other formats.
print('Done.')
TOTAL_DOWNLOADED += NUM_TO_DOWNLOAD
# End while loop for multiple chunks.
CSV_FILE.close()
print('Open CSV output.')
print(CSV_FILENAME)
# open CSV file. Comment this line out if you don't want this behavior
call(["/usr/bin/libreoffice6.0", "--calc", CSV_FILENAME])
print('Done!')