Merge pull request #17 from dlenski/fix_and_improve_upload
Fix and improve upload
This commit is contained in:
commit
8d62829e6d
@ -372,18 +372,25 @@ class GarminClient(object):
|
||||
|
||||
# upload it
|
||||
files = dict(data=(fn, file))
|
||||
response = self.session.post("https://connect.garmin.com/proxy/upload-service-1.1/json/upload/.{}".format(format),
|
||||
files=files)
|
||||
response = self.session.post("https://connect.garmin.com/modern/proxy/upload-service/upload/.{}".format(format),
|
||||
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")),
|
||||
|
@ -29,7 +29,7 @@ if __name__ == "__main__":
|
||||
parser.add_argument(
|
||||
"username", metavar="<username>", type=str, help="Account user name.")
|
||||
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).")
|
||||
|
||||
# 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)
|
||||
|
Loading…
Reference in New Issue
Block a user