diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a55cc..af22f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.4.0 + +### Added in 1.4.0 + +- Image object now has a referrer url (source) as well + ## 1.3.10 ### Fixed in 1.3.10 diff --git a/README.md b/README.md index 631e6bf..48cb77b 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,13 @@ gis.search(search_params=_search_params, path_to_dir='/path/', width=500, height # search first, then download and resize afterwards: gis.search(search_params=_search_params) for image in gis.results(): - image.download('/path/') - image.resize(500, 500) + image.url # image direct url + image.referrer_url # image referrer url (source) + + image.download('/path/') # download image + image.resize(500, 500) # resize downloaded image + + image.path # downloaded local file path ``` ## [Custom file name](#custom-file-name) diff --git a/google_images_search/fetch_resize_save.py b/google_images_search/fetch_resize_save.py index 465bd5b..d0abc2c 100644 --- a/google_images_search/fetch_resize_save.py +++ b/google_images_search/fetch_resize_save.py @@ -161,7 +161,7 @@ def search(self, search_params, path_to_dir=False, width=None, self._search_result = self._search_result[:self._number_of_images] def _search_images(self, search_params, path_to_dir=False, width=None, - height=None, cache_discovery=False): + height=None, cache_discovery=False): """Fetched images using Google API and does the download and resize if path_to_dir and width and height variables are provided. :param search_params: parameters for Google API Search @@ -174,12 +174,13 @@ def _search_images(self, search_params, path_to_dir=False, width=None, i = 0 threads = [] - for url in self._google_custom_search.search( + for url, referrer_url in self._google_custom_search.search( search_params, cache_discovery ): # initialise image object image = GSImage(self) image.url = url + image.referrer_url = referrer_url # set thread safe variables self._download_progress[url] = 0 @@ -363,6 +364,7 @@ def __init__(self, fetch_resize_save): self._url = None self._path = None + self._referrer_url = None self.resized = False @@ -400,6 +402,23 @@ def path(self, image_path): self._path = image_path + @property + def referrer_url(self): + """Returns image referrer url + :return: referrer_url + """ + + return self._referrer_url + + @referrer_url.setter + def referrer_url(self, referrer_url): + """Sets image referrer url + :param referrer_url: referrer url + :return: None + """ + + self._referrer_url = referrer_url + def download(self, path_to_dir): """Downloads image from url to path :param path_to_dir: path diff --git a/google_images_search/google_api.py b/google_images_search/google_api.py index 15de90c..010ac02 100644 --- a/google_images_search/google_api.py +++ b/google_images_search/google_api.py @@ -84,9 +84,8 @@ def search(self, params, cache_discovery=False): self._fetch_resize_save.zero_return = True for image in results: - if len(self._fetch_resize_save._search_result) >= \ - self._fetch_resize_save._number_of_images: + self._fetch_resize_save._number_of_images: break if self._fetch_resize_save.validate_images: @@ -106,7 +105,8 @@ def search(self, params, cache_discovery=False): ) except requests.exceptions.RequestException: continue - yield image['link'] + + yield image['link'], image['image']['thumbnailLink'] class GoogleBackendException(Exception): diff --git a/google_images_search/meta.py b/google_images_search/meta.py index f1a3fb0..96e3ce8 100644 --- a/google_images_search/meta.py +++ b/google_images_search/meta.py @@ -1 +1 @@ -__version__ = '1.3.10' +__version__ = '1.4.0' diff --git a/tests/test_fetch_resize_save.py b/tests/test_fetch_resize_save.py index 291ed06..8f0a315 100644 --- a/tests/test_fetch_resize_save.py +++ b/tests/test_fetch_resize_save.py @@ -8,8 +8,18 @@ items = { 'items': [ - {'link': 'https://www.gstatic.com/webp/gallery3/1.png'}, - {'link': 'https://www.gstatic.com/webp/gallery3/2.png'} + { + 'link': 'https://www.gstatic.com/webp/gallery3/1.png', + 'image': { + 'thumbnailLink': 'https://www.gstatic.com' + } + }, + { + 'link': 'https://www.gstatic.com/webp/gallery3/2.png', + 'image': { + 'thumbnailLink': 'https://www.gstatic.com' + } + } ] } @@ -30,8 +40,8 @@ def setUp(self): ), 'tests' ) self._file_paths = [ - os.path.join(self._base_dir, '1.png'), - os.path.join(self._base_dir, '2.png'), + os.path.join(self._base_dir, '1.jpg'), + os.path.join(self._base_dir, '2.jpg'), ] def tearDown(self): @@ -71,10 +81,16 @@ def test_search_url(self): for i, item in enumerate(self._frs.results()): self.assertEqual(item.url, items['items'][i]['link']) + def test_search_referrer_url(self): + self._frs.search({'num': 2}) + for i, item in enumerate(self._frs.results()): + self.assertEqual(item.referrer_url, + items['items'][i]['image']['thumbnailLink']) + def test_search_path(self): self._frs.search({}, path_to_dir=self._base_dir, width=100, height=100) - #for i, item in enumerate(self._frs.results()): - # self.assertEqual(item.path, self._file_paths[i]) + for i, item in enumerate(self._frs.results()): + self.assertEqual(item.path, self._file_paths[i]) def test_progressbar(self): progress_data = [] @@ -85,11 +101,11 @@ def pbar(url, progress): frs = FetchResizeSave(self._api_key, self._api_cx, progressbar_fn=pbar) frs.search({'num': 2}, path_to_dir=self._base_dir) - #self.assertEqual( - # progress_data, - # list(zip([items['items'][0]['link']] * 100, list(range(1, 101)))) + - # list(zip([items['items'][1]['link']] * 100, list(range(1, 101)))) - #) + self.assertEqual( + progress_data, + list(zip([items['items'][0]['link']] * 100, list(range(1, 101)))) + + list(zip([items['items'][1]['link']] * 100, list(range(1, 101)))) + ) def test_bytes_io(self): my_bytes_io = BytesIO()