Skip to content

Commit

Permalink
feature/image-referrer-url
Browse files Browse the repository at this point in the history
  • Loading branch information
arrrlo committed Feb 15, 2022
1 parent a7ebc72 commit d4f96ca
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions google_images_search/fetch_resize_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -363,6 +364,7 @@ def __init__(self, fetch_resize_save):

self._url = None
self._path = None
self._referrer_url = None

self.resized = False

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions google_images_search/google_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion google_images_search/meta.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.3.10'
__version__ = '1.4.0'
38 changes: 27 additions & 11 deletions tests/test_fetch_resize_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
]
}

Expand All @@ -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):
Expand Down Expand Up @@ -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 = []
Expand All @@ -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()
Expand Down

0 comments on commit d4f96ca

Please sign in to comment.