Sonntag, 2. Februar 2014

GMail contacts in alpine

I was wondering, if there is a possibility to import all my GoogleMail contacts in alpine. It was easy to figure out that alpine stores contacts per default in a hidden file called .addressbook in the home directory:
~/.addressbook
Each line in this file represents an addressbook entry in three tab-separated columns, like shown in the example below.
nickname (tab) full_name (tab) email-address
Albert_Einstein      Einstein, Prof. Dr. Albert     albert.einstein@patentamt.ch
Werner_Heisenberg    Heisenberg, Prof.Dr. Werner    werner.heisenberg@uni-leipzig.de
Max_Planck           Planck, Prof. Dr. Max          max.planck@uni-berlin.de

...
After some research I found a widely spread example script that makes use of the python module gdata interfacing Google's Data API to query the contacts stored in a GMail account. I modified and extended it to fit my needs in terms of exporting the contacts in alpine conform syntax. You can find the script here (you need the python gdata module as a prerequisite). The scripts takes your GMail address as a command line parameter and asks for your password via python's getpass module. After authentication it queries for all entries in your contacts database and prints the parsed information as lines of text in the right format. To get the contacts into your alpine .addressbook simply run
python gmail2alpine.py  >> .addressbook
I choose the nickname field to be the full name joined with underscores (like in the example above) and ignoring prefixes like "Prof. Dr.", but feel free to edit the corresponding method in the script to your needs.