@@ -14,18 +14,76 @@ from lxml import html
14from searx.poolrequests import get
14from searx.poolrequests import get
15from searx.engines.xpath import extract_text, extract_url
15from searx.engines.xpath import extract_text, extract_url
17# engine dependent config
18# engine dependent config
18categories = ['general']
19categories = ['general']
19paging = True
20paging = True
20language_support = True
21language_support = True
22+use_locale_domain = True
24+# based on https://en.wikipedia.org/wiki/List_of_Google_domains and tests
25+default_hostname = 'www.google.com'
27+country_to_hostname = {
28+ 'BG': 'www.google.bg', # Bulgaria
29+ 'CZ': 'www.google.cz', # Czech Republic
30+ 'DE': 'www.google.de', # Germany
31+ 'DK': 'www.google.dk', # Denmark
32+ 'AT': 'www.google.at', # Austria
33+ 'CH': 'www.google.ch', # Switzerland
34+ 'GR': 'www.google.gr', # Greece
35+ 'AU': 'www.google.com.au', # Australia
36+ 'CA': 'www.google.ca', # Canada
37+ 'GB': 'www.google.co.uk', # United Kingdom
38+ 'ID': 'www.google.co.id', # Indonesia
39+ 'IE': 'www.google.ie', # Ireland
40+ 'IN': 'www.google.co.in', # India
41+ 'MY': 'www.google.com.my', # Malaysia
42+ 'NZ': 'www.google.co.nz', # New Zealand
43+ 'PH': 'www.google.com.ph', # Philippines
44+ 'SG': 'www.google.com.sg', # Singapore
45+ # 'US': 'www.google.us', # United State, redirect to .com
46+ 'ZA': 'www.google.co.za', # South Africa
47+ 'AR': 'www.google.com.ar', # Argentina
48+ 'CL': 'www.google.cl', # Chile
49+ 'ES': 'www.google.es', # Span
50+ 'MX': 'www.google.com.mx', # Mexico
51+ 'EE': 'www.google.ee', # Estonia
52+ 'FI': 'www.google.fi', # Finland
53+ 'BE': 'www.google.be', # Belgium
54+ 'FR': 'www.google.fr', # France
55+ 'IL': 'www.google.co.il', # Israel
56+ 'HR': 'www.google.hr', # Croatia
57+ 'HU': 'www.google.hu', # Hungary
58+ 'IT': 'www.google.it', # Italy
59+ 'JP': 'www.google.co.jp', # Japan
60+ 'KR': 'www.google.co.kr', # South Korean
61+ 'LT': 'www.google.lt', # Lithuania
62+ 'LV': 'www.google.lv', # Latvia
63+ 'NO': 'www.google.no', # Norway
64+ 'NL': 'www.google.nl', # Netherlands
65+ 'PL': 'www.google.pl', # Poland
66+ 'BR': 'www.google.com.br', # Brazil
67+ 'PT': 'www.google.pt', # Portugal
68+ 'RO': 'www.google.ro', # Romania
69+ 'RU': 'www.google.ru', # Russia
70+ 'SK': 'www.google.sk', # Slovakia
71+ 'SL': 'www.google.si', # Slovenia (SL -> si)
72+ 'SE': 'www.google.se', # Sweden
73+ 'TH': 'www.google.co.th', # Thailand
74+ 'TR': 'www.google.com.tr', # Turkey
75+ 'UA': 'www.google.com.ua', # Ikraine
76+ # 'CN': 'www.google.cn', # China, only from china ?
77+ 'HK': 'www.google.com.hk', # Hong kong
78+ 'TW': 'www.google.com.tw' # Taiwan
22# search-url
81# search-url
23−google_hostname = 'www.google.com'
24search_path = '/search'
82search_path = '/search'
25redirect_path = '/url'
84redirect_path = '/url'
26images_path = '/images'
85images_path = '/images'
27−search_url = ('https://' +
86+search_url = ('https://{hostname}' +
29 search_path +
87 search_path +
30 '?{query}&start={offset}&gbv=1')
88 '?{query}&start={offset}&gbv=1')
@@ -34,6 +92,7 @@ results_xpath = '//li[@class="g"]'
34url_xpath = './/h3/a/@href'
92url_xpath = './/h3/a/@href'
35title_xpath = './/h3'
93title_xpath = './/h3'
36content_xpath = './/span[@class="st"]'
94content_xpath = './/span[@class="st"]'
95+content_misc_xpath = './/div[@class="f slp"]'
37suggestion_xpath = '//p[@class="_Bmc"]'
96suggestion_xpath = '//p[@class="_Bmc"]'
39images_xpath = './/div/a'
98images_xpath = './/div/a'
@@ -41,6 +100,7 @@ image_url_xpath = './@href'
41image_img_src_xpath = './img/@src'
100image_img_src_xpath = './img/@src'
43pref_cookie = ''
102pref_cookie = ''
46# see https://support.google.com/websearch/answer/873?hl=en
106# see https://support.google.com/websearch/answer/873?hl=en
@@ -52,8 +112,16 @@ def get_google_pref_cookie():
52 return pref_cookie
112 return pref_cookie
115+def get_google_nid_cookie(google_hostname):
117+ if google_hostname not in nid_cookie:
118+ resp = get('https://' + google_hostname)
119+ nid_cookie[google_hostname] = resp.cookies.get("NID", None)
120+ return nid_cookie[google_hostname]
55# remove google-specific tracking-url
123# remove google-specific tracking-url
56−def parse_url(url_string):
124+def parse_url(url_string, google_hostname):
57 parsed_url = urlparse(url_string)
125 parsed_url = urlparse(url_string)
58 if (parsed_url.netloc in [google_hostname, '']
126 if (parsed_url.netloc in [google_hostname, '']
59 and parsed_url.path == redirect_path):
127 and parsed_url.path == redirect_path):
@@ -63,21 +131,45 @@ def parse_url(url_string):
63 return url_string
131 return url_string
134+# returns extract_text on the first result selected by the xpath or None
135+def extract_text_from_dom(result, xpath):
136+ r = result.xpath(xpath)
138+ return extract_text(r[0])
66# do search-request
142# do search-request
67def request(query, params):
143def request(query, params):
68 offset = (params['pageno'] - 1) * 10
144 offset = (params['pageno'] - 1) * 10
70 if params['language'] == 'all':
146 if params['language'] == 'all':
71 language = 'en'
147 language = 'en'
73− language = params['language'].replace('_', '-').lower()
150+ language_array = params['language'].lower().split('_')
151+ if len(language_array) == 2:
152+ country = language_array[1]
155+ language = language_array[0] + ',' + language_array[0] + '-' + country
157+ if use_locale_domain:
158+ google_hostname = country_to_hostname.get(country.upper(), default_hostname)
160+ google_hostname = default_hostname
75 params['url'] = search_url.format(offset=offset,
162 params['url'] = search_url.format(offset=offset,
76− query=urlencode({'q': query}))
163+ query=urlencode({'q': query}),
164+ hostname=google_hostname)
78 params['headers']['Accept-Language'] = language
166 params['headers']['Accept-Language'] = language
79− if language.startswith('en'):
167+ params['headers']['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
168+ if google_hostname == default_hostname:
80 params['cookies']['PREF'] = get_google_pref_cookie()
169 params['cookies']['PREF'] = get_google_pref_cookie()
170+ params['cookies']['NID'] = get_google_nid_cookie(google_hostname)
172+ params['google_hostname'] = google_hostname
82 return params
174 return params
@@ -86,17 +178,30 @@ def request(query, params):
86def response(resp):
178def response(resp):
87 results = []
179 results = []
181+ # detect google sorry
182+ resp_url = urlparse(resp.url)
183+ if resp_url.netloc == 'sorry.google.com' or resp_url.path == '/sorry/IndexRedirect':
184+ raise RuntimeWarning('sorry.google.com')
187+ google_hostname = resp.search_params.get('google_hostname')
188+ google_url = "https://" + google_hostname
190+ # convert the text to dom
89 dom = html.fromstring(resp.text)
191 dom = html.fromstring(resp.text)
91 # parse results
193 # parse results
92 for result in dom.xpath(results_xpath):
194 for result in dom.xpath(results_xpath):
93 title = extract_text(result.xpath(title_xpath)[0])
195 title = extract_text(result.xpath(title_xpath)[0])
95− url = parse_url(extract_url(result.xpath(url_xpath), search_url))
197+ url = parse_url(extract_url(result.xpath(url_xpath), google_url), google_hostname)
96− parsed_url = urlparse(url)
198+ parsed_url = urlparse(url, google_hostname)
97 if (parsed_url.netloc == google_hostname
199 if (parsed_url.netloc == google_hostname
98− and parsed_url.path == search_path):
200+ and (parsed_url.path == search_path
201+ or parsed_url.path.startswith(maps_path))):
99− # remove the link to google news
202+ # remove the link to google news and google maps
203+ # FIXME : sometimes the URL is https://maps.google.*/maps
204+ # no consequence, the result trigger an exception after which is ignored
102 # images result
207 # images result
@@ -104,16 +209,21 @@ def response(resp):
104 and parsed_url.path == images_path):
209 and parsed_url.path == images_path):
105 # only thumbnail image provided,
210 # only thumbnail image provided,
106 # so skipping image results
211 # so skipping image results
107− # results = results + parse_images(result)
212+ # results = results + parse_images(result, google_hostname)
110 # normal result
215 # normal result
111− content = extract_text(result.xpath(content_xpath)[0])
216+ content = extract_text_from_dom(result, content_xpath)
219+ content_misc = extract_text_from_dom(result, content_misc_xpath)
220+ if content_misc is not None:
221+ content = content_misc + "<br />" + content
112 # append result
222 # append result
113 results.append({'url': url,
223 results.append({'url': url,
114 'title': title,
224 'title': title,
115 'content': content})
225 'content': content})
116− except:
226+ except Exception:
119 # parse suggestion
229 # parse suggestion
@@ -125,10 +235,10 @@ def response(resp):
125 return results
235 return results
128−def parse_images(result):
238+def parse_images(result, google_hostname):
129 results = []
239 results = []
130 for image in result.xpath(images_xpath):
240 for image in result.xpath(images_xpath):
131− url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
241+ url = parse_url(extract_text(image.xpath(image_url_xpath)[0]), google_hostname)
132 img_src = extract_text(image.xpath(image_img_src_xpath)[0])
242 img_src = extract_text(image.xpath(image_img_src_xpath)[0])
134 # append result
244 # append result