Gravatars in Python 2.5

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)})

Subscribe and get notified about the latest Gravatar news…

Join 15.3K other subscribers

Comments

11 responses to “Gravatars in Python 2.5”

  1. Anders Dahnielson Avatar

    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

    Like

  2. Anders Dahnielson Avatar

    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.

    Like

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

    Like

  4. Monica Avatar
    Monica

    test. please delete

    Like

  5. silveira Avatar

    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. 😉

    Like

  6. hugin Avatar
    hugin

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

    Like

  7. JJ Avatar
    JJ

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

    Like

  8. nsanenoob Avatar
    nsanenoob

    Well written.

    Like

  9. Omarm Avatar
    Omarm

    ohh..thanks

    Like

  10. postalesdelsur Avatar
    postalesdelsur

    Thanks..

    Like

%d