From ec44cb3e2068b3bf083725ff5587cfd4fd543f4a Mon Sep 17 00:00:00 2001 From: Ajit Nath Date: Tue, 12 Jun 2018 18:08:06 +0800 Subject: [PATCH 1/2] Add time parameter to search query --- google/modules/standard_search.py | 4 ++-- google/modules/utils.py | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/google/modules/standard_search.py b/google/modules/standard_search.py index 0045556..eea9183 100644 --- a/google/modules/standard_search.py +++ b/google/modules/standard_search.py @@ -51,7 +51,7 @@ def _limit_str_size(self, str_element, size_limit): # PUBLIC -def search(query, pages=1, lang='en', area='com', ncr=False, void=True): +def search(query, pages=1, lang='en', area='com', ncr=False, void=True, time_period=False): """Returns a list of GoogleResult. Args: @@ -65,7 +65,7 @@ def search(query, pages=1, lang='en', area='com', ncr=False, void=True): results = [] for i in range(pages): - url = _get_search_url(query, i, lang=lang, area=area, ncr=ncr) + url = _get_search_url(query, i, lang=lang, area=area, ncr=ncr, time_period=time_period) html = get_html(url) if html: diff --git a/google/modules/utils.py b/google/modules/utils.py index 30f68ee..be12843 100644 --- a/google/modules/utils.py +++ b/google/modules/utils.py @@ -40,12 +40,27 @@ def normalize_query(query): return query.strip().replace(":", "%3A").replace("+", "%2B").replace("&", "%26").replace(" ", "+") -def _get_search_url(query, page=0, per_page=10, lang='en', area='com', ncr=False): +def _get_search_url(query, page=0, per_page=10, lang='en', area='com', ncr=False, time_period=False): # note: num per page might not be supported by google anymore (because of # google instant) - params = {'nl': lang, 'q': query.encode( - 'utf8'), 'start': page * per_page, 'num': per_page } + params = { + 'nl': lang, + 'q': query.encode('utf8'), + 'start': page * per_page, + 'num': per_page + } + + time_mapping = { + 'hour': 'qdr:h', + 'week': 'qdr:w', + 'month': 'qdr:m', + 'year': 'qdr:y' + } + + # Set time period for query if given + if time_period: + params['tbs'] = time_mapping[time_period] # This will allow to search Google with No Country Redirect if ncr: From 6ab82e263a5308d7c851b8f04ee1241067c81179 Mon Sep 17 00:00:00 2001 From: Ajit Nath Date: Thu, 14 Jun 2018 12:13:03 +0800 Subject: [PATCH 2/2] [FIX] Check if the param exists in mapping --- google/modules/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google/modules/utils.py b/google/modules/utils.py index be12843..651924c 100644 --- a/google/modules/utils.py +++ b/google/modules/utils.py @@ -59,7 +59,7 @@ def _get_search_url(query, page=0, per_page=10, lang='en', area='com', ncr=False } # Set time period for query if given - if time_period: + if time_period and time_period in time_mapping: params['tbs'] = time_mapping[time_period] # This will allow to search Google with No Country Redirect