Hide keyboard shortcuts

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 

2 

3from django.conf import settings 

4import hashlib 

5import os 

6from urllib.request import urlretrieve 

7 

8# Exports 

9 

10__all__ = ( 

11 "get_gravatar_url", 

12) 

13 

14# Functions 

15 

16 

17def get_gravatar_url(email, size=60): 

18 """Get the Gravatar URL for the given email address. 

19 

20 :param email: The email address. 

21 :type email: str 

22 

23 :param size: The desired size of the image. 

24 :type size: int 

25 

26 :rtype: str 

27 

28 """ 

29 encoded_email = email.strip().lower().encode("utf-8") 

30 email_hash = hashlib.md5(encoded_email).hexdigest() 

31 

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) 

36 

37 url = "https://secure.gravatar.com/avatar/%s.jpg?s=%s" % (email_hash, size) 

38 urlretrieve(url, filename=path) 

39 

40 return "%savatars/%s.jpg" % (settings.MEDIA_URL, email_hash) 

41 

42 # return "https://secure.gravatar.com/avatar/%s.jpg?s=%s" % (email_hash, size)