Simplify log statements.
Make use of format specifiers in the logging library rather than relying on string.format(), which makes log output unnecessarily verbose.
This commit is contained in:
parent
4ee0cda314
commit
c00ccd3887
@ -88,7 +88,7 @@ def _not_found_activities(backup_dir):
|
|||||||
if os.path.isfile(_not_found):
|
if os.path.isfile(_not_found):
|
||||||
with open(_not_found, mode="r") as f:
|
with open(_not_found, mode="r") as f:
|
||||||
failed_activities = [line.strip() for line in f.readlines()]
|
failed_activities = [line.strip() for line in f.readlines()]
|
||||||
log.debug("{} tried but failed activities in {}".format(len(failed_activities), _not_found))
|
log.debug("%d tried but failed activities in %s", len(failed_activities), _not_found)
|
||||||
return failed_activities
|
return failed_activities
|
||||||
|
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
|
|||||||
id = activity[0]
|
id = activity[0]
|
||||||
|
|
||||||
if 'json_summary' in export_formats:
|
if 'json_summary' in export_formats:
|
||||||
log.debug("getting json summary for {}".format(id))
|
log.debug("getting json summary for %s", id)
|
||||||
|
|
||||||
activity_summary = retryer.call(client.get_activity_summary, id)
|
activity_summary = retryer.call(client.get_activity_summary, id)
|
||||||
dest = os.path.join(
|
dest = os.path.join(
|
||||||
@ -126,7 +126,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
|
|||||||
f.write(json.dumps(activity_summary, ensure_ascii=False, indent=4))
|
f.write(json.dumps(activity_summary, ensure_ascii=False, indent=4))
|
||||||
|
|
||||||
if 'json_details' in export_formats:
|
if 'json_details' in export_formats:
|
||||||
log.debug("getting json details for {}".format(id))
|
log.debug("getting json details for %s", id)
|
||||||
activity_details = retryer.call(client.get_activity_details, id)
|
activity_details = retryer.call(client.get_activity_details, id)
|
||||||
dest = os.path.join(backup_dir, export_filename(activity, 'json_details'))
|
dest = os.path.join(backup_dir, export_filename(activity, 'json_details'))
|
||||||
with codecs.open(dest, encoding="utf-8", mode="w") as f:
|
with codecs.open(dest, encoding="utf-8", mode="w") as f:
|
||||||
@ -135,7 +135,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
|
|||||||
not_found_path = os.path.join(backup_dir, not_found_file)
|
not_found_path = os.path.join(backup_dir, not_found_file)
|
||||||
with open(not_found_path, mode="a") as not_found:
|
with open(not_found_path, mode="a") as not_found:
|
||||||
if 'gpx' in export_formats:
|
if 'gpx' in export_formats:
|
||||||
log.debug("getting gpx for {}".format(id))
|
log.debug("getting gpx for %s", id)
|
||||||
activity_gpx = retryer.call(client.get_activity_gpx, id)
|
activity_gpx = retryer.call(client.get_activity_gpx, id)
|
||||||
dest = os.path.join(backup_dir, export_filename(activity, 'gpx'))
|
dest = os.path.join(backup_dir, export_filename(activity, 'gpx'))
|
||||||
if activity_gpx is None:
|
if activity_gpx is None:
|
||||||
@ -145,7 +145,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
|
|||||||
f.write(activity_gpx)
|
f.write(activity_gpx)
|
||||||
|
|
||||||
if 'tcx' in export_formats:
|
if 'tcx' in export_formats:
|
||||||
log.debug("getting tcx for {}".format(id))
|
log.debug("getting tcx for %s", id)
|
||||||
activity_tcx = retryer.call(client.get_activity_tcx, id)
|
activity_tcx = retryer.call(client.get_activity_tcx, id)
|
||||||
dest = os.path.join(backup_dir, export_filename(activity, 'tcx'))
|
dest = os.path.join(backup_dir, export_filename(activity, 'tcx'))
|
||||||
if activity_tcx is None:
|
if activity_tcx is None:
|
||||||
@ -155,7 +155,7 @@ def download(client, activity, retryer, backup_dir, export_formats=None):
|
|||||||
f.write(activity_tcx)
|
f.write(activity_tcx)
|
||||||
|
|
||||||
if 'fit' in export_formats:
|
if 'fit' in export_formats:
|
||||||
log.debug("getting fit for {}".format(id))
|
log.debug("getting fit for %s", id)
|
||||||
activity_fit = retryer.call(client.get_activity_fit, id)
|
activity_fit = retryer.call(client.get_activity_fit, id)
|
||||||
dest = os.path.join(
|
dest = os.path.join(
|
||||||
backup_dir, export_filename(activity, 'fit'))
|
backup_dir, export_filename(activity, 'fit'))
|
||||||
|
@ -116,11 +116,11 @@ class GarminClient(object):
|
|||||||
headers = {'origin': 'https://sso.garmin.com'}
|
headers = {'origin': 'https://sso.garmin.com'}
|
||||||
auth_response = self.session.post(
|
auth_response = self.session.post(
|
||||||
SSO_LOGIN_URL, headers=headers, params=request_params, data=form_data)
|
SSO_LOGIN_URL, headers=headers, params=request_params, data=form_data)
|
||||||
log.debug("got auth response: {}".format(auth_response.text))
|
log.debug("got auth response: %s", auth_response.text)
|
||||||
if auth_response.status_code != 200:
|
if auth_response.status_code != 200:
|
||||||
raise ValueError("authentication failure: did you enter valid credentials?")
|
raise ValueError("authentication failure: did you enter valid credentials?")
|
||||||
auth_ticket_url = self._extract_auth_ticket_url(auth_response.text)
|
auth_ticket_url = self._extract_auth_ticket_url(auth_response.text)
|
||||||
log.debug("auth ticket url: '{}'".format(auth_ticket_url))
|
log.debug("auth ticket url: '%s'", auth_ticket_url)
|
||||||
|
|
||||||
log.info("claiming auth ticket ...")
|
log.info("claiming auth ticket ...")
|
||||||
response = self.session.get(auth_ticket_url)
|
response = self.session.get(auth_ticket_url)
|
||||||
@ -185,7 +185,7 @@ class GarminClient(object):
|
|||||||
:returns: A list of activity identifiers (along with their starting timestamps).
|
:returns: A list of activity identifiers (along with their starting timestamps).
|
||||||
:rtype: tuples of (int, datetime)
|
:rtype: tuples of (int, datetime)
|
||||||
"""
|
"""
|
||||||
log.debug("fetching activities {} through {} ...".format(start_index, start_index + max_limit - 1))
|
log.debug("fetching activities %d through %d ...", start_index, start_index + max_limit - 1)
|
||||||
response = self.session.get(
|
response = self.session.get(
|
||||||
"https://connect.garmin.com/modern/proxy/activitylist-service/activities/search/activities",
|
"https://connect.garmin.com/modern/proxy/activitylist-service/activities/search/activities",
|
||||||
params={"start": start_index, "limit": max_limit})
|
params={"start": start_index, "limit": max_limit})
|
||||||
@ -205,7 +205,7 @@ class GarminClient(object):
|
|||||||
# make sure UTC timezone gets set
|
# make sure UTC timezone gets set
|
||||||
timestamp_utc = timestamp_utc.replace(tzinfo=dateutil.tz.tzutc())
|
timestamp_utc = timestamp_utc.replace(tzinfo=dateutil.tz.tzutc())
|
||||||
entries.append((id, timestamp_utc))
|
entries.append((id, timestamp_utc))
|
||||||
log.debug("got {} activities.".format(len(entries)))
|
log.debug("got %d activities.", len(entries))
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
@require_session
|
@require_session
|
||||||
@ -222,8 +222,8 @@ class GarminClient(object):
|
|||||||
response = self.session.get(
|
response = self.session.get(
|
||||||
"https://connect.garmin.com/modern/proxy/activity-service/activity/{}".format(activity_id))
|
"https://connect.garmin.com/modern/proxy/activity-service/activity/{}".format(activity_id))
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
log.error(u"failed to fetch json summary for activity {}: {}\n{}".format(
|
log.error(u"failed to fetch json summary for activity %s: %d\n%s",
|
||||||
activity_id, response.status_code, response.text))
|
activity_id, response.status_code, response.text)
|
||||||
raise Exception(u"failed to fetch json summary for activity {}: {}\n{}".format(
|
raise Exception(u"failed to fetch json summary for activity {}: {}\n{}".format(
|
||||||
activity_id, response.status_code, response.text))
|
activity_id, response.status_code, response.text))
|
||||||
return json.loads(response.text)
|
return json.loads(response.text)
|
||||||
|
@ -35,7 +35,7 @@ def incremental_backup(username: str,
|
|||||||
"""
|
"""
|
||||||
# if no --format was specified, all formats are to be backed up
|
# if no --format was specified, all formats are to be backed up
|
||||||
format = format if format else export_formats
|
format = format if format else export_formats
|
||||||
log.info("backing up formats: {}".format(", ".join(format)))
|
log.info("backing up formats: %s", ", ".join(format))
|
||||||
|
|
||||||
if not os.path.isdir(backup_dir):
|
if not os.path.isdir(backup_dir):
|
||||||
os.makedirs(backup_dir)
|
os.makedirs(backup_dir)
|
||||||
@ -50,23 +50,23 @@ def incremental_backup(username: str,
|
|||||||
|
|
||||||
with GarminClient(username, password) as client:
|
with GarminClient(username, password) as client:
|
||||||
# get all activity ids and timestamps from Garmin account
|
# get all activity ids and timestamps from Garmin account
|
||||||
log.info("scanning activities for {} ...".format(username))
|
log.info("scanning activities for %s ...", username)
|
||||||
activities = set(retryer.call(client.list_activities))
|
activities = set(retryer.call(client.list_activities))
|
||||||
log.info("account has a total of {} activities".format(len(activities)))
|
log.info("account has a total of %d activities", len(activities))
|
||||||
|
|
||||||
missing_activities = garminexport.backup.need_backup(activities, backup_dir, format)
|
missing_activities = garminexport.backup.need_backup(activities, backup_dir, format)
|
||||||
backed_up = activities - missing_activities
|
backed_up = activities - missing_activities
|
||||||
log.info("{} contains {} backed up activities".format(backup_dir, len(backed_up)))
|
log.info("%s contains %d backed up activities", backup_dir, len(backed_up))
|
||||||
|
|
||||||
log.info("activities that aren't backed up: {}".format(len(missing_activities)))
|
log.info("activities that aren't backed up: %d", len(missing_activities))
|
||||||
|
|
||||||
for index, activity in enumerate(missing_activities):
|
for index, activity in enumerate(missing_activities):
|
||||||
id, start = activity
|
id, start = activity
|
||||||
log.info("backing up activity {} from {} ({} out of {}) ...".format(
|
log.info("backing up activity %s from %s (%d out of %d) ...",
|
||||||
id, start, index + 1, len(missing_activities)))
|
id, start, index + 1, len(missing_activities))
|
||||||
try:
|
try:
|
||||||
garminexport.backup.download(client, activity, retryer, backup_dir, format)
|
garminexport.backup.download(client, activity, retryer, backup_dir, format)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("failed with exception: {}".format(e))
|
log.error("failed with exception: %s", e)
|
||||||
if not ignore_errors:
|
if not ignore_errors:
|
||||||
raise
|
raise
|
||||||
|
@ -195,21 +195,21 @@ class Retryer(object):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
attempts += 1
|
attempts += 1
|
||||||
log.info('{{}}: attempt {} ...'.format(name, attempts))
|
log.info('{%s}: attempt %d ...', name, attempts)
|
||||||
returnval = function(*args, **kw)
|
returnval = function(*args, **kw)
|
||||||
if self.returnval_predicate(returnval):
|
if self.returnval_predicate(returnval):
|
||||||
# return value satisfies predicate, we're done!
|
# return value satisfies predicate, we're done!
|
||||||
log.debug('{{}}: success: "{}"'.format(name, returnval))
|
log.debug('{%s}: success: "%s"', name, returnval)
|
||||||
return returnval
|
return returnval
|
||||||
log.debug('{{}}: failed: return value: {}'.format(name, returnval))
|
log.debug('{%s}: failed: return value: %s', name, returnval)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not self.error_strategy.should_suppress(e):
|
if not self.error_strategy.should_suppress(e):
|
||||||
raise e
|
raise e
|
||||||
log.debug('{{}}: failed: error: {}'.format(name, e))
|
log.debug('{%s}: failed: error: %s', name, e)
|
||||||
elapsed_time = datetime.now() - start
|
elapsed_time = datetime.now() - start
|
||||||
# should we make another attempt?
|
# should we make another attempt?
|
||||||
if not self.stop_strategy.should_continue(attempts, elapsed_time):
|
if not self.stop_strategy.should_continue(attempts, elapsed_time):
|
||||||
raise GaveUpError('{{}}: gave up after {} failed attempt(s)'.format(name, attempts))
|
raise GaveUpError('{{}}: gave up after {} failed attempt(s)'.format(name, attempts))
|
||||||
delay = self.delay_strategy.next_delay(attempts)
|
delay = self.delay_strategy.next_delay(attempts)
|
||||||
log.info('{{}}: waiting {} seconds for next attempt'.format(name, delay.total_seconds()))
|
log.info('{%s}: waiting %d seconds for next attempt', name, delay.total_seconds())
|
||||||
time.sleep(delay.total_seconds())
|
time.sleep(delay.total_seconds())
|
||||||
|
@ -64,7 +64,7 @@ if __name__ == "__main__":
|
|||||||
args.password = getpass.getpass("Enter password: ")
|
args.password = getpass.getpass("Enter password: ")
|
||||||
|
|
||||||
with GarminClient(args.username, args.password) as client:
|
with GarminClient(args.username, args.password) as client:
|
||||||
log.info("fetching activity {} ...".format(args.activity))
|
log.info("fetching activity %s ...", args.activity)
|
||||||
summary = client.get_activity_summary(args.activity)
|
summary = client.get_activity_summary(args.activity)
|
||||||
# set up a retryer that will handle retries of failed activity downloads
|
# set up a retryer that will handle retries of failed activity downloads
|
||||||
retryer = Retryer(
|
retryer = Retryer(
|
||||||
@ -75,5 +75,5 @@ if __name__ == "__main__":
|
|||||||
garminexport.backup.download(
|
garminexport.backup.download(
|
||||||
client, (args.activity, start_time), retryer, args.destination, export_formats=[args.format])
|
client, (args.activity, start_time), retryer, args.destination, export_formats=[args.format])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("failed with exception: {}".format(e))
|
log.error("failed with exception: %s", e)
|
||||||
raise
|
raise
|
||||||
|
@ -11,7 +11,7 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)-15s [%(levelname)s] %
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Export all Garmin Connect activities")
|
description="Export all Garmin Connect activities")
|
||||||
# positional args
|
# positional args
|
||||||
@ -26,22 +26,22 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
if not args.password:
|
if not args.password:
|
||||||
args.password = getpass.getpass("Enter password: ")
|
args.password = getpass.getpass("Enter password: ")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with GarminClient(args.username, args.password) as client:
|
with GarminClient(args.username, args.password) as client:
|
||||||
log.info("activities:")
|
log.info("activities:")
|
||||||
activity_ids = client.list_activities()
|
activity_ids = client.list_activities()
|
||||||
log.info("num ids: {}".format(len(activity_ids)))
|
log.info("num ids: %d", len(activity_ids))
|
||||||
log.info(activity_ids)
|
log.info(activity_ids)
|
||||||
|
|
||||||
latest_activity, latest_activity_start = activity_ids[0]
|
latest_activity, latest_activity_start = activity_ids[0]
|
||||||
activity = client.get_activity_summary(latest_activity)
|
activity = client.get_activity_summary(latest_activity)
|
||||||
log.info("activity id: {}".format(activity["activity"]["activityId"]))
|
log.info("activity id: %s", activity["activity"]["activityId"])
|
||||||
log.info("activity name: '{}'".format(activity["activity"]["activityName"]))
|
log.info("activity name: '%s'", activity["activity"]["activityName"])
|
||||||
log.info("activity description: '{}'".format(activity["activity"]["activityDescription"]))
|
log.info("activity description: '%s'", activity["activity"]["activityDescription"])
|
||||||
log.info(json.dumps(client.get_activity_details(latest_activity), indent=4))
|
log.info(json.dumps(client.get_activity_details(latest_activity), indent=4))
|
||||||
log.info(client.get_activity_gpx(latest_activity))
|
log.info(client.get_activity_gpx(latest_activity))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("failed with exception: {}".format(e))
|
log.error("failed with exception: %s", e)
|
||||||
finally:
|
finally:
|
||||||
log.info("done")
|
log.info("done")
|
||||||
|
@ -56,15 +56,15 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
with GarminClient(args.username, args.password) as client:
|
with GarminClient(args.username, args.password) as client:
|
||||||
for activity in args.activity:
|
for activity in args.activity:
|
||||||
log.info("uploading activity file {} ...".format(activity.name))
|
log.info("uploading activity file %s ...", activity.name)
|
||||||
try:
|
try:
|
||||||
id = client.upload_activity(activity, name=args.name, description=args.description,
|
id = client.upload_activity(activity, name=args.name, description=args.description,
|
||||||
private=args.private, activity_type=args.type)
|
private=args.private, activity_type=args.type)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("upload failed: {!r}".format(e))
|
log.error("upload failed: {!r}".format(e))
|
||||||
else:
|
else:
|
||||||
log.info("upload successful: https://connect.garmin.com/modern/activity/{}".format(id))
|
log.info("upload successful: https://connect.garmin.com/modern/activity/%s", id)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("failed with exception: {}".format(e))
|
log.error("failed with exception: %s", e)
|
||||||
raise
|
raise
|
||||||
|
Loading…
Reference in New Issue
Block a user