Method to import contacts from CardMunch to Google Contacts

The script below is my method of importing contacts from CardMunch to Google Contacts.  It could be useful for any address book which imports VCF files.
It automates part of the work, but much still is manual.

CardMunch is an iOS app and service purchased by LinkedIn that digitizes pictures of business cards into an address book, with export (one by one) to iOS contacts, and to e-mail.
Since I am using Google Contacts, I want to automate the transfer as much as possible.  I feel it is still easier than typing cards in by hand, although I am not confident of that.



Prerequisites:
  • A basic comfort with bash scripting, enough to customize the script below without fear that it will format your drive.
  • Equivalent of: apt-get install mpack procmail perl coreutils

If not using procmail to filter mail, one can manually save to the separate mail file instead.  However, procmail is still needed for the formail program.


bash script cardmunch_to_vcf.sh
---
       
#!/bin/bash
#
# cardmunch_to_vcf.sh
# Import digitized business cards from CardMunch to Google Contacts
#  minimizing manual interaction.
#
# Improvements desired
#  See: Better if:
# Bugs:
#  See: Bug?:

# Configuration
#  Pending Import directory
#IMPORT_PENDING_DIR=~/contacts_new/
IMPORT_PENDING_DIR=~/Data/AddressBook/Import_Needed/
#ARCHIVE_DIR=~/contacts/
ARCHIVE_DIR=~/Data/AddressBook/Archive/
#Incoming Mail file with contact attachments
INC_MAIL=~/Mail/incoming.cardmunch.eml

# Manual procedure intermixed with code at start and end of this file:
# scan and submit with iPad and CardMunch
# wait for processing (1 day?)
# for each contact, send to e-mail from iPad CardMunch app
#  in my case, to my university account from my gmail account.
# procmail recipe then saves in $INC_MAIL

# Better if: check existence of $INC_MAIL
# Better if: check existence of needed programs, such as munpack, formail, prename

# in a tempdirectory 
TMP_DIR=$(mktemp --tmpdir --directory cardMunch2GContacts_XXXX )
# 
# cd /tmp/cardMunch2GContacts_tgYg
cd /tmp/
# Better if: check for existance of TMP_DIR and that correctly in it.
cd $TMP_DIR

#Put vcf and jpg files in $TMP_DIR , possibly along with .desc files
# get attachments from e-mail(s) in $INC_MAIL
#  CardMunch_FullName.vcf
#  CardMunch_FullName.jpg
cat $INC_MAIL |formail -s munpack -C $TMP_DIR 

# if there are 2 of same name, such as a company named card.
#  munpack will make in format of name.jpg.1 and name.vcf.2 
# Better if: Try to prevent issue, and recover by renaming
#  Better if check for existance, and only do it if needed.
# Not sure of bash scripting method to do while loop based on file existance.
#If were in DOS, would be: if exist ( *.1) // if exist ( *.2)
#while ( *.[1-9][0-9]) ...
prename 's/^CardMunch_(.*).vcf.([1-9])/CardMunch_$1_$2.vcf/' CardMunch_*.vcf.[1-9]
prename 's/^CardMunch_(.*).jpg.([1-9])/CardMunch_$1_$2.jpg/' CardMunch_*.jpg.[1-9]
# Bug?: Seems numbers may not match up correctly in this case, however.

# rename CardMunch_*.vcf *_via_CardMunch.vcf
# Perhaps Better if rely on different rename program than perl?
# Best if use whichever installed, and if none, just leave as is without error.
prename 's/^CardMunch_(.*).vcf/$1_via_CardMunch.vcf/' CardMunch_*.vcf
# rename CardMunch_*.jpg *_via_CardMunch.jpg
prename 's/^CardMunch_(.*).jpg/$1_via_CardMunch.jpg/' CardMunch_*.jpg
# Better if rename to:
#  full_name-Company-TitleOrRole_via_CardMunch.Vcf 
#  &
#  full_name-Company-TitleOrRole-Card_via_CardMunch.jpg 

# Better if: edit VCF phone numbers
#  seems to be cardmunch standard: (###) ###-####
#  my standard: (###)###-####

# Better if: edit each VCF note
#  from: NOTE:Card Scanned on 09-21-2013
#  to: NOTE:Card Scanned on MM-DD-YYYY by CardMunch \n
#   See picture of business card in jpg file.
#  to: NOTE:Card Scanned on MM-DD-YYYY by CardMunch \n
#   See picture of business card in file FullName_via_CardMunch.jpg dated MM-DD-YYYY.
# Better if: edit each VCF note, adding: suspect met on MM-DD-YYYY, at 'event'.
#  where 'event' is longest not recurring event from calendar on that day.

# concatenate all new vcf files: 
cat *.vcf > export.vcf
# Better if to YYYY-MM-DD-via_CardMunch.vcf , 
#  which should be doable by `date --...` but I am too lazy at moment for a
#  temporary file.

# store to import to google
mv -i export.vcf $IMPORT_PENDING_DIR
# Maybe Better if instead of move /overwrite, just add to: cat *.vcf >> $IMPORT_PENDING_DIR/export.vcf
# archive vcf & jpg  files
mv -i *_via_CardMunch.jpg *_via_CardMunch.vcf $ARCHIVE_DIR

## Clean up
rm CardMunch_*.desc
#Should be not needed: #rm -i *
# remove temporary directory
cd ..
rmdir $TMP_DIR
# remove from mail file.
rm $INC_MAIL

# ?Not really needed, since cleared out by end of script?
unset IMPORT_PENDING ARCHIVE_DIR INC_MAIL TMP_DIR

## Next manual steps
# next import to google by manual web interaction
# Better if: automate by web post URL or googlecli or some method.
# Better if: if can fully automate, then 
# create a daily cronjob to batch process.

cat << EOF
Now go to: http://www.google.com/contacts/ 
import export.vcf from $IMPORT_PENDING_DIR
If satisfied, 
 remove concatenated vcf file export.vcf
 remove from source e-mail, such as mtu.edu

Optional in Google Contacts:
find group auto-tagged with "Imported M/D/YY" today's date
tag with more groups, such as: 
 Imported_from_CardMunch 
 InCardMuchAppIOS
 InGoogleContacts
optionally tag with other groups: Business, etc
compare jpgs to imported contacts, and make desired edits.
Make any extra notes, such as where / when met.
EOF
read -n 1 -p "Press any key to quit..."
       
---

.procmailrc fragment :
---
       
MAILDIR=$HOME/Mail      #you'd better make sure it exists
FORMAIL=/usr/bin/formail

# CardMunch by Linked-In
INCLUDERC=$HOME/bin/Mail/pm-CardMunch.rc
       
---

pm-CardMunch.rc
---
       
# Handle e-mails from
# CardMunch by Linked-In
#  focused on finding digitizing business cards
# Allows forwarding jpg & vcf of card from app

# Needed variables: FORMAIL, MAILDIR
# Usage:
# INCLUDERC=$HOME/bin/Mail/pm-CardMunch.rc


# Better if:
# Improve detection of CardMunch forwards
#  could search and Detect by attachments CardMunch_*.vcf CardMunch_*.jpg
#  Perhaps a body search for
#  name=CardMunch_
#  filename=CardMunch_

:0:
* ^Subject:.*Forwarding .*s Contact Info
| $FORMAIL -A"X-Sorted: Contact Management" >>$MAILDIR/incoming.cardmunch.eml

       
---

No comments: