Gravatars in Python 2.5

January 17, 2008 | Posted by Demitrious Kelly

Cory Wright wrote in with his method for generating Gravatar URLs in python (seems to require 2.5 or better.) It’s truly great when we get submissions like this. Not only do we get to see how people are using Gravatar, but we get to share that information to help other people who might be thinking about using Gravatar themselves!

# import code for encoding urls and generating md5 hashes
import urllib, hashlib

# Set your variables here  
email = "Someone@somewhere.com"
default = "http://www.somewhere.com/homsar.jpg"
size = 40

# construct the url 
gravatar_url = "http://www.gravatar.com/avatar.php?" 
gravatar_url += urllib.urlencode({'gravatar_id':hashlib.md5(email.lower()).hexdigest(), 'default':default, 'size':str(size)})

11 Responses

  1. To get it to work in python version previous to 2.5, import the MD5 this way:

    try:
    from hashlib import md5
    except ImportError:
    import md5
    md5 = md5.new

  2. Argh, wordpress ate my indents. Also, for the alternative import to work “hashlib.md5” needs to be changed to “md5” in the rest of the code.

  3. […] stata scritta l’implementazione di Gravatar in Python: per chi serve e si vuole risparmiare 11 righe di codice. « […]

  4. Monica says:

    test. please delete

  5. silveira says:

    Very good piece of code.
    Gravatar is very easy to implement with Python. I’m using in some projects. I hope see a good integration of Gravatar with WordPress 2.4. 😉

  6. hugin says:

    Good luck, silveira, since WP 2.4 won’t be released. 😉

  7. JJ says:

    Isn’t the md5 encoding of e-mail addresses vulnerable to harvesting by ne’er-do-wells?

  8. nsanenoob says:

    Well written.

  9. Omarm says:

    ohh..thanks

  10. postalesdelsur says:

    Thanks..

an    joint