From 07db24d8f241b533a378356e5ec08f4e275c7842 Mon Sep 17 00:00:00 2001 From: bleugreen Date: Thu, 5 Jan 2023 11:05:43 +0000 Subject: [PATCH] Added 'siterestrict' flag --- google_images_search/fetch_resize_save.py | 4 ++-- google_images_search/google_api.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/google_images_search/fetch_resize_save.py b/google_images_search/fetch_resize_save.py index d06eba1..a7afe87 100644 --- a/google_images_search/fetch_resize_save.py +++ b/google_images_search/fetch_resize_save.py @@ -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 diff --git a/google_images_search/google_api.py b/google_images_search/google_api.py index a5c930e..8509132 100644 --- a/google_images_search/google_api.py +++ b/google_images_search/google_api.py @@ -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') @@ -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, @@ -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