|
@@ -14,18 +14,76 @@ from lxml import html
|
|
from searx.poolrequests import get
|
|
from searx.poolrequests import get
|
|
from searx.engines.xpath import extract_text, extract_url
|
|
from searx.engines.xpath import extract_text, extract_url
|
|
|
|
|
|
|
|
+
|
|
|
|
|
|
categories = ['general']
|
|
categories = ['general']
|
|
paging = True
|
|
paging = True
|
|
language_support = True
|
|
language_support = True
|
|
|
|
+use_locale_domain = True
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+default_hostname = 'www.google.com'
|
|
|
|
+
|
|
|
|
+country_to_hostname = {
|
|
|
|
+ 'BG': 'www.google.bg',
|
|
|
|
+ 'CZ': 'www.google.cz',
|
|
|
|
+ 'DE': 'www.google.de',
|
|
|
|
+ 'DK': 'www.google.dk',
|
|
|
|
+ 'AT': 'www.google.at',
|
|
|
|
+ 'CH': 'www.google.ch',
|
|
|
|
+ 'GR': 'www.google.gr',
|
|
|
|
+ 'AU': 'www.google.com.au',
|
|
|
|
+ 'CA': 'www.google.ca',
|
|
|
|
+ 'GB': 'www.google.co.uk',
|
|
|
|
+ 'ID': 'www.google.co.id',
|
|
|
|
+ 'IE': 'www.google.ie',
|
|
|
|
+ 'IN': 'www.google.co.in',
|
|
|
|
+ 'MY': 'www.google.com.my',
|
|
|
|
+ 'NZ': 'www.google.co.nz',
|
|
|
|
+ 'PH': 'www.google.com.ph',
|
|
|
|
+ 'SG': 'www.google.com.sg',
|
|
|
|
+
|
|
|
|
+ 'ZA': 'www.google.co.za',
|
|
|
|
+ 'AR': 'www.google.com.ar',
|
|
|
|
+ 'CL': 'www.google.cl',
|
|
|
|
+ 'ES': 'www.google.es',
|
|
|
|
+ 'MX': 'www.google.com.mx',
|
|
|
|
+ 'EE': 'www.google.ee',
|
|
|
|
+ 'FI': 'www.google.fi',
|
|
|
|
+ 'BE': 'www.google.be',
|
|
|
|
+ 'FR': 'www.google.fr',
|
|
|
|
+ 'IL': 'www.google.co.il',
|
|
|
|
+ 'HR': 'www.google.hr',
|
|
|
|
+ 'HU': 'www.google.hu',
|
|
|
|
+ 'IT': 'www.google.it',
|
|
|
|
+ 'JP': 'www.google.co.jp',
|
|
|
|
+ 'KR': 'www.google.co.kr',
|
|
|
|
+ 'LT': 'www.google.lt',
|
|
|
|
+ 'LV': 'www.google.lv',
|
|
|
|
+ 'NO': 'www.google.no',
|
|
|
|
+ 'NL': 'www.google.nl',
|
|
|
|
+ 'PL': 'www.google.pl',
|
|
|
|
+ 'BR': 'www.google.com.br',
|
|
|
|
+ 'PT': 'www.google.pt',
|
|
|
|
+ 'RO': 'www.google.ro',
|
|
|
|
+ 'RU': 'www.google.ru',
|
|
|
|
+ 'SK': 'www.google.sk',
|
|
|
|
+ 'SL': 'www.google.si',
|
|
|
|
+ 'SE': 'www.google.se',
|
|
|
|
+ 'TH': 'www.google.co.th',
|
|
|
|
+ 'TR': 'www.google.com.tr',
|
|
|
|
+ 'UA': 'www.google.com.ua',
|
|
|
|
+
|
|
|
|
+ 'HK': 'www.google.com.hk',
|
|
|
|
+ 'TW': 'www.google.com.tw'
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
-google_hostname = 'www.google.com'
|
|
|
|
search_path = '/search'
|
|
search_path = '/search'
|
|
|
|
+maps_path = '/maps/'
|
|
redirect_path = '/url'
|
|
redirect_path = '/url'
|
|
images_path = '/images'
|
|
images_path = '/images'
|
|
-search_url = ('https://' +
|
|
+search_url = ('https://{hostname}' +
|
|
- google_hostname +
|
|
|
|
search_path +
|
|
search_path +
|
|
'?{query}&start={offset}&gbv=1')
|
|
'?{query}&start={offset}&gbv=1')
|
|
|
|
|
|
@@ -34,6 +92,7 @@ results_xpath = '//li[@class="g"]'
|
|
url_xpath = './/h3/a/@href'
|
|
url_xpath = './/h3/a/@href'
|
|
title_xpath = './/h3'
|
|
title_xpath = './/h3'
|
|
content_xpath = './/span[@class="st"]'
|
|
content_xpath = './/span[@class="st"]'
|
|
|
|
+content_misc_xpath = './/div[@class="f slp"]'
|
|
suggestion_xpath = '//p[@class="_Bmc"]'
|
|
suggestion_xpath = '//p[@class="_Bmc"]'
|
|
|
|
|
|
images_xpath = './/div/a'
|
|
images_xpath = './/div/a'
|
|
@@ -41,6 +100,7 @@ image_url_xpath = './@href'
|
|
image_img_src_xpath = './img/@src'
|
|
image_img_src_xpath = './img/@src'
|
|
|
|
|
|
pref_cookie = ''
|
|
pref_cookie = ''
|
|
|
|
+nid_cookie = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -52,8 +112,16 @@ def get_google_pref_cookie():
|
|
return pref_cookie
|
|
return pref_cookie
|
|
|
|
|
|
|
|
|
|
|
|
+def get_google_nid_cookie(google_hostname):
|
|
|
|
+ global nid_cookie
|
|
|
|
+ if google_hostname not in nid_cookie:
|
|
|
|
+ resp = get('https://' + google_hostname)
|
|
|
|
+ nid_cookie[google_hostname] = resp.cookies.get("NID", None)
|
|
|
|
+ return nid_cookie[google_hostname]
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
-def parse_url(url_string):
|
|
+def parse_url(url_string, google_hostname):
|
|
parsed_url = urlparse(url_string)
|
|
parsed_url = urlparse(url_string)
|
|
if (parsed_url.netloc in [google_hostname, '']
|
|
if (parsed_url.netloc in [google_hostname, '']
|
|
and parsed_url.path == redirect_path):
|
|
and parsed_url.path == redirect_path):
|
|
@@ -63,21 +131,45 @@ def parse_url(url_string):
|
|
return url_string
|
|
return url_string
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
+def extract_text_from_dom(result, xpath):
|
|
|
|
+ r = result.xpath(xpath)
|
|
|
|
+ if len(r) > 0:
|
|
|
|
+ return extract_text(r[0])
|
|
|
|
+ return None
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
def request(query, params):
|
|
def request(query, params):
|
|
offset = (params['pageno'] - 1) * 10
|
|
offset = (params['pageno'] - 1) * 10
|
|
|
|
|
|
if params['language'] == 'all':
|
|
if params['language'] == 'all':
|
|
language = 'en'
|
|
language = 'en'
|
|
|
|
+ country = 'US'
|
|
else:
|
|
else:
|
|
- language = params['language'].replace('_', '-').lower()
|
|
+ language_array = params['language'].lower().split('_')
|
|
|
|
+ if len(language_array) == 2:
|
|
|
|
+ country = language_array[1]
|
|
|
|
+ else:
|
|
|
|
+ country = ' '
|
|
|
|
+ language = language_array[0] + ',' + language_array[0] + '-' + country
|
|
|
|
+
|
|
|
|
+ if use_locale_domain:
|
|
|
|
+ google_hostname = country_to_hostname.get(country.upper(), default_hostname)
|
|
|
|
+ else:
|
|
|
|
+ google_hostname = default_hostname
|
|
|
|
|
|
params['url'] = search_url.format(offset=offset,
|
|
params['url'] = search_url.format(offset=offset,
|
|
- query=urlencode({'q': query}))
|
|
+ query=urlencode({'q': query}),
|
|
|
|
+ hostname=google_hostname)
|
|
|
|
|
|
params['headers']['Accept-Language'] = language
|
|
params['headers']['Accept-Language'] = language
|
|
- if language.startswith('en'):
|
|
+ params['headers']['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
|
|
|
|
+ if google_hostname == default_hostname:
|
|
params['cookies']['PREF'] = get_google_pref_cookie()
|
|
params['cookies']['PREF'] = get_google_pref_cookie()
|
|
|
|
+ params['cookies']['NID'] = get_google_nid_cookie(google_hostname)
|
|
|
|
+
|
|
|
|
+ params['google_hostname'] = google_hostname
|
|
|
|
|
|
return params
|
|
return params
|
|
|
|
|
|
@@ -86,17 +178,30 @@ def request(query, params):
|
|
def response(resp):
|
|
def response(resp):
|
|
results = []
|
|
results = []
|
|
|
|
|
|
|
|
+
|
|
|
|
+ resp_url = urlparse(resp.url)
|
|
|
|
+ if resp_url.netloc == 'sorry.google.com' or resp_url.path == '/sorry/IndexRedirect':
|
|
|
|
+ raise RuntimeWarning('sorry.google.com')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ google_hostname = resp.search_params.get('google_hostname')
|
|
|
|
+ google_url = "https://" + google_hostname
|
|
|
|
+
|
|
|
|
+
|
|
dom = html.fromstring(resp.text)
|
|
dom = html.fromstring(resp.text)
|
|
|
|
|
|
|
|
|
|
for result in dom.xpath(results_xpath):
|
|
for result in dom.xpath(results_xpath):
|
|
title = extract_text(result.xpath(title_xpath)[0])
|
|
title = extract_text(result.xpath(title_xpath)[0])
|
|
try:
|
|
try:
|
|
- url = parse_url(extract_url(result.xpath(url_xpath), search_url))
|
|
+ url = parse_url(extract_url(result.xpath(url_xpath), google_url), google_hostname)
|
|
- parsed_url = urlparse(url)
|
|
+ parsed_url = urlparse(url, google_hostname)
|
|
if (parsed_url.netloc == google_hostname
|
|
if (parsed_url.netloc == google_hostname
|
|
- and parsed_url.path == search_path):
|
|
+ and (parsed_url.path == search_path
|
|
-
|
|
+ or parsed_url.path.startswith(maps_path))):
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -104,16 +209,21 @@ def response(resp):
|
|
and parsed_url.path == images_path):
|
|
and parsed_url.path == images_path):
|
|
|
|
|
|
|
|
|
|
-
|
|
+
|
|
pass
|
|
pass
|
|
else:
|
|
else:
|
|
|
|
|
|
- content = extract_text(result.xpath(content_xpath)[0])
|
|
+ content = extract_text_from_dom(result, content_xpath)
|
|
|
|
+ if content is None:
|
|
|
|
+ continue
|
|
|
|
+ content_misc = extract_text_from_dom(result, content_misc_xpath)
|
|
|
|
+ if content_misc is not None:
|
|
|
|
+ content = content_misc + "<br />" + content
|
|
|
|
|
|
results.append({'url': url,
|
|
results.append({'url': url,
|
|
'title': title,
|
|
'title': title,
|
|
'content': content})
|
|
'content': content})
|
|
- except:
|
|
+ except Exception:
|
|
continue
|
|
continue
|
|
|
|
|
|
|
|
|
|
@@ -125,10 +235,10 @@ def response(resp):
|
|
return results
|
|
return results
|
|
|
|
|
|
|
|
|
|
-def parse_images(result):
|
|
+def parse_images(result, google_hostname):
|
|
results = []
|
|
results = []
|
|
for image in result.xpath(images_xpath):
|
|
for image in result.xpath(images_xpath):
|
|
- url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
|
|
+ url = parse_url(extract_text(image.xpath(image_url_xpath)[0]), google_hostname)
|
|
img_src = extract_text(image.xpath(image_img_src_xpath)[0])
|
|
img_src = extract_text(image.xpath(image_img_src_xpath)[0])
|
|
|
|
|
|
|
|
|