diff --git a/lib/gravatar/utils.py b/lib/gravatar/utils.py index be3bd238..08d789fb 100644 --- a/lib/gravatar/utils.py +++ b/lib/gravatar/utils.py @@ -2,6 +2,7 @@ import hashlib # Third Party (PyPI) Imports +import requests import six.moves.urllib as urllib # Django Imports @@ -11,7 +12,7 @@ # http://en.gravatar.com/site/implement/images/ GRAVATAR_URL_PROTOCOL = 'https' if settings.SECURE_SSL_HOST or settings.SECURE_SSL_REDIRECT else 'http' GRAVATAR_URL_PREFIX = getattr(settings, 'GRAVATAR_URL_PREFIX', '%s://%s' % (GRAVATAR_URL_PROTOCOL, 'www.gravatar.com',)) -GRAVATAR_DEFAULT_IMAGE = getattr(settings, 'GRAVATAR_DEFAULT_IMAGE', 'mm') +GRAVATAR_DEFAULT_IMAGE = getattr(settings, 'GRAVATAR_DEFAULT_IMAGE', 'mp') GRAVATAR_DEFAULT_SIZE = 80 @@ -24,7 +25,11 @@ def get_gravatar_hash(email): return gravatar_hash -def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE): +def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE): + return build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE) + + +def build_gravatar_url_for_email(email, size=GRAVATAR_DEFAULT_SIZE, default=GRAVATAR_DEFAULT_IMAGE): """ https://en.gravatar.com/site/implement/images/ """ @@ -40,7 +45,14 @@ def get_gravatar_for_email(email, size=GRAVATAR_DEFAULT_SIZE): url += urllib.parse.urlencode( { 's': str(size), - 'default': GRAVATAR_DEFAULT_IMAGE, + 'default': default, } ) return url + + +def has_gravatar(email): + url = get_gravatar_for_email(email, default='404') + response = requests.get(url) + has_gravatar = response.status_code == 200 + return has_gravatar