not all activities have FIT files (e.g., manually entered ones)

This commit is contained in:
petergardfjall 2014-11-09 13:56:50 +01:00
parent 35aea411d0
commit aa5de9c4a7
3 changed files with 20 additions and 10 deletions

View File

@ -45,7 +45,8 @@ For each activity, these files are stored:
- an activity TCX file (XML) - an activity TCX file (XML)
- an activity FIT file (binary) - an activity FIT file (binary) (if available -- the activity may have
been entered manuall rather than imported from a Garmin device).
All files are written to the same directory (``activities/<timestamp>/`` All files are written to the same directory (``activities/<timestamp>/``
by default). Each activity file is prefixed by its upload timestamp and its by default). Each activity file is prefixed by its upload timestamp and its

View File

@ -92,6 +92,7 @@ if __name__ == "__main__":
f.write(activity_gpx) f.write(activity_gpx)
with codecs.open(tcx_file, encoding="utf-8", mode="w") as f: with codecs.open(tcx_file, encoding="utf-8", mode="w") as f:
f.write(activity_tcx) f.write(activity_tcx)
if activity_fit:
with open(fit_file, mode="wb") as f: with open(fit_file, mode="wb") as f:
f.write(activity_fit) f.write(activity_fit)
except Exception as e: except Exception as e:

View File

@ -262,22 +262,30 @@ class GarminClient(object):
:returns: The TCX representation of the activity as an XML string. :returns: The TCX representation of the activity as an XML string.
:rtype: str :rtype: str
""" """
response = self.session.get("https://connect.garmin.com/proxy/activity-service-1.3/tcx/course/{}".format(activity_id))
response = self.session.get("http://connect.garmin.com/proxy/activity-service-1.1/tcx/activity/{}?full=true".format(activity_id))
if response.status_code != 200: if response.status_code != 200:
raise Exception(u"failed to fetch TCX for activity {}: {}\n{}".format( raise Exception(u"failed to fetch TCX for activity {}: {}\n{}".format(
activity_id, response.status_code, response.text)) activity_id, response.status_code, response.text))
return response.text return response.text
def get_activity_fit(self, activity_id): def get_activity_fit(self, activity_id):
"""Return a FIT representation for a given activity. """Return a FIT representation for a given activity. If the activity
doesn't have a FIT source (for example, if it was entered manually
rather than imported from a Garmin device) a :obj:`None` value is
returned.
:param activity_id: Activity identifier. :param activity_id: Activity identifier.
:type activity_id: int :type activity_id: int
:returns: A string with a FIT file for the activity. :returns: A string with a FIT file for the activity or :obj:`None`
if no FIT source exists for this activity (e.g., entered manually).
:rtype: str :rtype: str
""" """
response = self.session.get("https://connect.garmin.com/proxy/download-service/files/activity/{}".format(activity_id)) response = self.session.get("https://connect.garmin.com/proxy/download-service/files/activity/{}".format(activity_id))
if response.status_code == 404:
# No FIT source available for activity
return None
if response.status_code != 200: if response.status_code != 200:
raise Exception(u"failed to fetch FIT for activity {}: {}\n{}".format( raise Exception(u"failed to fetch FIT for activity {}: {}\n{}".format(
activity_id, response.status_code, response.text)) activity_id, response.status_code, response.text))