2015-02-22 10:52:36 +01:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""A program that uploads an activity file to a Garmin
|
|
|
|
Connect account.
|
|
|
|
"""
|
|
|
|
import argparse
|
|
|
|
import getpass
|
|
|
|
import logging
|
|
|
|
|
2020-03-08 17:19:19 +01:00
|
|
|
from garminexport.garminclient import GarminClient
|
|
|
|
from garminexport.logging_config import LOG_LEVELS
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)-15s [%(levelname)s] %(message)s")
|
2015-02-22 10:52:36 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2020-03-08 20:05:56 +01:00
|
|
|
description="Uploads an activity file to a Garmin Connect account.")
|
|
|
|
|
2015-02-22 10:52:36 +01:00
|
|
|
# positional args
|
|
|
|
parser.add_argument(
|
|
|
|
"username", metavar="<username>", type=str, help="Account user name.")
|
|
|
|
parser.add_argument(
|
2017-12-22 01:11:21 +01:00
|
|
|
"activity", nargs='+', metavar="<file>", type=argparse.FileType("rb"),
|
2015-02-22 10:52:36 +01:00
|
|
|
help="Activity file (.gpx, .tcx, or .fit).")
|
|
|
|
|
|
|
|
# optional args
|
|
|
|
parser.add_argument(
|
|
|
|
"--password", type=str, help="Account password.")
|
|
|
|
parser.add_argument(
|
|
|
|
'-N', '--name', help="Activity name on Garmin Connect.")
|
|
|
|
parser.add_argument(
|
|
|
|
'-D', '--description', help="Activity description on Garmin Connect.")
|
|
|
|
parser.add_argument(
|
|
|
|
'-P', '--private', action='store_true', help="Make activity private on Garmin Connect.")
|
2018-03-29 00:35:25 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'-T', '--type', help="Override activity type (running, cycling, walking, hiking, strength_training, etc.)")
|
2015-02-22 10:52:36 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--log-level", metavar="LEVEL", type=str,
|
2020-03-08 20:05:56 +01:00
|
|
|
help="Desired log output level (DEBUG, INFO, WARNING, ERROR). Default: INFO.",
|
|
|
|
default="INFO")
|
2015-02-22 10:52:36 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
2020-03-08 20:05:56 +01:00
|
|
|
|
|
|
|
if len(args.activity) > 1 and (args.description is not None or args.name is not None):
|
2017-12-22 01:11:21 +01:00
|
|
|
parser.error("When uploading multiple activities, --name or --description cannot be used.")
|
2020-03-08 20:05:56 +01:00
|
|
|
|
|
|
|
if args.log_level not in LOG_LEVELS:
|
|
|
|
raise ValueError("Illegal log-level argument: {}".format(args.log_level))
|
|
|
|
|
2015-02-22 10:52:36 +01:00
|
|
|
logging.root.setLevel(LOG_LEVELS[args.log_level])
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not args.password:
|
|
|
|
args.password = getpass.getpass("Enter password: ")
|
2020-03-08 20:05:56 +01:00
|
|
|
|
2015-02-22 10:52:36 +01:00
|
|
|
with GarminClient(args.username, args.password) as client:
|
2017-12-22 01:11:21 +01:00
|
|
|
for activity in args.activity:
|
|
|
|
log.info("uploading activity file {} ...".format(activity.name))
|
|
|
|
try:
|
2020-03-08 20:05:56 +01:00
|
|
|
id = client.upload_activity(activity, name=args.name, description=args.description,
|
|
|
|
private=args.private, activity_type=args.type)
|
2017-12-22 01:11:21 +01:00
|
|
|
except Exception as e:
|
2018-03-29 00:35:25 +02:00
|
|
|
log.error("upload failed: {!r}".format(e))
|
2017-12-22 01:11:21 +01:00
|
|
|
else:
|
|
|
|
log.info("upload successful: https://connect.garmin.com/modern/activity/{}".format(id))
|
2020-03-08 20:05:56 +01:00
|
|
|
|
2015-02-22 10:52:36 +01:00
|
|
|
except Exception as e:
|
2020-03-08 20:05:56 +01:00
|
|
|
log.error("failed with exception: {}".format(e))
|
2015-02-22 10:52:36 +01:00
|
|
|
raise
|