Gravatar

What is it?

It's a Python module that wraps the public API available from gravatar.com. To quote the gravatar.com site:

A gravatar, or globally recognized avatar, is quite simply an 80×80 pixel avatar image that follows you from weblog to weblog appearing beside your name when you comment on gravatar enabled sites.

Download

Download the latest version of gravatar.py

Installation

Just place the gravatar.py module in Python's site-package folder (or any other folder listed in your PYTHONPATH enviroment variable).

Sample usage

>>> import gravatar
>>> gravatar.get_uri('john@example.com')
'http://www.gravatar.com/avatar.php?gravatar_id=d4c74594d841139328695756648b6bd6'

Then put returned URI into an HTML image tag, like this:

<img alt="avatar" src="your-uri-goes-here" width="80" height="80" />

Maybe you want ot give a default URI if requested avatar don't match your web site rating criteria or cannot be found in gravatar.com database? Then use the default argument in the following way:

>>> gravatar.get_uri('john@example.com', default='http://example.com/blah.jpg')
'http://www.gravatar.com/avatar.php?default=http%3A%2F%2Fexample.com%2Fblah.jpg&gravatar_id=d4c74594d841139328695756648b6bd6'

Wanna see how the avatar looks like in your browser?

>>> import webbrowser
>>> webbrowser.open(gravatar.get_uri('john@example.com'))

You may want to grab actual bytes for the avatar image. Use the get_image function:

>>> mime, bytes = gravatar.get_image('john@example.com')
>>> print mime
image/jpeg

get_image function will issue a HTTP request on gravatar.com, grab the images bytes and return them as a (mime, bytes) tuple, common MIME types are image/gif, image/png or image/jpeg.

Notes

If you have Joe Gregorio's httpcache module installed in your system get_image the function will use that to cache images on disk, sparing GET requests to the remote server. For further details check the httpcache project page.