2014-12-05 20:03:16 +01:00
|
|
|
# Google (Web)
|
|
|
|
#
|
2014-09-01 15:10:05 +02:00
|
|
|
# @website https://www.google.com
|
2014-09-14 14:40:55 +02:00
|
|
|
# @provide-api yes (https://developers.google.com/custom-search/)
|
2014-12-05 20:03:16 +01:00
|
|
|
#
|
2014-09-14 14:40:55 +02:00
|
|
|
# @using-api no
|
|
|
|
# @results HTML
|
|
|
|
# @stable no (HTML can change)
|
|
|
|
# @parse url, title, content, suggestion
|
2014-01-29 19:28:38 +01:00
|
|
|
|
2015-06-05 11:23:24 +02:00
|
|
|
import re
|
2015-09-30 16:42:03 +02:00
|
|
|
from cgi import escape
|
2014-01-29 19:28:38 +01:00
|
|
|
from urllib import urlencode
|
2014-12-05 20:03:16 +01:00
|
|
|
from urlparse import urlparse, parse_qsl
|
2015-10-29 12:47:12 +01:00
|
|
|
from lxml import html, etree
|
2014-09-14 14:40:55 +02:00
|
|
|
from searx.engines.xpath import extract_text, extract_url
|
2015-10-29 12:47:12 +01:00
|
|
|
from searx.search import logger
|
|
|
|
|
|
|
|
logger = logger.getChild('google engine')
|
2014-01-29 19:28:38 +01:00
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
|
2014-09-01 15:10:05 +02:00
|
|
|
# engine dependent config
|
2014-01-29 19:28:38 +01:00
|
|
|
categories = ['general']
|
2014-09-01 15:10:05 +02:00
|
|
|
paging = True
|
|
|
|
language_support = True
|
2015-05-30 17:41:40 +02:00
|
|
|
use_locale_domain = True
|
|
|
|
|
|
|
|
# based on https://en.wikipedia.org/wiki/List_of_Google_domains and tests
|
|
|
|
default_hostname = 'www.google.com'
|
|
|
|
|
|
|
|
country_to_hostname = {
|
|
|
|
'BG': 'www.google.bg', # Bulgaria
|
|
|
|
'CZ': 'www.google.cz', # Czech Republic
|
|
|
|
'DE': 'www.google.de', # Germany
|
|
|
|
'DK': 'www.google.dk', # Denmark
|
|
|
|
'AT': 'www.google.at', # Austria
|
|
|
|
'CH': 'www.google.ch', # Switzerland
|
|
|
|
'GR': 'www.google.gr', # Greece
|
|
|
|
'AU': 'www.google.com.au', # Australia
|
|
|
|
'CA': 'www.google.ca', # Canada
|
|
|
|
'GB': 'www.google.co.uk', # United Kingdom
|
|
|
|
'ID': 'www.google.co.id', # Indonesia
|
|
|
|
'IE': 'www.google.ie', # Ireland
|
|
|
|
'IN': 'www.google.co.in', # India
|
|
|
|
'MY': 'www.google.com.my', # Malaysia
|
|
|
|
'NZ': 'www.google.co.nz', # New Zealand
|
|
|
|
'PH': 'www.google.com.ph', # Philippines
|
|
|
|
'SG': 'www.google.com.sg', # Singapore
|
2016-04-13 19:04:53 +02:00
|
|
|
# 'US': 'www.google.us', # United States, redirect to .com
|
2015-05-30 17:41:40 +02:00
|
|
|
'ZA': 'www.google.co.za', # South Africa
|
|
|
|
'AR': 'www.google.com.ar', # Argentina
|
|
|
|
'CL': 'www.google.cl', # Chile
|
2016-04-13 19:04:53 +02:00
|
|
|
'ES': 'www.google.es', # Spain
|
2015-05-30 17:41:40 +02:00
|
|
|
'MX': 'www.google.com.mx', # Mexico
|
|
|
|
'EE': 'www.google.ee', # Estonia
|
|
|
|
'FI': 'www.google.fi', # Finland
|
|
|
|
'BE': 'www.google.be', # Belgium
|
|
|
|
'FR': 'www.google.fr', # France
|
|
|
|
'IL': 'www.google.co.il', # Israel
|
|
|
|
'HR': 'www.google.hr', # Croatia
|
|
|
|
'HU': 'www.google.hu', # Hungary
|
|
|
|
'IT': 'www.google.it', # Italy
|
|
|
|
'JP': 'www.google.co.jp', # Japan
|
2016-04-13 19:04:53 +02:00
|
|
|
'KR': 'www.google.co.kr', # South Korea
|
2015-05-30 17:41:40 +02:00
|
|
|
'LT': 'www.google.lt', # Lithuania
|
|
|
|
'LV': 'www.google.lv', # Latvia
|
|
|
|
'NO': 'www.google.no', # Norway
|
|
|
|
'NL': 'www.google.nl', # Netherlands
|
|
|
|
'PL': 'www.google.pl', # Poland
|
|
|
|
'BR': 'www.google.com.br', # Brazil
|
|
|
|
'PT': 'www.google.pt', # Portugal
|
|
|
|
'RO': 'www.google.ro', # Romania
|
|
|
|
'RU': 'www.google.ru', # Russia
|
|
|
|
'SK': 'www.google.sk', # Slovakia
|
|
|
|
'SL': 'www.google.si', # Slovenia (SL -> si)
|
|
|
|
'SE': 'www.google.se', # Sweden
|
|
|
|
'TH': 'www.google.co.th', # Thailand
|
|
|
|
'TR': 'www.google.com.tr', # Turkey
|
2016-04-13 19:04:53 +02:00
|
|
|
'UA': 'www.google.com.ua', # Ukraine
|
|
|
|
# 'CN': 'www.google.cn', # China, only from China ?
|
|
|
|
'HK': 'www.google.com.hk', # Hong Kong
|
2015-05-30 17:41:40 +02:00
|
|
|
'TW': 'www.google.com.tw' # Taiwan
|
|
|
|
}
|
2014-01-29 19:28:38 +01:00
|
|
|
|
2015-06-05 11:23:24 +02:00
|
|
|
# osm
|
|
|
|
url_map = 'https://www.openstreetmap.org/'\
|
|
|
|
+ '?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
|
|
|
|
|
2014-09-01 15:10:05 +02:00
|
|
|
# search-url
|
2014-09-14 14:40:55 +02:00
|
|
|
search_path = '/search'
|
2015-05-30 17:41:40 +02:00
|
|
|
search_url = ('https://{hostname}' +
|
2014-12-05 20:03:16 +01:00
|
|
|
search_path +
|
2016-03-25 18:38:02 +01:00
|
|
|
'?{query}&start={offset}&gws_rd=cr&gbv=1&lr={lang}&ei=x')
|
2014-01-31 04:35:23 +01:00
|
|
|
|
2015-06-05 11:23:24 +02:00
|
|
|
# other URLs
|
|
|
|
map_hostname_start = 'maps.google.'
|
|
|
|
maps_path = '/maps'
|
|
|
|
redirect_path = '/url'
|
|
|
|
images_path = '/images'
|
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
# specific xpath variables
|
2016-01-10 18:49:50 +01:00
|
|
|
results_xpath = '//div[@class="g"]'
|
2014-09-14 14:40:55 +02:00
|
|
|
url_xpath = './/h3/a/@href'
|
|
|
|
title_xpath = './/h3'
|
|
|
|
content_xpath = './/span[@class="st"]'
|
2015-05-30 17:41:40 +02:00
|
|
|
content_misc_xpath = './/div[@class="f slp"]'
|
2014-09-14 14:40:55 +02:00
|
|
|
suggestion_xpath = '//p[@class="_Bmc"]'
|
|
|
|
|
2015-06-05 11:23:24 +02:00
|
|
|
# map : detail location
|
|
|
|
map_address_xpath = './/div[@class="s"]//table//td[2]/span/text()'
|
|
|
|
map_phone_xpath = './/div[@class="s"]//table//td[2]/span/span'
|
|
|
|
map_website_url_xpath = 'h3[2]/a/@href'
|
|
|
|
map_website_title_xpath = 'h3[2]'
|
|
|
|
|
|
|
|
# map : near the location
|
|
|
|
map_near = 'table[@class="ts"]//tr'
|
|
|
|
map_near_title = './/h4'
|
|
|
|
map_near_url = './/h4/a/@href'
|
|
|
|
map_near_phone = './/span[@class="nobr"]'
|
|
|
|
|
|
|
|
# images
|
2014-09-14 14:40:55 +02:00
|
|
|
images_xpath = './/div/a'
|
|
|
|
image_url_xpath = './@href'
|
|
|
|
image_img_src_xpath = './img/@src'
|
|
|
|
|
2015-06-05 11:23:24 +02:00
|
|
|
# property names
|
|
|
|
# FIXME : no translation
|
|
|
|
property_address = "Address"
|
|
|
|
property_phone = "Phone number"
|
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
# remove google-specific tracking-url
|
2015-05-30 17:41:40 +02:00
|
|
|
def parse_url(url_string, google_hostname):
|
2015-06-05 11:23:24 +02:00
|
|
|
# sanity check
|
|
|
|
if url_string is None:
|
|
|
|
return url_string
|
|
|
|
|
|
|
|
# normal case
|
2014-09-14 14:40:55 +02:00
|
|
|
parsed_url = urlparse(url_string)
|
2014-12-05 20:03:16 +01:00
|
|
|
if (parsed_url.netloc in [google_hostname, '']
|
|
|
|
and parsed_url.path == redirect_path):
|
2014-09-14 14:40:55 +02:00
|
|
|
query = dict(parse_qsl(parsed_url.query))
|
|
|
|
return query['q']
|
|
|
|
else:
|
|
|
|
return url_string
|
2014-01-29 19:28:38 +01:00
|
|
|
|
2014-12-05 20:03:16 +01:00
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
# returns extract_text on the first result selected by the xpath or None
|
|
|
|
def extract_text_from_dom(result, xpath):
|
|
|
|
r = result.xpath(xpath)
|
|
|
|
if len(r) > 0:
|
2015-09-30 16:42:03 +02:00
|
|
|
return escape(extract_text(r[0]))
|
2015-05-30 17:41:40 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-09-01 15:10:05 +02:00
|
|
|
# do search-request
|
2014-01-29 19:28:38 +01:00
|
|
|
def request(query, params):
|
2014-09-14 14:40:55 +02:00
|
|
|
offset = (params['pageno'] - 1) * 10
|
2014-09-01 15:10:05 +02:00
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
if params['language'] == 'all':
|
|
|
|
language = 'en'
|
2015-05-30 17:41:40 +02:00
|
|
|
country = 'US'
|
2016-03-25 18:38:02 +01:00
|
|
|
url_lang = ''
|
2014-09-14 14:40:55 +02:00
|
|
|
else:
|
2015-05-30 17:41:40 +02:00
|
|
|
language_array = params['language'].lower().split('_')
|
|
|
|
if len(language_array) == 2:
|
|
|
|
country = language_array[1]
|
|
|
|
else:
|
2015-06-05 11:23:24 +02:00
|
|
|
country = 'US'
|
2015-05-30 17:41:40 +02:00
|
|
|
language = language_array[0] + ',' + language_array[0] + '-' + country
|
2016-03-25 18:38:02 +01:00
|
|
|
url_lang = 'lang_' + language_array[0]
|
2015-05-30 17:41:40 +02:00
|
|
|
|
|
|
|
if use_locale_domain:
|
|
|
|
google_hostname = country_to_hostname.get(country.upper(), default_hostname)
|
|
|
|
else:
|
|
|
|
google_hostname = default_hostname
|
2014-09-01 15:10:05 +02:00
|
|
|
|
2014-01-29 21:14:38 +01:00
|
|
|
params['url'] = search_url.format(offset=offset,
|
2015-05-30 17:41:40 +02:00
|
|
|
query=urlencode({'q': query}),
|
2016-03-25 18:38:02 +01:00
|
|
|
hostname=google_hostname,
|
|
|
|
lang=url_lang)
|
2014-09-14 14:40:55 +02:00
|
|
|
|
|
|
|
params['headers']['Accept-Language'] = language
|
2015-05-30 17:41:40 +02:00
|
|
|
params['headers']['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
|
|
|
|
|
|
|
|
params['google_hostname'] = google_hostname
|
2014-09-01 15:10:05 +02:00
|
|
|
|
2014-01-29 19:28:38 +01:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-01 15:10:05 +02:00
|
|
|
# get response from search-request
|
2014-01-29 19:28:38 +01:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-09-01 15:10:05 +02:00
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
# detect google sorry
|
|
|
|
resp_url = urlparse(resp.url)
|
|
|
|
if resp_url.netloc == 'sorry.google.com' or resp_url.path == '/sorry/IndexRedirect':
|
|
|
|
raise RuntimeWarning('sorry.google.com')
|
|
|
|
|
|
|
|
# which hostname ?
|
|
|
|
google_hostname = resp.search_params.get('google_hostname')
|
|
|
|
google_url = "https://" + google_hostname
|
|
|
|
|
|
|
|
# convert the text to dom
|
2014-09-14 14:40:55 +02:00
|
|
|
dom = html.fromstring(resp.text)
|
2014-01-29 19:28:38 +01:00
|
|
|
|
2014-09-01 15:10:05 +02:00
|
|
|
# parse results
|
2014-09-14 14:40:55 +02:00
|
|
|
for result in dom.xpath(results_xpath):
|
|
|
|
try:
|
2015-10-29 12:47:12 +01:00
|
|
|
title = extract_text(result.xpath(title_xpath)[0])
|
2015-05-30 17:41:40 +02:00
|
|
|
url = parse_url(extract_url(result.xpath(url_xpath), google_url), google_hostname)
|
|
|
|
parsed_url = urlparse(url, google_hostname)
|
2015-06-05 11:23:24 +02:00
|
|
|
|
|
|
|
# map result
|
2016-01-18 13:08:46 +01:00
|
|
|
if parsed_url.netloc == google_hostname:
|
|
|
|
# TODO fix inside links
|
|
|
|
continue
|
|
|
|
# if parsed_url.path.startswith(maps_path) or parsed_url.netloc.startswith(map_hostname_start):
|
|
|
|
# print "yooooo"*30
|
|
|
|
# x = result.xpath(map_near)
|
|
|
|
# if len(x) > 0:
|
|
|
|
# # map : near the location
|
|
|
|
# results = results + parse_map_near(parsed_url, x, google_hostname)
|
|
|
|
# else:
|
|
|
|
# # map : detail about a location
|
|
|
|
# results = results + parse_map_detail(parsed_url, result, google_hostname)
|
|
|
|
# # google news
|
|
|
|
# elif parsed_url.path == search_path:
|
|
|
|
# # skipping news results
|
|
|
|
# pass
|
|
|
|
|
|
|
|
# # images result
|
|
|
|
# elif parsed_url.path == images_path:
|
|
|
|
# # only thumbnail image provided,
|
|
|
|
# # so skipping image results
|
|
|
|
# # results = results + parse_images(result, google_hostname)
|
|
|
|
# pass
|
2015-06-05 11:23:24 +02:00
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
else:
|
|
|
|
# normal result
|
2015-05-30 17:41:40 +02:00
|
|
|
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
|
2014-09-14 14:40:55 +02:00
|
|
|
# append result
|
2014-12-05 20:03:16 +01:00
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
2015-06-06 00:18:00 +02:00
|
|
|
'content': content
|
|
|
|
})
|
|
|
|
except:
|
2015-10-29 12:47:12 +01:00
|
|
|
logger.debug('result parse error in:\n%s', etree.tostring(result, pretty_print=True))
|
2014-09-14 14:40:55 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
# parse suggestion
|
|
|
|
for suggestion in dom.xpath(suggestion_xpath):
|
|
|
|
# append suggestion
|
2015-09-30 16:42:03 +02:00
|
|
|
results.append({'suggestion': escape(extract_text(suggestion))})
|
2014-09-01 15:10:05 +02:00
|
|
|
|
|
|
|
# return results
|
2014-01-29 19:28:38 +01:00
|
|
|
return results
|
2014-09-14 14:40:55 +02:00
|
|
|
|
2014-12-05 20:03:16 +01:00
|
|
|
|
2015-05-30 17:41:40 +02:00
|
|
|
def parse_images(result, google_hostname):
|
2014-09-14 14:40:55 +02:00
|
|
|
results = []
|
|
|
|
for image in result.xpath(images_xpath):
|
2015-05-30 17:41:40 +02:00
|
|
|
url = parse_url(extract_text(image.xpath(image_url_xpath)[0]), google_hostname)
|
2014-09-14 14:40:55 +02:00
|
|
|
img_src = extract_text(image.xpath(image_img_src_xpath)[0])
|
2014-12-05 20:03:16 +01:00
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
# append result
|
|
|
|
results.append({'url': url,
|
|
|
|
'title': '',
|
|
|
|
'content': '',
|
|
|
|
'img_src': img_src,
|
2015-06-05 23:56:23 +02:00
|
|
|
'template': 'images.html'
|
|
|
|
})
|
2014-09-14 14:40:55 +02:00
|
|
|
|
|
|
|
return results
|
2015-06-05 11:23:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_map_near(parsed_url, x, google_hostname):
|
|
|
|
results = []
|
|
|
|
|
|
|
|
for result in x:
|
|
|
|
title = extract_text_from_dom(result, map_near_title)
|
|
|
|
url = parse_url(extract_text_from_dom(result, map_near_url), google_hostname)
|
2015-06-05 23:56:23 +02:00
|
|
|
attributes = []
|
2015-06-05 11:23:24 +02:00
|
|
|
phone = extract_text_from_dom(result, map_near_phone)
|
2015-06-05 23:56:23 +02:00
|
|
|
add_attributes(attributes, property_phone, phone, 'tel:' + phone)
|
|
|
|
results.append({'title': title,
|
|
|
|
'url': url,
|
|
|
|
'content': attributes_to_html(attributes)
|
|
|
|
})
|
2015-06-05 11:23:24 +02:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def parse_map_detail(parsed_url, result, google_hostname):
|
|
|
|
results = []
|
|
|
|
|
|
|
|
# try to parse the geoloc
|
|
|
|
m = re.search('@([0-9\.]+),([0-9\.]+),([0-9]+)', parsed_url.path)
|
|
|
|
if m is None:
|
|
|
|
m = re.search('ll\=([0-9\.]+),([0-9\.]+)\&z\=([0-9]+)', parsed_url.query)
|
|
|
|
|
|
|
|
if m is not None:
|
2015-06-05 23:56:23 +02:00
|
|
|
# geoloc found (ignored)
|
|
|
|
lon = float(m.group(2)) # noqa
|
|
|
|
lat = float(m.group(1)) # noqa
|
|
|
|
zoom = int(m.group(3)) # noqa
|
2015-06-05 11:23:24 +02:00
|
|
|
|
|
|
|
# attributes
|
|
|
|
attributes = []
|
2015-06-05 23:56:23 +02:00
|
|
|
address = extract_text_from_dom(result, map_address_xpath)
|
|
|
|
phone = extract_text_from_dom(result, map_phone_xpath)
|
|
|
|
add_attributes(attributes, property_address, address, 'geo:' + str(lat) + ',' + str(lon))
|
|
|
|
add_attributes(attributes, property_phone, phone, 'tel:' + phone)
|
2015-06-05 11:23:24 +02:00
|
|
|
|
|
|
|
# title / content / url
|
|
|
|
website_title = extract_text_from_dom(result, map_website_title_xpath)
|
|
|
|
content = extract_text_from_dom(result, content_xpath)
|
|
|
|
website_url = parse_url(extract_text_from_dom(result, map_website_url_xpath), google_hostname)
|
|
|
|
|
2015-06-05 23:56:23 +02:00
|
|
|
# add a result if there is a website
|
2015-06-05 11:23:24 +02:00
|
|
|
if website_url is not None:
|
2015-06-05 23:56:23 +02:00
|
|
|
results.append({'title': website_title,
|
|
|
|
'content': (content + '<br />' if content is not None else '')
|
|
|
|
+ attributes_to_html(attributes),
|
|
|
|
'url': website_url
|
2015-06-05 11:23:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2015-06-05 23:56:23 +02:00
|
|
|
def add_attributes(attributes, name, value, url):
|
2015-06-05 11:23:24 +02:00
|
|
|
if value is not None and len(value) > 0:
|
2015-06-05 23:56:23 +02:00
|
|
|
attributes.append({'label': name, 'value': value, 'url': url})
|
2015-06-05 11:23:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
def attributes_to_html(attributes):
|
|
|
|
retval = '<table class="table table-striped">'
|
|
|
|
for a in attributes:
|
2015-06-05 23:56:23 +02:00
|
|
|
value = a.get('value')
|
|
|
|
if 'url' in a:
|
|
|
|
value = '<a href="' + a.get('url') + '">' + value + '</a>'
|
|
|
|
retval = retval + '<tr><th>' + a.get('label') + '</th><td>' + value + '</td></tr>'
|
2015-06-05 11:23:24 +02:00
|
|
|
retval = retval + '</table>'
|
|
|
|
return retval
|