Accessing your Gmail contact list with Python

Written by Adrian Holovaty on June 20, 2004

Many people have been wanting to import their existing e-mail address books into Gmail; address-book import is a feature the Gmail folks say they're planning but haven't released yet. To fill that void, I've updated gmail.py, my Python Gmail interface (see previous entry), to add methods to retrieve from, add to, and delete from your Gmail address book. Now importing and exporting is possible.

It's still raw Python code at the moment; it just needs a snazzy user interface and bindings to popular e-mail address-book formats. Any Python coders out there want to collaborate? Leave a comment or contact me.

Comments

Posted by Gustavo Sverzut Barbieri on June 24, 2004, at 1:10 a.m.:

Small utility to migrate yahoo contacts to gmail.

Uses Yahoo! CSV (comma separated).

Notes:

- I ripped a small subset of GmailClient(), just the necessary to add contacts

- As I have a yahoo.com.br account, I get the CSV in brazilian portuguese. I added the possibility to map values in CSV to class YahooContact attributes, so if someone have an account in other country, please mail me the CSV header.

Get it from: http://gsbarbieri.sytes.net/export_contacts-yahoo_to_gmail.py

Posted by Adrian on June 24, 2004, at 1:18 a.m.:

Nice work, Gustavo! You might consider using the getpass module as a cleaner way of getting users' passwords without displaying them on the command line.

Posted by Gustavo Sverzut Barbieri on June 24, 2004, at 8:37 p.m.:

Hi Adrian,

Thanks for getpass. I'm coding a 0.2.

Aside from that I noticed that Yahoo! CSV is the same in every language, so I changed the CSV index map to be general and it can work with any CSV, give user map their field index to class attributes.

I'm having problems with connection to gmail... it's quite unstable right now and many times it hangs... There is a way to set the connection timeout somewhere?

Thanks,

Gustavo

Posted by Gustavo Sverzut Barbieri on June 24, 2004, at 10:54 p.m.:

Hi,

I solved the login issue... it was a mix of HTTP Errors and bad login (my fault ;), a way to detect the bad login is to check for the message:

"Username and password do not match."

Also, better you to check at least the first regular expression if it really find something (!= None) and then raise a GmailBadLogin or something like this.

But I still can't get the contacts :(

Posted by Gustavo Sverzut Barbieri on June 25, 2004, at 3:45 a.m.:

New version of Yahoo! to Gmail contacts migrator

Hello,

Just upgraded my script to migrate Yahoo! CSV contacts to Gmail. It's much more improved than the previous version... I'll stop naming it "Yahoo!" since it's general enough to support any CSV file out there, provided the CSV format is know (by now, just Yahoo is :(... just waiting for some from Hotmail, Outlook or something else).

CHANGELOG:

* Changed lang to format. Yahoo CSV is constant through languages, the lang is so translated in format, allowing to support multiple CSV formats.

* Better handling of exceptions.

* Bad login detection.

* Connection (Socket) timeout

* Support for non-ASCII chars (google uses utf-8!)

TODO:

* Map other CSV files (Hotmail, Outlook, ...)

* Web interface to help parents and friends to migrate

* Possible way to map non previously mapped CSV on demand (web interface)

Get it from:

http://gsbarbieri.sytes.net/export_contacts-yahoo_to_gmail.py

If you have some CSV and want it mapped, send me the header (barbieri@gmail.com)

PS: Adrian I added some interesting features, like the bad login recognition, socket timeout and the UTF-8 handling. Could you please look at it and merge those in your gmail.py?

Gustavo

Posted by Gustavo Sverzut Barbieri on June 25, 2004, at 4:09 a.m.:

Adrian,

If you are willing to change your GmailClient, here is the patch:

http://gsbarbieri.sytes.net/gmail-sock+utf+badlogin.diff

Thanks for your work,

Gustavo

Posted by Adrian on June 25, 2004, at 5:40 a.m.:

Gustavo: Thanks very much for your patch! I've modified gmail.py to include your improvements. (Note that I renamed the exception from GmailWrongLogin to LoginFailure.)

Posted by Gustavo Sverzut Barbieri on June 25, 2004, at 7:24 p.m.:

Great!

I'll keep using a reduced version in my script since I can keep it in one file

In this weekend I'll try to code a web interface for it...

Posted by Gustavo Sverzut Barbieri on June 30, 2004, at 9:50 p.m.:

New update (0.5) with improved support for other CSV.

Support:

- Yahoo!

- KAddressBook (KDE/Linux)

- Orkut

Added web interface! Just name your file ending with .cgi and it will output html code.

Get your copy at: http://gustavo.ltc.ic.unicamp.br/~gustavo/csv2gmail.py

Posted by Gustavo Sverzut Barbieri on June 30, 2004, at 10:28 p.m.:

New update (0.6) with Outlook support. (Right now just the csv files with name and email are supported).

http://gustavo.ltc.ic.unicamp.br/~gustavo/csv2gmail.py

Posted by Ian on July 3, 2004, at 12:42 a.m.:

I've posted my KNewGmail script.

It contains a slightly modified version of the newest gmail.py.

Posted by Phil on July 4, 2004, at 8:31 p.m.:

In case anyone is interested in checking out another Python/Gmail binding (begun before I found this site!) you can find it here:

<http://sourceforge.net/projects/libgmail/>

There's a few style & functionality differences, but you might find it interesting.

It includes a rough demo to archive mail messages.

--Phil.

Posted by anonymous on January 13, 2005, at 9 p.m.:

Is it possible for me get yahoo login password from a user and somehow use it to import yahoo address book of that user using a script. Your help will be appreciated.

Regards

Muhammad Salman

Posted by Chris on May 7, 2005, at 4:28 a.m.:

You can login into to yahoo using this http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/391929 as a template. Make sure you get the correct versions of ClientCookie and ClientForm.

Just replace the initial login url for address.yahoo.com and make a request to http://address.mail.yahoo.com/yab/us/Yahoo.csv?.rand=111111111&A=Y&Yahoo.csv

Chris

Posted by anonymous on March 27, 2006, at 9:36 p.m.:

http://gsbarbieri.sytes.net/export_contacts-yahoo_to_gmail.py

is 404 - not found

Comments have been turned off for this page.