Gravatars in Python 2.5

January 17, 2008 | Posted by apokalyptik

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

12 Responses »

  1. Anders Dahnielson Says:
    January 17, 2008 at 11:18 am

    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. Anders Dahnielson Says:
    January 17, 2008 at 10:23 pm

    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. Gravatar scritto in Python ~ Davide Bocci in... Says:
    January 19, 2008 at 8:42 am

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

  4. Monica Says:
    January 19, 2008 at 5:26 pm

    test. please delete

  5. silveira Says:
    January 20, 2008 at 10:47 pm

    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:
    January 21, 2008 at 2:14 pm

    Good luck, silveira, since WP 2.4 won’t be released. ;-)

  7. JJ Says:
    January 26, 2008 at 4:58 pm

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

  8. alexander Says:
    February 3, 2008 at 4:07 am

    yayayya

  9. nsanenoob Says:
    May 5, 2008 at 1:10 am

    Well written.

  10. Omarm Says:
    May 22, 2008 at 1:55 am

    ohh..thanks

  11. postalesdelsur Says:
    June 2, 2008 at 6:18 pm

    Thanks..

RSS feed for comments on this post. TrackBack URI

Leave a comment

an    joint