Sunday, March 18, 2012

import your google contacts to bbdb

I'm a gnus user, but I have an android cellphone...

Here's a quick and dirty way to import your google contacts list to bbdb (only name and e-mail addresses).


  1. A. Get googlecl ("sudo apt-get install googlecl" in debian).
    Remember to change the default settings in ~/.googlecl/config for [Contacts]'s list_style to:
    list_style = name,email
    
    and do
    google contacts list >some_temp_file.txt
    B. Or export your contacts to CSV in google contacts (more -> export), and leave only the Name and e-mail address fields
  2. in emacs, open ~/.bbdb
  3. Press M-> to go the end of the file, and C-u M-! to insert the output of a command
  4. Run the script below like this:
  5. google_contacts_to_bbdb.sh some_temp_file.txt
  6. check your contacts are in there, and restart emacs or reload bbdb
  7. Test C-m to write an e-mail and write someone's name or e-mail address, press TAB for the next e-mail address of the same person

Source for google_contacts_to_bbdb.sh:
#!/bin/bash

FILE=$1
DATE=`date +%Y-%m-%d`

awk -F','  '{
  name=$1;
  split($2,emails_dirty," ");

  newline_counter=0;
  newline[newline_counter++] = $1;
  for (field in emails_dirty){
      match(emails_dirty[field],/[^ ]*@[^ ;]*/,clean_email);

      if (clean_email[0] != "") {
   newline[newline_counter++] = clean_email[0];
      }
  }

  if (newline[1] != "") {

      #A outputs name in bbdb format
      printf "[\"%s\" ", newline[0]; 
      printf " nil nil nil nil nil (";

      #B printing name for csv output 
#      printf "%s, ", newline[0];

      for (i=1; i< length(newline); i++) { #list of e-mails
   # A to output email addresses quoted
   if (i != (length(newline) -1 )){
       printf "\"%s\" " , newline[i]; 
   }
   else{
       printf "\"%s\"" , newline[i]; 
   }
   # B to output emails pretty
#   printf "%s " , newline[i];
      }
      
      # A for closing part in line for bbdb fields
      printf ") ((creation-date . \"'$DATE'\") (timestamp . \"'$DATE'\")) nil]";
      printf "\n";
  }
  delete newline;
}' $FILE

#sample output
#["my-name3" nil nil nil nil nil ("my3@email.com") ((creation-date . "2012-03-17") (timestamp . "2012-03-17")) nil]