diff --git a/README.rst b/README.rst index 23338ef..aa6aac3 100644 --- a/README.rst +++ b/README.rst @@ -32,14 +32,14 @@ Getting the user profile image:: from libgravatar import Gravatar g = Gravatar('myemailaddress@example.com') g.get_image() - 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' + 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' Getting the profile URL:: from libgravatar import Gravatar g = Gravatar('myemailaddress@example.com') g.get_profile() - 'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' + 'https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' Gravatar XML-RPC API @@ -70,3 +70,4 @@ Author and contributors * Caleb FANGMEIER (`@cfangmeier `_) * Rarm NAGALINGAM (`@snowjet `_) * Manan (`@mentix02 `_) +* Gareth Simpson (`@xurble `_) diff --git a/libgravatar/__init__.py b/libgravatar/__init__.py index 01a09ea..184f2fa 100644 --- a/libgravatar/__init__.py +++ b/libgravatar/__init__.py @@ -66,13 +66,13 @@ def __init__(self, email): self.email = sanitize_email(email) self.email_hash = md5_hash(self.email) - def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, rating="", filetype_extension=False, use_ssl=False): + def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, rating="", filetype_extension=False, use_ssl=True): """ Returns an URL to the user profile image. >>> g = Gravatar('myemailaddress@example.com') >>> g.get_image() - 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' + 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' With *size* you can request a specific image size, by default, images are presented at 80px by 80px. You may request image anywhere from 1px up to 2048px. @@ -92,7 +92,7 @@ def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, ra See more details on `Gravatar Image Requests `_. """ - base_url = '{protocol}://{domain}/avatar/' \ + base_url = '{protocol}://www.gravatar.com/avatar/' \ '{hash}{extension}{params}' params_dict = { @@ -125,40 +125,42 @@ def get_image(self, size=DEFAULT_IMAGE_SIZE, default="", force_default=False, ra params = urlencode(params_dict) - protocol = 'http' - domain = 'www.gravatar.com' - if use_ssl: - protocol = 'https' - domain = 'secure.gravatar.com' + protocol = 'https' + if not use_ssl: + protocol = 'http' extension = '.jpg' if filetype_extension else '' params = '?%s' % params if params else '' data = { 'protocol': protocol, - 'domain': domain, 'hash': self.email_hash, 'extension': extension, 'params': params, } return base_url.format(**data) - def get_profile(self, data_format=''): + def get_profile(self, data_format='', use_ssl=True): """ Returns an URL to the profile information associated with the Gravatar account. >>> g = Gravatar('myemailaddress@example.com') >>> g.get_profile() - 'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' + 'https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' See more details on `Gravatar Profile Requests `_. """ - base_url = 'http://www.gravatar.com/{hash}{data_format}' + protocol = 'https' + if not use_ssl: + protocol = 'http' + + base_url = '{protocol}://www.gravatar.com/{hash}{data_format}' if data_format and data_format in self.PROFILE_FORMATS: data_format = '.%s' % data_format data = { + 'protocol': protocol, 'hash': self.email_hash, 'data_format': data_format, } diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/test_gravatar.py b/tests/test_gravatar.py index 60be233..9fc86ac 100644 --- a/tests/test_gravatar.py +++ b/tests/test_gravatar.py @@ -11,13 +11,13 @@ def setUp(self): def test_without_params(self): """Test a get_image with default parameters.""" - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' result = self.g.get_image() self.assertEqual(image_url, result) def test_image_size(self): """Test image size parameter.""" - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?size=24' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?size=24' result = self.g.get_image(size=24) self.assertEqual(image_url, result) @@ -28,17 +28,17 @@ def test_invalid_image_size(self): def test_filetype_extension(self): """Test filetype_extension parameter.""" - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346.jpg' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346.jpg' result = self.g.get_image(filetype_extension=True) self.assertEqual(image_url, result) def test_default(self): """Test default parameter.""" - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=monsterid' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=monsterid' result = self.g.get_image(default='monsterid') self.assertEqual(image_url, result) - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?default=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg' result = self.g.get_image(default='http://example.com/images/avatar.jpg') self.assertEqual(image_url, result) @@ -47,31 +47,31 @@ def test_default(self): def test_forcedefault(self): """Test forcedefault parameter.""" - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?forcedefault=y' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?forcedefault=y' result = self.g.get_image(force_default=True) self.assertEqual(image_url, result) def test_use_ssl(self): """Test use_ssl parameter.""" - image_url = 'https://secure.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' - result = self.g.get_image(use_ssl=True) + image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346' + result = self.g.get_image(use_ssl=False) self.assertEqual(image_url, result) def test_rating(self): """Test the rating parameter.""" - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=g' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=g' result = self.g.get_image(rating='g') self.assertEqual(image_url, result) - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=pg' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=pg' result = self.g.get_image(rating='pg') self.assertEqual(image_url, result) - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=r' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=r' result = self.g.get_image(rating='r') self.assertEqual(image_url, result) - image_url = 'http://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=x' + image_url = 'https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346?rating=x' result = self.g.get_image(rating='x') self.assertEqual(image_url, result) @@ -86,8 +86,15 @@ def setUp(self): def test_without_params(self): """Test a get_profile with default parameters.""" - profile_url = 'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' + profile_url = 'https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' result = self.g.get_profile() self.assertEqual(profile_url, result) + def test_use_ssl(self): + """Test use_ssl parameter.""" + image_url = 'http://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346' + result = self.g.get_profile(use_ssl=False) + self.assertEqual(image_url, result) + +