Skip to content

Commit

Permalink
feat: using jieba analyse given keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
RyouMon committed Aug 21, 2021
1 parent 00ebcf6 commit c4f59e3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions need_an_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def save_image(image):
return filename


def from_bing(keyword, save=True):
def from_bing(keyword, save=True, exact=True, allow_pos=()):
"""
Get an image from Bing image.
if save = True , image with save in disk and return a filename,
if save = False, return an PIL.Image object.
"""
image = bing.get_an_image(keyword=keyword)
image = bing.get_an_image(keyword=keyword, exact=exact, allow_pos=allow_pos)

if save:
return save_image(image)
Expand Down
9 changes: 8 additions & 1 deletion need_an_image/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
from PIL import Image, UnidentifiedImageError
from bs4 import BeautifulSoup
from jieba import analyse
from need_an_image.decorators import retry_request


Expand All @@ -30,10 +31,16 @@ def get_image_source_url(self, response):

return source_url

def get_an_image(self, keyword, max_retry=0):
def get_an_image(self, keyword, max_retry=0, exact=True, allow_pos=()):
"""
return an Image matching keyword from web
"""
if not exact:
keywords = analyse.extract_tags(keyword, allowPOS=allow_pos, withWeight=True)
if not keywords:
keywords = analyse.extract_tags(keyword)
keyword = random.choice(keywords)

response = self.download_search_page(keyword)
while max_retry >= 0:
try:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Pillow>=8.0
requests>=2.0
beautifulsoup4==4.9.3
lxml==4.6.3
lxml==4.6.3
jieba>=0.42.1
4 changes: 2 additions & 2 deletions tests/unit/test_one_image_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_from_bing_return_filename_if_save_is_True(self, mock_save_image, mock_b
filename = need_an_image.from_bing(keyword='Cat', save=True)
image = mock_bing.get_an_image.return_value

mock_bing.get_an_image.assert_called_once_with(keyword='Cat')
mock_bing.get_an_image.assert_called_once_with(keyword='Cat', exact=True, allow_pos=())
mock_save_image.assert_called_once_with(image)
self.assertEqual(filename, mock_save_image.return_value)

Expand All @@ -20,6 +20,6 @@ def test_from_bing_return_filename_if_save_is_True(self, mock_save_image, mock_b
def test_from_bing_return_Image_if_save_is_False(self, mock_save_image, mock_bing):
image = need_an_image.from_bing(keyword='Cat', save=False)

mock_bing.get_an_image.assert_called_once_with(keyword='Cat')
mock_bing.get_an_image.assert_called_once_with(keyword='Cat', exact=True, allow_pos=())
mock_save_image.assert_not_called()
self.assertEqual(image, mock_bing.get_an_image.return_value)

0 comments on commit c4f59e3

Please sign in to comment.