Friday, September 07, 2007

Gmail Contact List

I was searching the net for a way to get the gmail contact list and came across libgmail
The demo is pretty sweet, after untarring the package, just type >
python libgmail.py
and it will log you in and present you with your list of gmail folders to choose from.
Since there was no option for getting the contact list, i thought of putting that in by following the general mechanism used for getting the emails. But I was pleasantly surprised to find a getContacts() method in the lib that was not exposed in the demo.

So I added that as the last option to the package, so it looks like this:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
STANDARD_FOLDERS = [U_INBOX_SEARCH, U_STARRED_SEARCH,
U_ALL_SEARCH, U_DRAFTS_SEARCH,
U_SENT_SEARCH, U_SPAM_SEARCH]

// add this right after STANDARD_FOLDERS def (around line 61 in libgmail.py)
CONTACT_LIST = ["contacts"]
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// around line 1609
searches = STANDARD_FOLDERS + ga.getLabelNames() + CONTACT_LIST
name = None
while 1:
try:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// around line 1622
name = None
if name in STANDARD_FOLDERS:
result = ga.getMessagesByFolder(name, True)
elif name not in CONTACT_LIST:
result = ga.getMessagesByLabel(name, True)
else:
contacts = ga.getContacts()
print "No contacts: %d" % contacts.getCount()
for entry in contacts.getAllContacts():
print " %s %s" % (entry.getName(), entry.getEmail())
result = []
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


This will print your contacts when you pick the last option (after the folder names)
I have submitted the change to libgmail folks - hope they put it in the official package.

There is also an alternate hack to get some script that has your contacts if you're already logged in. Just hit this url: http://video.google.com/data/contacts?out=js&max=500&psort=Affinity courtsey Garett Rogers.

1 comment:

Cooper B said...

This is a great post tthanks