Skip to content

Commit

Permalink
Merge branch 'pe-st-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cristian5th committed Mar 5, 2024
2 parents a4d22ab + cf7d107 commit 98883be
Showing 1 changed file with 93 additions and 30 deletions.
123 changes: 93 additions & 30 deletions gcexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,36 +812,39 @@ def export_data_file(activity_id, activity_details, args, file_time, append_desc
if args.unzip and data_filename[-3:].lower() == 'zip':
logging.debug('Unzipping and removing original file, size is %s', os.stat(data_filename).st_size)
if os.stat(data_filename).st_size > 0:
with open(data_filename, 'rb') as zip_file, zipfile.ZipFile(zip_file) as zip_obj:
for name in zip_obj.namelist():
unzipped_name = zip_obj.extract(name, directory)
# prepend 'activity_' and append the description to the base name
name_base, name_ext = os.path.splitext(name)
# sometimes in 2020 Garmin added '_ACTIVITY' to the name in the ZIP. Remove it...
# note that 'new_name' should match 'original_basename' elsewhere in this script to
# avoid downloading the same files again
name_base = name_base.replace('_ACTIVITY', '')
new_name = os.path.join(directory, f'{prefix}activity_{name_base}{append_desc}{name_ext}')
logging.debug('renaming %s to %s', unzipped_name, new_name)
os.rename(unzipped_name, new_name)
if file_time:
os.utime(new_name, (file_time, file_time))
zip_file = open(data_filename, 'rb')
zip_obj = zipfile.ZipFile(zip_file)
for name in zip_obj.namelist():
unzipped_name = zip_obj.extract(name, directory)
# prepend 'activity_' and append the description to the base name
name_base, name_ext = os.path.splitext(name)
# sometimes in 2020 Garmin added '_ACTIVITY' to the name in the ZIP. Remove it...
# note that 'new_name' should match 'original_basename' elsewhere in this script to
# avoid downloading the same files again
# name_base = name_base.replace('_ACTIVITY', '')
new_name = original_basename + name_ext
logging.debug('renaming %s to %s', unzipped_name, new_name)
os.rename(unzipped_name, new_name)
if file_time:
os.utime(new_name, (file_time, file_time))
zip_file.close()
else:
print('\tSkipping 0Kb zip file.')
os.remove(data_filename)

# Inform the main program that the file is new
return True


def setup_logging(args):
"""Setup logging"""
logpath = args.logpath if args.logpath else args.directory
if not os.path.isdir(logpath):
os.makedirs(logpath)

logging.basicConfig(
filename=os.path.join(logpath, 'gcexport.log'), level=logging.DEBUG, format='%(asctime)s [%(levelname)-7.7s] %(message)s'
filename = os.path.join(args.directory, 'gcexport.log'),
level=logging.DEBUG,
format='%(asctime)s [%(levelname)-7.7s] %(message)s'
)

# set up logging to console
Expand Down Expand Up @@ -1072,24 +1075,84 @@ def copy_details_to_summary(summary, details):
summary['maxHR'] = details['summaryDTO']['maxHR'] if 'summaryDTO' in details and 'maxHR' in details['summaryDTO'] else None
summary['averageHR'] = details['summaryDTO']['averageHR'] if 'summaryDTO' in details and 'averageHR' in details['summaryDTO'] else None
summary['elevationCorrected'] = details['metadataDTO']['elevationCorrected'] if 'metadataDTO' in details and 'elevationCorrected' in details['metadataDTO'] else None
# fmt: on


def process_activity_item(item, number_of_items, device_dict, activity_type_name, event_type_name, csv_filter, args):
def main(argv):
"""
Process one activity item: download the data, parse it and write a line to the CSV file
:param item: activity item tuple, see `annotate_activity_list()`
:param number_of_items: total number of items (for progress output)
:param device_dict: cache (dict) of already known devices
:param activity_type_name: lookup table for activity type descriptions
:param event_type_name: lookup table for event type descriptions
:param csv_filter: object encapsulating CSV file access
:param args: command-line arguments
Main entry point for gcexport.py
"""
current_index = item['index'] + 1
actvty = item['activity']
action = item['action']
args = parse_arguments(argv)
setup_logging(args)
logging.info("Starting %s version %s, using Python version %s", argv[0], SCRIPT_VERSION, python_version())
logging_verbosity(args.verbosity)

print('Welcome to Garmin Connect Exporter!')

if not python3:
print('Please upgrade to Python 3.x, version', python_version(), 'isn\'t supported anymore, see https://github.com/pe-st/garmin-connect-export/issues/64')
sys.exit(1)

# Get filter list with IDs to exclude
if args.exclude is not None:
exclude_list = read_exclude(args.exclude)
if exclude_list is None:
sys.exit(1)
else:
exclude_list = []

# Create directory for data files.
if os.path.isdir(args.directory):
logging.warning("Output directory %s already exists. "
"Will skip already-downloaded files and append to the CSV file.",
args.directory)
else:
os.mkdir(args.directory)

login_to_garmin_connect(args)

csv_filename = args.directory + '/activities.csv'
csv_existed = os.path.isfile(csv_filename)

if python3:
csv_file = open(csv_filename, mode='a', encoding='utf-8')
else:
csv_file = open(csv_filename, 'a')
csv_filter = CsvFilter(csv_file, args.template)

# Write header to CSV file
if not csv_existed:
csv_filter.write_header()

# Query the userstats (activities totals on the profile page). Needed for
# filtering and for downloading 'all' to know how many activities are available
userstats_json = fetch_userstats(args)

if args.count == 'all':
total_to_download = int(userstats_json['userMetrics'][0]['totalActivities'])
else:
total_to_download = int(args.count)

device_dict = dict()

# load some dictionaries with lookup data from REST services
activity_type_props = http_req_as_string(URL_GC_ACT_PROPS)
if args.verbosity > 0:
write_to_file(os.path.join(args.directory, 'activity_types.properties'), activity_type_props, 'w')
activity_type_name = load_properties(activity_type_props)
event_type_props = http_req_as_string(URL_GC_EVT_PROPS)
if args.verbosity > 0:
write_to_file(os.path.join(args.directory, 'event_types.properties'), activity_type_props, 'w')
event_type_name = load_properties(event_type_props)

activities = fetch_activity_list(args, total_to_download)
action_list = annotate_activity_list(activities, args.start_activity_no, exclude_list)

# Process each activity.
for item in action_list:

current_index = item['index'] + 1
actvty = item['activity']
action = item['action']

# Action: skipping
if action == 's':
Expand Down

0 comments on commit 98883be

Please sign in to comment.