From 81d4ecf26d54fab1c589ead006855a544088a988 Mon Sep 17 00:00:00 2001 From: petergardfjall Date: Wed, 12 Nov 2014 12:35:13 +0100 Subject: [PATCH] added script for getting one particular activity --- get_activity.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 get_activity.py diff --git a/get_activity.py b/get_activity.py new file mode 100755 index 0000000..0235af0 --- /dev/null +++ b/get_activity.py @@ -0,0 +1,70 @@ +#! /usr/bin/env python +"""A program that downloads one particular activity from a given Garmin +Connect account and stores it locally on the user's computer. +""" +import argparse +import getpass +from garminexport.garminclient import GarminClient +import garminexport.util +import logging +import os +import sys +import traceback + +logging.basicConfig( + level=logging.INFO, format="%(asctime)-15s [%(levelname)s] %(message)s") +log = logging.getLogger(__name__) + +LOG_LEVELS = { + "DEBUG": logging.DEBUG, + "INFO": logging.INFO, + "WARNING": logging.WARNING, + "ERROR": logging.ERROR +} +"""Command-line (string-based) log-level mapping to logging module levels.""" + + +if __name__ == "__main__": + + parser = argparse.ArgumentParser( + description=("Downloads one particular activity for a given " + "Garmin Connect account.")) + # positional args + parser.add_argument( + "username", metavar="", type=str, help="Account user name.") + parser.add_argument( + "activity", metavar="", type=int, help="Activity ID.") + + # optional args + parser.add_argument( + "--password", type=str, help="Account password.") + parser.add_argument( + "--destination", metavar="DIR", type=str, + help=("Destination directory for downloaded activity. Default: " + "./activities/"), default=os.path.join(".", "activities")) + parser.add_argument( + "--log-level", metavar="LEVEL", type=str, + help=("Desired log output level (DEBUG, INFO, WARNING, ERROR). " + "Default: INFO."), default="INFO") + + args = parser.parse_args() + if not args.log_level in LOG_LEVELS: + raise ValueError("Illegal log-level argument: {}".format(args.log_level)) + logging.root.setLevel(LOG_LEVELS[args.log_level]) + + try: + if not os.path.isdir(args.destination): + os.makedirs(args.destination) + + if not args.password: + args.password = getpass.getpass("Enter password: ") + + with GarminClient(args.username, args.password) as client: + log.info("fetching activity {} ...".format(args.activity)) + garminexport.util.save_activity( + client, args.activity, args.destination) + except Exception as e: + exc_type, exc_value, exc_traceback = sys.exc_info() + log.error(u"failed with exception: %s", e) + raise +