Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4import hashlib
5import os
6from urllib.request import urlretrieve
8# Exports
10__all__ = (
11 "get_gravatar_url",
12)
14# Functions
17def get_gravatar_url(email, size=60):
18 """Get the Gravatar URL for the given email address.
20 :param email: The email address.
21 :type email: str
23 :param size: The desired size of the image.
24 :type size: int
26 :rtype: str
28 """
29 encoded_email = email.strip().lower().encode("utf-8")
30 email_hash = hashlib.md5(encoded_email).hexdigest()
32 # IDEA: The gavatar could be cached locally to save a URL call.
33 path = os.path.join(settings.MEDIA_ROOT, "avatars", "%s.jpg" % email_hash)
34 if os.path.exists(path):
35 return "%savatars/%s.jpg" % (settings.MEDIA_URL, email_hash)
37 url = "https://secure.gravatar.com/avatar/%s.jpg?s=%s" % (email_hash, size)
38 urlretrieve(url, filename=path)
40 return "%savatars/%s.jpg" % (settings.MEDIA_URL, email_hash)
42 # return "https://secure.gravatar.com/avatar/%s.jpg?s=%s" % (email_hash, size)