Repository: lk-geimfari/mimesis

Issue:
Remove download_image function.
# Bug report

This is not a kind of problem mimesis solves. So it's pretty annoying when tests fail because of it all the time. It's better just remove it.


Discussion:

Resolving patch:
diff --git a/mimesis/providers/internet.py b/mimesis/providers/internet.py
--- a/mimesis/providers/internet.py
+++ b/mimesis/providers/internet.py
@@ -185,12 +185,13 @@ def stock_image(width: Union[int, str] = 1920,
                     height: Union[int, str] = 1080,
                     keywords: Optional[List[str]] = None,
                     writable: bool = False) -> Union[str, bytes]:
-        """Generate random stock image (JPEG) hosted on Unsplash.
+        """Generate random stock image (JPG/JPEG) hosted on Unsplash.
 
         See «Random search term» on https://source.unsplash.com/
         for more details.
 
-        .. note:: This method required an active HTTP connection.
+        .. note:: This method required an active HTTP connection
+            if you want to get writable object
 
         :param width: Width of the image.
         :param height: Height of the image.
@@ -198,24 +199,23 @@ def stock_image(width: Union[int, str] = 1920,
         :param writable: Return image as sequence ob bytes.
         :return: Link to the image.
         """
-        api = 'https://source.unsplash.com/{}x{}?{}'
+        api_url = 'https://source.unsplash.com/{}x{}?{}'
 
         if keywords is not None:
             keywords_str = ','.join(keywords)
         else:
             keywords_str = ''
 
-        url = api.format(width, height, keywords_str)
+        url = api_url.format(width, height, keywords_str)
 
-        try:
-            response = urllib.request.urlopen(url)
-            if writable:
+        if writable:
+            try:
+                response = urllib.request.urlopen(url)
                 return response.read()
-            url = response.geturl()
-            return url
-        except urllib.error.URLError:
-            raise urllib.error.URLError(
-                'Required an active HTTP connection')
+            except urllib.error.URLError:
+                raise urllib.error.URLError(
+                    'Required an active HTTP connection')
+        return url
 
     def hashtags(self, quantity: int = 4) -> Union[str, list]:
         """Generate a list of hashtags.
diff --git a/mimesis/shortcuts.py b/mimesis/shortcuts.py
--- a/mimesis/shortcuts.py
+++ b/mimesis/shortcuts.py
@@ -8,7 +8,7 @@
 from urllib import request
 from uuid import uuid4
 
-__all__ = ['download_image', 'luhn_checksum']
+__all__ = ['luhn_checksum']
 
 
 def luhn_checksum(num: str) -> str:
@@ -24,30 +24,3 @@ def luhn_checksum(num: str) -> str:
         sx = sx - 9 if sx > 9 else sx
         check += sx
     return str(check * 9 % 10)
-
-
-def download_image(url: str = '', save_path: str = '',
-                   unverified_ctx: bool = False) -> Union[None, str]:
-    """Download image and save in current directory on local machine.
-
-    :param url: URL to image.
-    :param save_path: Saving path.
-    :param unverified_ctx: Create unverified context.
-    :return: Path to downloaded image.
-    :rtype: str or None
-    """
-    if unverified_ctx:
-        ssl._create_default_https_context = ssl._create_unverified_context
-
-    if url:
-        image_name = url.rsplit('/')[-1]
-
-        splitted_name = image_name.rsplit('.')
-        if len(splitted_name) < 2:
-            image_name = '{}.jpg'.format(uuid4())
-        else:
-            image_name = '{}.{}'.format(uuid4(), splitted_name[-1])
-        full_image_path = path.join(save_path, image_name)
-        request.urlretrieve(url, full_image_path)
-        return full_image_path
-    return None


Test patch:
diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py
--- a/tests/test_shortcuts.py
+++ b/tests/test_shortcuts.py
@@ -1,7 +1,5 @@
 # -*- coding: utf-8 -*-
 
-import os
-
 import pytest
 
 from mimesis import shortcuts
@@ -16,31 +14,3 @@
 )
 def test_luhn_checksum(number, check_sum):
     assert shortcuts.luhn_checksum(number) == check_sum
-
-
-@pytest.mark.parametrize(
-    'ctx', [
-        False,
-        True,
-    ],
-)
-def test_download_image(ctx):
-    url = 'https://raw.githubusercontent.com/' \
-          'lk-geimfari/mimesis/master/media/readme-logo.png'
-
-    verified = shortcuts.download_image(
-        url=url,
-        unverified_ctx=ctx,
-    )
-    assert verified == str(verified)[:-4] + '.png'
-    os.remove(verified)
-
-    url_without_extension = 'https://source.unsplash.com/300x300/?people'
-    verified = shortcuts.download_image(
-        url=url_without_extension, unverified_ctx=ctx,
-    )
-    assert verified == str(verified)[:-4] + '.jpg'
-    os.remove(verified)
-
-    result = shortcuts.download_image('', unverified_ctx=ctx)
-    assert result is None