pe-st / garmin-connect-export

Download a copy of your Garmin Connect data, including stats and GPX tracks.
MIT License
376 stars 77 forks source link

Request: Download all files associated with an activity in a single call of gcexport.py #97

Open sgowtham opened 1 year ago

sgowtham commented 1 year ago

Greetings.

I am wondering if it's possible to download all files associated with an activity (e.g., csv, fit, gpx, hrz, json, kml and tcx) from a single call of gcexport.py. Workflow would be something like defining a list within the export_data_file function

formatList = ['csv', 'gpx', 'kml', 'tcx', 'original', 'hrz']

and loop through formatList along the lines of

    fit_filename = None
    for formatType in formatList:
      if formatType == 'csv':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.csv')
        download_url  = URL_GC_CSV_ACTIVITY + activity_id + '?full=true'
        file_mode     = 'w'
      elif formatType == 'gpx':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.gpx')
        download_url  = URL_GC_GPX_ACTIVITY + activity_id + '?full=true'
        file_mode     = 'w'
      elif formatType == 'hrz':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.hrz')
        download_url  = URL_GC_ACTIVITY + activity_id + '/hrTimeInZones'
        file_mode     = 'w'
      elif formatType == 'kml':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.kml')
        download_url  = URL_GC_KML_ACTIVITY + activity_id + '?full=true'
        file_mode     = 'w'
      elif formatType == 'tcx':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.tcx')
        download_url  = URL_GC_TCX_ACTIVITY + activity_id + '?full=true'
        file_mode     = 'w'
      elif formatType == 'original':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.zip')
        # TODO not all 'original' files are in FIT format, some are GPX or TCX.
        # Fri, 13 Nov 2020 07:29:13 -0500
        # https://github.com/pe-st/garmin-connect-export/commit/4b33c6cfbe5a50278e306c322b0297f4edd945e5
        # fit_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.fit')
        fit_filename = os.path.join(directory, prefix + 'activity_' + activity_id + '_ACTIVITY' + append_desc + '.fit')
        logging.debug('fitfilename: %s', fit_filename)
        download_url = URL_GC_FIT_ACTIVITY + activity_id
        file_mode    = 'wb'
      elif formatType == 'json':
        data_filename = os.path.join(directory, prefix + 'activity_' + activity_id + append_desc + '.json')
        file_mode     = 'w'
      else:
        raise Exception('Unrecognized format.')

and the variables below are defined to be accessible by all functions in gcexport.py.

URL_GC_CSV_ACTIVITY = 'https://connect.garmin.com/modern/proxy/download-service/export/csv/activity/'
URL_GC_GPX_ACTIVITY = 'https://connect.garmin.com/modern/proxy/download-service/export/gpx/activity/'
URL_GC_KML_ACTIVITY = 'https://connect.garmin.com/modern/proxy/download-service/export/kml/activity/'
URL_GC_TCX_ACTIVITY = 'https://connect.garmin.com/modern/proxy/download-service/export/tcx/activity/'
URL_GC_FIT_ACTIVITY = 'http://connect.garmin.com/proxy/download-service/files/activity/'

As far as checking duplicates is concerned, the workflow could check for csv existence. If it exists, move on to the next activity. If it doesn't download csv and others.

Thoughts? I will gladly help with testing and such.

My best, Gowtham (G)

pe-st commented 1 year ago

Good idea, I'll think about it. BTW I didn't know you could export KML and CSV... ;-)

Why would you need a separate download for HRZ, they should already be in the JSON files and can be configured to be written to the CSV file

sgowtham commented 1 year ago

@pe-st Thank you :)

I am ok with not doing a separate call for HRZ info. I can parse the JSON and read it from there. Current set up (I believe you had helped a while ago when I first requested downloading HRZ info for each activity) on my local end is to keep the .hrz file for symmetry reasons.