diff --git a/garminexport/garminclient.py b/garminexport/garminclient.py index b3a3351..aed53d8 100755 --- a/garminexport/garminclient.py +++ b/garminexport/garminclient.py @@ -376,14 +376,21 @@ class GarminClient(object): files=files, headers={"nk": "NT"}) # check response and get activity ID - if response.status_code != 200: + try: + j = response.json()["detailedImportResult"] + except (json.JSONDecodeException, KeyError): raise Exception(u"failed to upload {} for activity: {}\n{}".format( format, response.status_code, response.text)) - j = response.json() - if len(j["detailedImportResult"]["failures"]) or len(j["detailedImportResult"]["successes"])!=1: - raise Exception(u"failed to upload {} for activity") - activity_id = j["detailedImportResult"]["successes"][0]["internalId"] + if len(j["failures"]) or len(j["successes"])<1: + raise Exception(u"failed to upload {} for activity: {}\n{}".format( + format, response.status_code, j["failures"])) + + if len(j["successes"])>1: + raise Exception(u"uploading {} resulted in multiple activities ({})".format( + format, len(j["successes"]))) + + activity_id = j["successes"][0]["internalId"] # add optional fields fields = ( ('name',name,("display","value")), diff --git a/upload_activity.py b/upload_activity.py index e03440f..3198e44 100755 --- a/upload_activity.py +++ b/upload_activity.py @@ -29,7 +29,7 @@ if __name__ == "__main__": parser.add_argument( "username", metavar="", type=str, help="Account user name.") parser.add_argument( - "activity", metavar="", type=argparse.FileType("rb"), + "activity", nargs='+', metavar="", type=argparse.FileType("rb"), help="Activity file (.gpx, .tcx, or .fit).") # optional args @@ -47,6 +47,8 @@ if __name__ == "__main__": "Default: INFO."), default="INFO") args = parser.parse_args() + if len(args.activity)>1 and (args.description is not None or args.name is not None): + parser.error("When uploading multiple activities, --name or --description cannot be used.") if not args.log_level in LOG_LEVELS: raise ValueError("Illegal log-level argument: {}".format( args.log_level)) @@ -56,9 +58,14 @@ if __name__ == "__main__": if not args.password: args.password = getpass.getpass("Enter password: ") with GarminClient(args.username, args.password) as client: - log.info("uploading activity file {} ...".format(args.activity.name)) - id = client.upload_activity(args.activity, name=args.name, description=args.description, private=args.private) - log.info("upload successful: https://connect.garmin.com/activity/{}".format(id)) + 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) + except Exception as e: + log.error("upload failed: {}".format(e)) + else: + log.info("upload successful: https://connect.garmin.com/modern/activity/{}".format(id)) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() log.error(u"failed with exception: %s", e)