refactoring

This commit is contained in:
petergardfjall
2014-11-12 16:12:33 +01:00
parent 4789ebe3e4
commit d17df2fc6c
4 changed files with 60 additions and 35 deletions
+46 -28
View File
@@ -6,9 +6,12 @@ import json
from datetime import datetime
import os
def save_activity(client, activity_id, destination):
"""Downloads a certain Garmin Connect activity and saves it
to a given destination directory.
export_formats=["json_summary", "json_details", "gpx", "tcx", "fit"]
def export_activity(client, activity_id, destination,
formats=None):
"""Exports a Garmin Connect activity to a given set of formats
and saves the result file(es) to a given destination directory.
:param client: A :class:`garminexport.garminclient.GarminClient`
instance that is assumed to be connected.
@@ -17,37 +20,52 @@ def save_activity(client, activity_id, destination):
:type activity_id: int
:param destination: Destination directory (assumed to exist already).
:type destination: str
:keyword formats: Which format(s) to export to. Could be any
of: 'json_summary', 'json_details', 'gpx', 'tcx', 'fit'.
If set to :obj:`None` all formats will be exported.
:type formats: list of str
"""
if formats is None:
formats = ['json_summary', 'json_details', 'gpx', 'tcx', 'fit']
activity_summary = client.get_activity_summary(activity_id)
activity_details = client.get_activity_details(activity_id)
activity_gpx = client.get_activity_gpx(activity_id)
activity_tcx = client.get_activity_tcx(activity_id)
activity_fit = client.get_activity_fit(activity_id)
# save activitity summary, details and GPX, TCX and FIT file.
# prefix saved activity files with timestamp and activity id
creation_millis = activity_summary["activity"]["uploadDate"]["millis"]
timestamp = datetime.fromtimestamp(int(creation_millis)/1000.0)
filename_prefix = "{}_{}".format(
timestamp.strftime("%Y%m%d-%H%M%S"), activity_id)
path_prefix = os.path.join(destination, filename_prefix)
summary_file = path_prefix + "_summary.json"
details_file = path_prefix + "_details.json"
gpx_file = path_prefix + ".gpx"
tcx_file = path_prefix + ".tcx"
fit_file = path_prefix + ".fit"
with codecs.open(summary_file, encoding="utf-8", mode="w") as f:
f.write(json.dumps(
activity_summary, ensure_ascii=False, indent=4))
with codecs.open(details_file, encoding="utf-8", mode="w") as f:
f.write(json.dumps(
activity_details, ensure_ascii=False, indent=4))
with codecs.open(gpx_file, encoding="utf-8", mode="w") as f:
f.write(activity_gpx)
with codecs.open(tcx_file, encoding="utf-8", mode="w") as f:
f.write(activity_tcx)
if activity_fit:
with open(fit_file, mode="wb") as f:
f.write(activity_fit)
if 'json_summary' in formats:
summary_file = path_prefix + "_summary.json"
with codecs.open(summary_file, encoding="utf-8", mode="w") as f:
f.write(json.dumps(
activity_summary, ensure_ascii=False, indent=4))
if 'json_details' in formats:
activity_details = client.get_activity_details(activity_id)
details_file = path_prefix + "_details.json"
with codecs.open(details_file, encoding="utf-8", mode="w") as f:
f.write(json.dumps(
activity_details, ensure_ascii=False, indent=4))
if 'gpx' in formats:
activity_gpx = client.get_activity_gpx(activity_id)
gpx_file = path_prefix + ".gpx"
with codecs.open(gpx_file, encoding="utf-8", mode="w") as f:
f.write(activity_gpx)
if 'tcx' in formats:
activity_tcx = client.get_activity_tcx(activity_id)
tcx_file = path_prefix + ".tcx"
with codecs.open(tcx_file, encoding="utf-8", mode="w") as f:
f.write(activity_tcx)
if 'fit' in formats:
activity_fit = client.get_activity_fit(activity_id)
fit_file = path_prefix + ".fit"
if activity_fit:
with open(fit_file, mode="wb") as f:
f.write(activity_fit)