@@ -14,18 +14,76 @@ from lxml import html
1414from searx.poolrequests import get
1515from searx.engines.xpath import extract_text, extract_url
1616
17+
1718# engine dependent config
1819categories = ['general']
1920paging = True
2021language_support = True
22+use_locale_domain = True
23+
24+# based on https://en.wikipedia.org/wiki/List_of_Google_domains and tests
25+default_hostname = 'www.google.com'
26+
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
79+}
2180
2281# search-url
23−google_hostname = 'www.google.com'
2482search_path = '/search'
83+maps_path = '/maps/'
2584redirect_path = '/url'
2685images_path = '/images'
27−search_url = ('https://' +
28− google_hostname +
86+search_url = ('https://{hostname}' +
2987 search_path +
3088 '?{query}&start={offset}&gbv=1')
3189
@@ -34,6 +92,7 @@ results_xpath = '//li[@class="g"]'
3492url_xpath = './/h3/a/@href'
3593title_xpath = './/h3'
3694content_xpath = './/span[@class="st"]'
95+content_misc_xpath = './/div[@class="f slp"]'
3796suggestion_xpath = '//p[@class="_Bmc"]'
3897
3998images_xpath = './/div/a'
@@ -41,6 +100,7 @@ image_url_xpath = './@href'
41100image_img_src_xpath = './img/@src'
42101
43102pref_cookie = ''
103+nid_cookie = {}
44104
45105
46106# see https://support.google.com/websearch/answer/873?hl=en
@@ -52,8 +112,16 @@ def get_google_pref_cookie():
52112 return pref_cookie
53113
54114
115+def get_google_nid_cookie(google_hostname):
116+ global nid_cookie
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]
121+
122+
55123# remove google-specific tracking-url
56−def parse_url(url_string):
124+def parse_url(url_string, google_hostname):
57125 parsed_url = urlparse(url_string)
58126 if (parsed_url.netloc in [google_hostname, '']
59127 and parsed_url.path == redirect_path):
@@ -63,21 +131,45 @@ def parse_url(url_string):
63131 return url_string
64132
65133
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)
137+ if len(r) > 0:
138+ return extract_text(r[0])
139+ return None
140+
141+
66142# do search-request
67143def request(query, params):
68144 offset = (params['pageno'] - 1) * 10
69145
70146 if params['language'] == 'all':
71147 language = 'en'
148+ country = 'US'
72149 else:
73− language = params['language'].replace('_', '-').lower()
150+ language_array = params['language'].lower().split('_')
151+ if len(language_array) == 2:
152+ country = language_array[1]
153+ else:
154+ country = ' '
155+ language = language_array[0] + ',' + language_array[0] + '-' + country
156+
157+ if use_locale_domain:
158+ google_hostname = country_to_hostname.get(country.upper(), default_hostname)
159+ else:
160+ google_hostname = default_hostname
74161
75162 params['url'] = search_url.format(offset=offset,
76− query=urlencode({'q': query}))
163+ query=urlencode({'q': query}),
164+ hostname=google_hostname)
77165
78166 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:
80169 params['cookies']['PREF'] = get_google_pref_cookie()
170+ params['cookies']['NID'] = get_google_nid_cookie(google_hostname)
171+
172+ params['google_hostname'] = google_hostname
81173
82174 return params
83175
@@ -86,17 +178,30 @@ def request(query, params):
86178def response(resp):
87179 results = []
88180
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')
185+
186+ # which hostname ?
187+ google_hostname = resp.search_params.get('google_hostname')
188+ google_url = "https://" + google_hostname
189+
190+ # convert the text to dom
89191 dom = html.fromstring(resp.text)
90192
91193 # parse results
92194 for result in dom.xpath(results_xpath):
93195 title = extract_text(result.xpath(title_xpath)[0])
94196 try:
95− url = parse_url(extract_url(result.xpath(url_xpath), search_url))
96− parsed_url = urlparse(url)
197+ url = parse_url(extract_url(result.xpath(url_xpath), google_url), google_hostname)
198+ parsed_url = urlparse(url, google_hostname)
97199 if (parsed_url.netloc == google_hostname
98− and parsed_url.path == search_path):
99− # remove the link to google news
200+ and (parsed_url.path == search_path
201+ or parsed_url.path.startswith(maps_path))):
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
100205 continue
101206
102207 # images result
@@ -104,16 +209,21 @@ def response(resp):
104209 and parsed_url.path == images_path):
105210 # only thumbnail image provided,
106211 # so skipping image results
107− # results = results + parse_images(result)
212+ # results = results + parse_images(result, google_hostname)
108213 pass
109214 else:
110215 # normal result
111− content = extract_text(result.xpath(content_xpath)[0])
216+ content = extract_text_from_dom(result, content_xpath)
217+ if content is None:
218+ continue
219+ content_misc = extract_text_from_dom(result, content_misc_xpath)
220+ if content_misc is not None:
221+ content = content_misc + "<br />" + content
112222 # append result
113223 results.append({'url': url,
114224 'title': title,
115225 'content': content})
116− except:
226+ except Exception:
117227 continue
118228
119229 # parse suggestion
@@ -125,10 +235,10 @@ def response(resp):
125235 return results
126236
127237
128−def parse_images(result):
238+def parse_images(result, google_hostname):
129239 results = []
130240 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)
132242 img_src = extract_text(image.xpath(image_img_src_xpath)[0])
133243
134244 # append result