With a little side of applesauce...

Friday, September 12, 2008

Gmail contact import - using a delimited file

Here are shell scripts to import the contacts in the pipe-delimited file mentioned in my last post, "Meeting Maker - prepare export for pushing into gmail".

As is my style, I split them into two files.

1. run.sh - the "user interface", which handles the options, and calls the appropriate functions
2. common.sh - the library of functions which is included in run.sh

St. Olaf College has been great about giving me the opportunity to work on these gmail migrations, as well as letting me share these under the GPL.

run.sh:
#!/bin/sh
####################
#
# Copyright 2008 Shannon Eric Peevey <speeves@stolaf.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#####################
#
# Contacts Import/Export script
# - exports contacts from the existing systems, and re-imports from the gmail systems.
#
####################

# load our common library
. ./common.sh

### Initialize variables
TMP="/tmp/"
IMPORT_FILE_PATH=""
DOMAIN="example.com"


usage="
---------------------------
Usage: ${0} [-hadbupo]

Help Options
-h This message

Actions
-a Action to perform
(
i|contactstogoogle - import contacts into gmail
)

Variables
-u user account email address
-p user account password
-o import file (used to import contacts into gmail - see README.txt for more information)"



while getopts "a:db:u:p:o:h" OPT
do
case $OPT in
a )
ACTION=$OPTARG
;;
u )
USERNAMETMP=$OPTARG
USERNAME=$(echo $USERNAMETMP | cut -d\@ -f1)
;;
p )
PASSWORD=$OPTARG
;;
o )
IMPORT_FILE=$OPTARG
;;
h )
echo "$usage"
exit 1
;;
\?)
echo "$usage"
exit 1;;
esac
done
# remove the flags from $@
shift $((${OPTIND} - 1))

# grab the action that we need to perform
case $ACTION in
i|contactstogoogle )
contactstogoogle
;;
esac



common.sh:
#!/bin/sh
####################
#
# Copyright 2008 Shannon Eric Peevey <speeves@stolaf.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#####################
#
# Contacts Import/Export Library
# - exports contacts from the existing systems, and re-imports from the gmail systems.
#
####################

contactstogoogle()
{
# set tmpfolder
CONTACTFILE="${TMP}${USERNAME}.xml"
LOG_DATE=$(date +%d/%b/%Y:%X\ %z)

# let's wrap our migration steps here to simplify the function above
authtoken=$(getauthtoken)

### now we handle the import file
IMPORT_FILE_LOCATION="${IMPORT_FILE_PATH}${IMPORT_FILE}"
while read abook_line
do
contact_name=$(echo $abook_line | cut -d\| -f1)
contact_email=$(echo $abook_line | cut -d\| -f2)
contact_address=$(echo $abook_line | cut -d\| -f3)
contact_phone_home=$(echo $abook_line | cut -d\| -f4)
contact_phone_work=$(echo $abook_line | cut -d\| -f5)
contact_phone_mobile=$(echo $abook_line | cut -d\| -f6)
notes="$(echo $abook_line | awk -F\| '{print $7 }')"

if [ "$contact_name" != "" ]; then
usertitle="${contact_name}"
else
usertitle="Unnamed Contact"
fi

if [ "${contact_email}" != "" ]; then
xmlcreate
postentry
fi
done < ${IMPORT_FILE_LOCATION}

}


getauthtoken()
{
# use curl to get auth token
if [ "$USERNAME" = "" ] || [ "$PASSWORD" = "" ]; then
echo "$usage"
exit 1
else
curl -s https://www.google.com/accounts/ClientLogin -d Email=${USERNAME}@${DOMAIN} -d Passwd=${PASSWORD} -d accountType=HOSTED -d source=Google-cURL-Example-${USERNAME} -d service=cp | grep Auth
fi
}

postentry()
{
# use curl to post the xml batch feed that was created
# http://www.google.com/m8/feeds/contacts/liz%40gmail.com/full
curl -v -s --url http://www.google.com/m8/feeds/contacts/${USERNAME}@${DOMAIN}/full --header "Authorization: GoogleLogin ${authtoken}" --header "Content-Type: application/atom+xml" --data "@${CONTACTFILE}"
}


xmlcreate()
{
echo "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>" > ${CONTACTFILE}
echo " <atom:category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />" >> ${CONTACTFILE}
echo " <atom:title type='text'>${usertitle}</atom:title>" >> ${CONTACTFILE}
echo " <atom:content type='text'>${notes: -Notes}</atom:content>" >> ${CONTACTFILE}
echo " <gd:email rel='http://schemas.google.com/g/2005#work' address=\"${contact_email}\" />" >> ${CONTACTFILE}
echo " <gd:email rel='http://schemas.google.com/g/2005#home' address=\"${contact_email}\" />" >> ${CONTACTFILE}
if [ "${contact_phone_home}" != "" ]; then
echo " <gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'>${contact_phone_home}</gd:phoneNumber>" >> ${CONTACTFILE}
fi
if [ "${contact_phone_work}" != "" ]; then
echo " <gd:phoneNumber rel='http://schemas.google.com/g/2005#work' >${contact_phone_work}</gd:phoneNumber>" >> ${CONTACTFILE}
fi
if [ "${contact_phone_mobile}" != "" ]; then
echo " <gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile' >${contact_phone_mobile}</gd:phoneNumber>" >> ${CONTACTFILE}
fi
if [ "${contact_address}" != "" -a "${contact_address}" != " " ]; then
echo " <gd:postalAddress rel='http://schemas.google.com/g/2005#other' primary='true'>${contact_address}</gd:postalAddress>" >> ${CONTACTFILE}
fi
echo "</atom:entry>" >> ${CONTACTFILE}
}

2 comments:

Luciano said...

Cheers from a fellow blogger ;)

speeves said...

Hey yo! I'll pull your blog in and follow your doings :) thanks for stopping by!