|< Day Day Up >|
Hack 80. Program Gmail
Try your hand at writing an alternative interface to Gmail using the freely available Python, Perl, PHP, Java, and .NET libraries and API frameworks.
For a glimpse of the Gmail engine and protocol underlying the official Gmail interface and the lion's share of the unofficial APIs and libraries written to the service, take a gander at Johnvey Hwang's "About the Gmail engine and protocol" (http://johnvey.com/features/gmailapi/; scroll down).
The libgmail (http://libgmail.sourceforge.net; GNU Public License 2.0/PSF) Python binding for Gmail provides a nice, clean interface (as you'd expect from Python) to your Gmail account.
Libgmail bundles a lovely set of useful example applications, usable right out of the box:
22.214.171.124 Installing the hack
Installation is just a matter of downloading and unpacking the library (http://sourceforge.net/project/showfiles.php?group_id=113492, or click the Downloads link on the libgmail home page) and putting it someplace findable by Python.
126.96.36.199 The code
Libgmail sports much functionality, each with its own rather self-explanatory function name: getMessagesByQuery, getQuotaInfo, getLabelNames, getMessagesByLabel, getrawMessage, and getUnreadMsgCount. Leaf through libgmail.py for the kind of details only a programmer could love.
Here's a snippet of sample code showing off login, folder selection, and strolling through email—thread by thread, message by message:
#!/usr/bin/python # libgmail_example.py # A simple example of the libgmail Python binding for Gmail in action # http://libgmail.sourceforge.net/ # # Usage: python gmail_in_python.py import libgmail # Login gmail = libgmail.GmailAccount('email@example.com', '12bucklemyshoe') gmail.login( ) # Select a folder, label, or starred messages--in this case, the Inbox folder = gmail.getMessagesByFolder('inbox') # Stroll through threads in the Inbox for thread in folder: print thread.id, len(thread), thread.subject # Stroll through messages in each thread for msg in thread: print " ", msg.id, msg.number, msg.subject
Replace firstname.lastname@example.org and 12bucklemyshoe with your Gmail email address and password. Instead of inbox, you can use any of Gmail's standard folder names or your custom labels—e.g., starred, sent, or friends.
Save the code to a file called gmail_in_python.py.
188.8.131.52 Running the hack
Run the gmail_in_python.py script on the command line, like so:
184.108.40.206 Hacking the hack
Swap in a call to getMessagesByQuery and you now have a command line right to the Gmail search engine:
#folder = gmail.getMessagesByFolder('inbox') folder = gmail.getMessagesByQuery('from:rael subject:Howdy')
Here are the results of this little switch:
220.127.116.11 See also
gmail.py (http://www.holovaty.com/blog/archive/2004/06/18/1751) is a simple Python interface to Gmail, focusing on exporting raw messages for backup and import.
Mail::Webmail::Gmail (http://search.cpan.org/~mincus/Mail-Webmail-Gmail-1.00/lib/Mail/Webmail/Gmail.pm) provides Perl hackers a programmatic interface to Gmail. You'll find full POD documentation and more sample code than you can shake a stick it in the module and online at the aforementioned URL.
A Comprehensive Perl Archive Network (CPAN) search for gmail (http://search.cpan.org/search?query=gmail&mode=all) at the time of this writing finds three more Perl Gmail libraries: Mail::Webmail::Gmail, WWW::GMail, and WWW::Scraper::Gmail.
GMailer or libgmailer (http://gmail-lite.sourceforge.net; GNU Public License) is a PHP library for interacting with Gmail by way of the curl library (http://www.php.net/curl) with SSL support (http://www.openssl.org). It is the engine underlying gmail-lite Hack #76, an HTML-only interface to Gmail.
For full libgmailer documentation and plenty of sample code, leaf through the online documentation (http://gmail-lite.sourceforge.net/docs.html).
G4j or GMail API for Java (http://g4j.sourceforge.net; GNU Public License) is a Java interface to Gmail. The API comes with GMailer for Java, a basic GUI frontend to Gmail built on top of G4j.
Full documentation in Javadoc HTML is available online (http://g4j.sourceforge.net/doc).
The Gmail Agent API (http://johnvey.com/features/gmailapi; GNU Public License) is a .NET foundation for programming to Gmail. A full package of source for the API itself; the Gmail Agent Applet, a proof of concept Windows frontend to Gmail; and associated Windows Installer projects are available for download.
There's also full documentation available in both HTML format (http://johnvey.com/features/gmailapi/docs) and as Windows Help.
|< Day Day Up >|