#!/bin/bash # Copyright (c) 2009 Braiden Kindt # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. set -o pipefail # create a temporary file # delegate to "tempfile" command if it exists function tempfile { if [ -x /bin/tempfile ] ; then /bin/tempfile else echo "/tmp/$1" fi } # login in to garmin saving cookies # to file pointed to by $COOKIES function login { if [ -z "$USER" -o -z "$PASSWORD" ] ; then return 1 fi # 1) load sign page, and store any intial sesison cookies provided # 2) post the login data # 3) call the username json service to see if login succeeded curl \ --location \ --cookie "$COOKIES" \ --cookie-jar "$COOKIES" \ --output /dev/null \ "http://connect.garmin.com/signin" ;\ curl \ --location \ --cookie "$COOKIES" \ --cookie-jar "$COOKIES" \ --data "login=login&login%3AloginUsernameField=$USER&login%3Apassword=$PASSWORD&login%3AsignInButton=Sign+In&javax.faces.ViewState=j_id1" \ --output /dev/null \ "https://connect.garmin.com/signin" ;\ curl \ --location \ --cookie "$COOKIES" \ --cookie-jar "$COOKIES" \ --output - \ "http://connect.garmin.com/user/username" |\ grep -i "$USER" if [ $? == 0 ] ; then return 0 else return 1 fi } # send the specified tcx file to garmin # assumes we're already logged in function sendtcx { TCX=$1 curl \ --location \ --write-out "STATUS:%{http_code}" \ --cookie "$COOKIES" \ --cookie-jar "$COOKIES" \ --form "responseContentType=text%2Fhtml" \ --form "data=@$TCX" \ --output - \ "http://connect.garmin.com/proxy/upload-service-1.1/json/upload/.tcx" |\ grep "STATUS:200" } # given one or more gmn files convert to # a single tcx and send. # assumes we're already logged in function sendgmn { RET=0 TCX=`tempfile garmin-data.tcx` if `dirname $0`/gmn2tcx "$@" > $TCX ; then if sendtcx $TCX ; then rm -f "$@" else echo "Failed to upload tcx file to garmin." RET=1 fi else echo "Failed to convert gmn file to tcx." RET=2 fi rm -f $TCX return $RET } # login cookies are saved here COOKIES=`tempfile garmin-cookies.txt` # where to find the gmn2tcx conversion script GMN2TCX=`dirname $0`/gmn2tcx # where are .gmn files saved # GARMIN_SAVE_RUNS is used by garmin-tools if [ -z "$GARMIN_SAVE_RUNS" ] ; then GARMIN_SAVE_RUNS=`pwd` fi # We look for pending direct # which should contian symlinks of # .gmn ready for upload. if [ -d "$GARMIN_SAVE_RUNS/pending" ] ; then PENDING=`ls "$GARMIN_SAVE_RUNS"/pending/*.gmn 2>/dev/null` fi FAILED=false # if there anything to upload if [ ! -z "$PENDING" ] ; then # attempt to login to garmin if login ; then cd "$GARMIN_SAVE_RUNS" # and upload the queue for gmn in $PENDING ; do sendgmn $gmn || FAILED=true done cd - >/dev/null else echo "Unable to login to Garmin Connect!" echo "Make sure you specified user name an password." echo "USER=myusername PASSWORD=***** $0" fi fi if $FAILED ; then echo "FAILED to upload one or more activities:" ls $GARMIN_SAVE_RUNS/pending fi rm -f $COOKIES $FAILED