From eec4dcf1de96e4c586a0cafd4c41347b34cf05ac Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 28 Mar 2018 15:33:47 -0700 Subject: [PATCH 1/2] Garmin changed the endpoint for setting metadata Cribbed from cpfair/tapiriik: https://github.com/cpfair/tapiriik/commit/e685dce36f3cb89a9fa3f0692bcb78676b05dc23 --- garminexport/garminclient.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/garminexport/garminclient.py b/garminexport/garminclient.py index aed53d8..019529a 100755 --- a/garminexport/garminclient.py +++ b/garminexport/garminclient.py @@ -393,21 +393,17 @@ class GarminClient(object): activity_id = j["successes"][0]["internalId"] # add optional fields - fields = ( ('name',name,("display","value")), - ('description',description,("display","value")), - ('type',activity_type,("activityType","key")), - ('privacy','private' if private else None,("definition","key")) ) - for endpoint, value, path in fields: - if value is not None: - response = self.session.post("https://connect.garmin.com/proxy/activity-service-1.2/json/{}/{}".format(endpoint, activity_id), - data={'value':value}) - if response.status_code != 200: - raise Exception(u"failed to set {} for activity {}: {}\n{}".format( - endpoint, activity_id, response.status_code, response.text)) - - j = response.json() - p0, p1 = path - if p0 not in j or j[p0][p1] != value: - raise Exception(u"failed to set {} for activity {}\n".format(endpoint, activity_id)) + data = {} + if name is not None: data['activityName'] = name + if description is not None: data['description'] = name + if activity_type is not None: data['activityTypeDTO'] = {"typeKey": activity_type} + if private: data['privacy'] = {"typeKey": "private"} + if data: + data['activityId'] = activity_id + encoding_headers = {"Content-Type": "application/json; charset=UTF-8"} # see Tapiriik + response = self.session.put("https://connect.garmin.com/proxy/activity-service/activity/{}".format(activity_id), data=json.dumps(data), headers=encoding_headers) + if response.status_code != 204: + raise Exception(u"failed to set metadata for activity {}: {}\n{}".format( + activity_id, response.status_code, response.text)) return activity_id From 34c7a823ad109f2721c9dc368a4fef1c05a24175 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 28 Mar 2018 15:35:25 -0700 Subject: [PATCH 2/2] allow overriding activity type from CLI --- upload_activity.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/upload_activity.py b/upload_activity.py index 3198e44..37f2da0 100755 --- a/upload_activity.py +++ b/upload_activity.py @@ -41,6 +41,8 @@ if __name__ == "__main__": '-D', '--description', help="Activity description on Garmin Connect.") parser.add_argument( '-P', '--private', action='store_true', help="Make activity private on Garmin Connect.") + parser.add_argument( + '-T', '--type', help="Override activity type (running, cycling, walking, hiking, strength_training, etc.)") parser.add_argument( "--log-level", metavar="LEVEL", type=str, help=("Desired log output level (DEBUG, INFO, WARNING, ERROR). " @@ -61,9 +63,9 @@ if __name__ == "__main__": for activity in args.activity: log.info("uploading activity file {} ...".format(activity.name)) try: - id = client.upload_activity(activity, name=args.name, description=args.description, private=args.private) + id = client.upload_activity(activity, name=args.name, description=args.description, private=args.private, activity_type=args.type) except Exception as e: - log.error("upload failed: {}".format(e)) + log.error("upload failed: {!r}".format(e)) else: log.info("upload successful: https://connect.garmin.com/modern/activity/{}".format(id)) except Exception as e: