Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'siterestrict' flag #168

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions google_images_search/fetch_resize_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class FetchResizeSave(object):
"""Class with resizing and downloading logic"""

def __init__(self, developer_key, custom_search_cx,
progressbar_fn=None, validate_images=True):
progressbar_fn=None, validate_images=True, siterestrict=False):

# initialise google api
self._google_custom_search = GoogleCustomSearch(
developer_key, custom_search_cx, self)
developer_key, custom_search_cx, self, siterestrict)

self._search_result = []
self.validate_images = validate_images
Expand Down
14 changes: 11 additions & 3 deletions google_images_search/google_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GoogleCustomSearch(object):

def __init__(self, developer_key=None,
custom_search_cx=None,
fetch_resize_save=None):
fetch_resize_save=None, siterestrict=False):

self._developer_key = developer_key or \
os.environ.get('GCS_DEVELOPER_KEY')
Expand All @@ -17,6 +17,7 @@ def __init__(self, developer_key=None,

self._google_build = None
self._fetch_resize_save = fetch_resize_save
self.siterestrict = siterestrict

self._search_params_keys = {
'q': None,
Expand All @@ -42,11 +43,18 @@ def _query_google_api(self, search_params, cache_discovery=True):
if not self._google_build:
self._google_build = discovery.build(
"customsearch", "v1",
# discoveryServiceUrl="https://www.googleapis.com/discovery/v1/apis/" "{api}/{apiVersion}/rest",
developerKey=self._developer_key,
cache_discovery=cache_discovery)
req = self._google_build.cse().list(
cx=self._custom_search_cx, **search_params)
if self.siterestrict:
req.uri = req.uri.replace('/v1?', '/v1/siterestrict?')
return req.execute()

return self._google_build.cse().list(
cx=self._custom_search_cx, **search_params).execute()

# return self._google_build.cse().list(
# cx=self._custom_search_cx, **search_params).execute()

def _search_params(self, params):
"""Received a dict of params and merges
Expand Down