Skip to content

Commit

Permalink
Remove download image (Fix lk-geimfari#868) (lk-geimfari#873)
Browse files Browse the repository at this point in the history
* Remove download image (Fix lk-geimfari#868)

* Add more info about removing function to changelog

* Fix type in CHANGELOG

* Fix issues related to lk-geimfari#868
  • Loading branch information
lk-geimfari authored Jun 6, 2020
1 parent 249e472 commit 695a227
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Version 4.1.0
- Added method ``manufacturer()`` for class ``Transport()``
- Added ``sk`` (Slovak) locale support

**Removed**:

- Removed the deprecated ``download_image()`` function from the ``shortcuts`` module, use your custom downloader instead.

Version 4.0.0
-------------

Expand Down
22 changes: 0 additions & 22 deletions docs/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,6 @@ Very bad:
~ python manage.py fill_fake_db --count=600000 --locale=de
Importing images
----------------

Class :class:`~mimesis.Internet()` boasts of several methods which generate image
links (more details here). Links to images locate on remote servers
would be enough, however, if you still want to have a number of random
images locally, you can download images generated by the respective
class :class:`~mimesis.Internet()` methods with the help of function
``download_image()`` from model utils:

.. code:: python
>>> from mimesis import Internet
>>> from mimesis.shortcuts import download_image
>>> net = Internet()
>>> url = net.stock_image(width=1920, height=1080, keywords=['love', 'passion'])
>>> download_image(url=url, save_path='/some/path/')
Romanization of Cyrillic data
-----------------------------

Expand Down
24 changes: 12 additions & 12 deletions mimesis/providers/internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,37 @@ 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.
:param keywords: List of search keywords.
: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.
Expand Down
29 changes: 1 addition & 28 deletions mimesis/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
30 changes: 0 additions & 30 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-

import os

import pytest

from mimesis import shortcuts
Expand All @@ -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

0 comments on commit 695a227

Please sign in to comment.