Merge pull request #22 from dlenski/fix_and_improve_upload

Fix and improve upload (round 2)
This commit is contained in:
Peter Gardfjäll 2018-04-01 17:15:18 +02:00 committed by GitHub
commit 186fa4ac7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 18 deletions

View File

@ -393,21 +393,17 @@ class GarminClient(object):
activity_id = j["successes"][0]["internalId"] activity_id = j["successes"][0]["internalId"]
# add optional fields # add optional fields
fields = ( ('name',name,("display","value")), data = {}
('description',description,("display","value")), if name is not None: data['activityName'] = name
('type',activity_type,("activityType","key")), if description is not None: data['description'] = name
('privacy','private' if private else None,("definition","key")) ) if activity_type is not None: data['activityTypeDTO'] = {"typeKey": activity_type}
for endpoint, value, path in fields: if private: data['privacy'] = {"typeKey": "private"}
if value is not None: if data:
response = self.session.post("https://connect.garmin.com/proxy/activity-service-1.2/json/{}/{}".format(endpoint, activity_id), data['activityId'] = activity_id
data={'value':value}) encoding_headers = {"Content-Type": "application/json; charset=UTF-8"} # see Tapiriik
if response.status_code != 200: response = self.session.put("https://connect.garmin.com/proxy/activity-service/activity/{}".format(activity_id), data=json.dumps(data), headers=encoding_headers)
raise Exception(u"failed to set {} for activity {}: {}\n{}".format( if response.status_code != 204:
endpoint, activity_id, response.status_code, response.text)) raise Exception(u"failed to set metadata for activity {}: {}\n{}".format(
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))
return activity_id return activity_id

View File

@ -41,6 +41,8 @@ if __name__ == "__main__":
'-D', '--description', help="Activity description on Garmin Connect.") '-D', '--description', help="Activity description on Garmin Connect.")
parser.add_argument( parser.add_argument(
'-P', '--private', action='store_true', help="Make activity private on Garmin Connect.") '-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( parser.add_argument(
"--log-level", metavar="LEVEL", type=str, "--log-level", metavar="LEVEL", type=str,
help=("Desired log output level (DEBUG, INFO, WARNING, ERROR). " help=("Desired log output level (DEBUG, INFO, WARNING, ERROR). "
@ -61,9 +63,9 @@ if __name__ == "__main__":
for activity in args.activity: for activity in args.activity:
log.info("uploading activity file {} ...".format(activity.name)) log.info("uploading activity file {} ...".format(activity.name))
try: 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: except Exception as e:
log.error("upload failed: {}".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/{}".format(id))
except Exception as e: except Exception as e: