allow upload of multiple activity files, and improve error handling
This commit is contained in:
		
							parent
							
								
									d410a7c212
								
							
						
					
					
						commit
						31974b0bf0
					
				@ -376,14 +376,21 @@ class GarminClient(object):
 | 
				
			|||||||
                                     files=files, headers={"nk": "NT"})
 | 
					                                     files=files, headers={"nk": "NT"})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # check response and get activity ID
 | 
					        # 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(
 | 
					            raise Exception(u"failed to upload {} for activity: {}\n{}".format(
 | 
				
			||||||
                format, response.status_code, response.text))
 | 
					                format, response.status_code, response.text))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        j = response.json()
 | 
					        if len(j["failures"]) or len(j["successes"])<1:
 | 
				
			||||||
        if len(j["detailedImportResult"]["failures"]) or len(j["detailedImportResult"]["successes"])!=1:
 | 
					            raise Exception(u"failed to upload {} for activity: {}\n{}".format(
 | 
				
			||||||
            raise Exception(u"failed to upload {} for activity")
 | 
					                format, response.status_code, j["failures"]))
 | 
				
			||||||
        activity_id = j["detailedImportResult"]["successes"][0]["internalId"]
 | 
					
 | 
				
			||||||
 | 
					        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
 | 
					        # add optional fields
 | 
				
			||||||
        fields = ( ('name',name,("display","value")),
 | 
					        fields = ( ('name',name,("display","value")),
 | 
				
			||||||
 | 
				
			|||||||
@ -29,7 +29,7 @@ if __name__ == "__main__":
 | 
				
			|||||||
    parser.add_argument(
 | 
					    parser.add_argument(
 | 
				
			||||||
        "username", metavar="<username>", type=str, help="Account user name.")
 | 
					        "username", metavar="<username>", type=str, help="Account user name.")
 | 
				
			||||||
    parser.add_argument(
 | 
					    parser.add_argument(
 | 
				
			||||||
        "activity", metavar="<file>", type=argparse.FileType("rb"),
 | 
					        "activity", nargs='+', metavar="<file>", type=argparse.FileType("rb"),
 | 
				
			||||||
        help="Activity file (.gpx, .tcx, or .fit).")
 | 
					        help="Activity file (.gpx, .tcx, or .fit).")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # optional args
 | 
					    # optional args
 | 
				
			||||||
@ -47,6 +47,8 @@ if __name__ == "__main__":
 | 
				
			|||||||
              "Default: INFO."), default="INFO")
 | 
					              "Default: INFO."), default="INFO")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    args = parser.parse_args()
 | 
					    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:
 | 
					    if not args.log_level in LOG_LEVELS:
 | 
				
			||||||
        raise ValueError("Illegal log-level argument: {}".format(
 | 
					        raise ValueError("Illegal log-level argument: {}".format(
 | 
				
			||||||
            args.log_level))
 | 
					            args.log_level))
 | 
				
			||||||
@ -56,9 +58,14 @@ if __name__ == "__main__":
 | 
				
			|||||||
        if not args.password:
 | 
					        if not args.password:
 | 
				
			||||||
            args.password = getpass.getpass("Enter password: ")
 | 
					            args.password = getpass.getpass("Enter password: ")
 | 
				
			||||||
        with GarminClient(args.username, args.password) as client:
 | 
					        with GarminClient(args.username, args.password) as client:
 | 
				
			||||||
            log.info("uploading activity file {} ...".format(args.activity.name))
 | 
					            for activity in args.activity:
 | 
				
			||||||
            id = client.upload_activity(args.activity, name=args.name, description=args.description, private=args.private)
 | 
					                log.info("uploading activity file {} ...".format(activity.name))
 | 
				
			||||||
            log.info("upload successful: https://connect.garmin.com/activity/{}".format(id))
 | 
					                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:
 | 
					    except Exception as e:
 | 
				
			||||||
        exc_type, exc_value, exc_traceback = sys.exc_info()
 | 
					        exc_type, exc_value, exc_traceback = sys.exc_info()
 | 
				
			||||||
        log.error(u"failed with exception: %s", e)
 | 
					        log.error(u"failed with exception: %s", e)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user