#!/usr/bin/python
#
# Code by Tony Bussieres <t.bussieres@gmail.com> inspired by 
# 40-convert_to_tcx.py by Gustav Tiger <gustav@tiger.name>
#
# This helper uses GcpUploader to send the fit files to Garmin Connect
# 
# To install GcpUploader:
#
# sudo pip install GcpUploader
#
# edit the file ~/.guploadrc and add the following
# [Credentials]
# username=yourgarminuser
# password=yourgarminpass
#
# Then point the GUPLOAD environment variable at the path to your gupload.py
# script (defaults to /usr/bin/gupload.py)
#
# Don't forget to make this script executable :
#
# chmod +x /path/to/40-upload_to_garmin_connect.py

from __future__ import absolute_import, print_function

import errno
import os
import subprocess
import sys

gupload = os.getenv("GUPLOAD") or "/usr/bin/gupload.py"
if not os.path.exists(gupload):
    sys.stderr.write("%s didn't exist; ensure GcpUploader is installed, "
                     "and set GUPLOAD correctly if necessary.\n")
    sys.exit(1)

def main(action, filename):

    if action != "DOWNLOAD":
        return 0

    try:
        process = subprocess.Popen([gupload, filename], stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        (data, _) = process.communicate()
    except OSError as e:
        print("Could not send to Garmin", gupload, \
              "-", errno.errorcode[e.errno], os.strerror(e.errno))
        return -1

    if process.returncode != 0:
        print("gupload.py exited with error code", process.returncode)
        return -1

    if data.find("Status: SUCCESS ") != -1:
        print("Successfully uploaded %s to Garmin Connect" % filename)
        return 0

    if data.find("Status: EXISTS ") != -1:
        print("%s already uploaded to Garmin Connect" % filename)
        return 0

    print("Couldn't understand output from uploading %s to Garmin Connect:" %
          filename)
    print(data)
    return -1

if __name__ == "__main__":
    sys.exit(main(sys.argv[1], sys.argv[2]))


@@ -61,16 +61,70 @@
 ail.com%3E
+%0A# Updated by Bastien Abadie %3Cbastien@nextcairn.com%3E%0A#
  inspire
@@ -128,19 +128,16 @@
 ired by 
-%0A# 
 40-conve
@@ -206,20 +206,24 @@
 er uses 
-GcpU
+garmin-u
 ploader 
@@ -263,17 +263,16 @@
 onnect%0A#
- 
 %0A# To in
@@ -281,20 +281,23 @@
 all 
-GcpU
+garmin-u
 ploader
-:
 %0A#%0A#
@@ -314,20 +314,24 @@
 install 
-GcpU
+garmin-u
 ploader%0A
@@ -457,130 +457,8 @@
 s%0A#%0A
-# Then point the GUPLOAD environment variable at the path to your gupload.py%0A# script (defaults to /usr/bin/gupload.py)%0A#%0A
 # Do
@@ -618,21 +618,19 @@
 %0Aimport 
-errno
+sys
 %0Aimport 
@@ -631,24 +631,29 @@
 mport os
+.path
 %0Aimport 
 subproce
@@ -648,1201 +648,813 @@
 ort 
-subprocess%0Aimport sys%0A%0Agupload = os.getenv(%22GUPLOAD%22) or %22/usr/bin/gupload.py%22%0Aif not os.path.exists(gupload):%0A    sys.stderr.write(%22%25s didn't exist; ensure GcpUploader is installed, %22%0A                     %22and set GUPLOAD correctly if necessary.%5Cn%22)%0A    sys.exit(1)%0A%0Adef main(action, filename):%0A%0A    if action != %22DOWNLOAD%22:%0A        return 0%0A%0A    try:%0A        process = subprocess.Popen(%5Bgupload, filename%5D, stdout=subprocess.PIPE,%0A                                   stderr=subprocess.PIPE)%0A        (data, _) = process.communicate()%0A    except OSError as e:%0A        print(%22Could not send to Garmin%22, gupload, %5C%0A              %22-%22, errno.errorcode%5Be.errno%5D, os.strerror(e.errno))%0A        return -1%0A%0A    if process.returncode != 0:%0A        print(%22gupload.py exited with error code%22, process.returncode)%0A        return -1%0A%0A    if data.find(%22Status: SUCCESS %22) != -1:%0A        print(%22Successfully uploaded %25s to Garmin Connect%22 %25 filename)%0A        return 0%0A%0A    if data.find(%22Status: EXISTS %22) != -1:%0A        print(%22%25s already uploaded to Garmin Connect%22 %25 filename)%0A        return 0%0A%0A    print(%22Couldn't understand output from uploading %25s to Garmin Connect:%22 %25%0A          filename)%0A    print(data)
+logging%0A%0Atry:%0A    from garmin_uploader import logger%0A    from garmin_uploader.user import User%0A    from garmin_uploader.workflow import Activity%0Aexcept ImportError:%0A    print('Python package garmin_uploader is not available. Please install with pip install garmin-uploader')%0A    sys.exit(1)%0A%0A# Setup garmin uploader logger%0Alogger.setLevel(logging.INFO)%0A%0Adef main(action, filename):%0A    assert os.path.exists(filename)%0A%0A    if action != %22DOWNLOAD%22:%0A        return 0%0A%0A    # Auth with ~/.guploadrc credentials%0A    user = User()%0A    if not user.authenticate():%0A        logger.error('Invalid Garmin Connect credentials')%0A        return -1%0A%0A    # Upload the activity%0A    activity = Activity(filename)%0A    if not activity.upload(user):%0A        logger.error('Failed to send activity to Garmin')%0A        return -1%0A
 %0A   
@@ -1461,18 +1461,17 @@
  return 
--1
+0
 %0A%0Aif __n
@@ -1536,9 +1536,8 @@
 gv%5B2%5D))%0A
-%0A

Use garmin-uploader in 40-upload_to_garmin_connect script.