def getallfiles(): # If the user wants to download all activities, query the userstats # on the profile page to know how many are available user_stats = gceaccess.query_garmin_stats() # Persist JSON gceutils.write_to_file(ARGS.directory + "/userstats.json", user_stats.decode(), "a") # Modify total_to_download based on how many activities the server reports. json_user = json.loads(user_stats) return int(json_user["userMetrics"][0]["totalActivities"])
def processactivity(alist): global TOTAL_SKIPPED, TOTAL_RETRIEVED for a in alist: # create a string from the activity to avoid having to use the str function multiple times. stractid = str(a["activityId"]) # Display which entry we're working on. print("Garmin Connect activity: [" + stractid + "] " + str(a["activityName"])) # download the file from Garmin download_url, file_mode, data_filename = downloadfile(stractid) # if the file already existed go get the next file if download_url == 1: TOTAL_SKIPPED += 1 continue # extract the data from the downloaded file data = gceaccess.download_data(download_url, ARGS.format) # if the original zip has no data if data == "": print("/tempty file, no data existed in the downloaded file") continue TOTAL_RETRIEVED += 1 # write the file gceutils.write_to_file(data_filename, gceutils.decoding_decider(ARGS.format, data), file_mode) log.debug("Activity summary URL: " + gceaccess.URL_GC_ACTIVITY + stractid) # get the summary info, if unavailable go get next file try: activity_summary = gceaccess.http_req(gceaccess.URL_GC_ACTIVITY + stractid) except Exception as aerror: print("unable to get activity " + str(aerror)) continue # write the summary file gceutils.write_to_file( ARGS.directory + sep + stractid + "_activity_summary.json", activity_summary.decode(), "a", ) # build the json format files json_summary, json_gear, json_device, json_detail = gceaccess.createjson( ARGS.directory, stractid, activity_summary) # CSV_FILE.write(csv_record) CSV_FILE.write( gceaccess.buildcsvrecord(a, json_summary, json_gear, json_device, json_detail)) friendly_filename = gceaccess.buildFriendlyFilename( a, json_summary, json_gear, json_device, json_detail, ARGS) finalizefiles(data, data_filename, friendly_filename)
def createjson(directory, stractId, actsum): json_summary = json.loads(actsum) log.debug(json_summary) log.debug( "Device detail URL: " + URL_DEVICE_DETAIL + str(json_summary["metadataDTO"]["deviceApplicationInstallationId"])) device_detail = http_req( URL_DEVICE_DETAIL + str(json_summary["metadataDTO"]["deviceApplicationInstallationId"])) if device_detail: gceutils.write_to_file( directory + "/" + stractId + "_app_info.json", device_detail.decode(), "a", ) json_device = json.loads(device_detail) log.debug(json_device) else: log.debug("Retrieving Device Details failed.") json_device = None log.debug("Activity details URL: " + URL_GC_ACTIVITY + stractId + "/details") try: activity_detail = http_req(URL_GC_ACTIVITY + stractId + "/details") gceutils.write_to_file( directory + "/" + stractId + "_activity_detail.json", activity_detail.decode(), "a", ) json_detail = json.loads(activity_detail) log.debug(json_detail) except Exception as error: print("Retrieving Activity Details failed. Reason: " + str(error)) json_detail = None log.debug("Gear details URL: " + URL_GEAR_DETAIL + "activityId=" + stractId) gear_detail = http_req(URL_GEAR_DETAIL + "activityId=" + stractId) try: gceutils.write_to_file( directory + "/" + stractId + "_gear_detail.json", gear_detail.decode(), "a", ) json_gear = json.loads(gear_detail) log.debug(json_gear) except Exception as error: print("Retrieving Gear Details failed. Error: " + str(error)) json_gear = None return json_summary, json_gear, json_device, json_detail
# 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 > gceaccess.LIMIT_MAXIMUM: NUM_TO_DOWNLOAD = gceaccess.LIMIT_MAXIMUM else: NUM_TO_DOWNLOAD = TOTAL_TO_DOWNLOAD - TOTAL_DOWNLOADED gceutils.printverbose(ARGS.verbose, "Number left to download = " + str(NUM_TO_DOWNLOAD)) search_parms = {"start": TOTAL_DOWNLOADED, "limit": NUM_TO_DOWNLOAD} log.debug("Search parms" + str(search_parms)) # Query Garmin Connect log.debug("Activity list URL: " + gceaccess.URL_GC_LIST + urllib.parse.urlencode(search_parms)) activity_list = gceaccess.http_req(gceaccess.URL_GC_LIST + urllib.parse.urlencode(search_parms)) gceutils.write_to_file(ARGS.directory + "/activity_list.json", activity_list.decode(), "a") processactivity(json.loads(activity_list)) TOTAL_DOWNLOADED += NUM_TO_DOWNLOAD # End while loop for multiple chunks. CSV_FILE.close() # delete the json and csv files before archiving. If requested if ARGS.delete is not None: print("deleting types " + str(ARGS.delete) + " from the output directory") gceutils.removefiles(ARGS.directory, ARGS.delete) # archive the downloaded files if ARGS.archive: print("archiving the downloaded files to: " + ARGS.archive)