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
|
|
|
|
|
|
|
from urllib import urlencode
|
2014-12-05 20:03:16 +01:00
|
|
|
from urlparse import urlparse, parse_qsl
|
2014-09-14 14:40:55 +02:00
|
|
|
from lxml import html
|
|
|
|
from searx.engines.xpath import extract_text, extract_url
|
2014-01-29 19:28:38 +01: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
|
2014-01-29 19:28:38 +01:00
|
|
|
|
2014-09-01 15:10:05 +02:00
|
|
|
# search-url
|
2014-09-14 14:40:55 +02:00
|
|
|
google_hostname = 'www.google.com'
|
|
|
|
search_path = '/search'
|
|
|
|
redirect_path = '/url'
|
|
|
|
images_path = '/images'
|
2014-12-05 20:03:16 +01:00
|
|
|
search_url = ('https://' +
|
|
|
|
google_hostname +
|
|
|
|
search_path +
|
|
|
|
'?{query}&start={offset}&gbv=1')
|
2014-01-31 04:35:23 +01:00
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
# specific xpath variables
|
2014-12-05 20:03:16 +01:00
|
|
|
results_xpath = '//li[@class="g"]'
|
2014-09-14 14:40:55 +02:00
|
|
|
url_xpath = './/h3/a/@href'
|
|
|
|
title_xpath = './/h3'
|
|
|
|
content_xpath = './/span[@class="st"]'
|
|
|
|
suggestion_xpath = '//p[@class="_Bmc"]'
|
|
|
|
|
|
|
|
images_xpath = './/div/a'
|
|
|
|
image_url_xpath = './@href'
|
|
|
|
image_img_src_xpath = './img/@src'
|
|
|
|
|
2014-12-05 20:03:16 +01:00
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
# remove google-specific tracking-url
|
|
|
|
def parse_url(url_string):
|
|
|
|
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
|
|
|
|
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'
|
|
|
|
else:
|
2014-12-05 20:03:16 +01:00
|
|
|
language = params['language'].replace('_', '-').lower()
|
2014-09-01 15:10:05 +02:00
|
|
|
|
2014-01-29 21:14:38 +01:00
|
|
|
params['url'] = search_url.format(offset=offset,
|
2014-09-14 14:40:55 +02:00
|
|
|
query=urlencode({'q': query}))
|
|
|
|
|
|
|
|
params['headers']['Accept-Language'] = language
|
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
|
|
|
|
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):
|
|
|
|
title = extract_text(result.xpath(title_xpath)[0])
|
|
|
|
try:
|
|
|
|
url = parse_url(extract_url(result.xpath(url_xpath), search_url))
|
|
|
|
parsed_url = urlparse(url)
|
2014-12-05 20:03:16 +01:00
|
|
|
if (parsed_url.netloc == google_hostname
|
|
|
|
and parsed_url.path == search_path):
|
2014-09-14 14:40:55 +02:00
|
|
|
# remove the link to google news
|
|
|
|
continue
|
|
|
|
|
2014-12-09 00:53:09 +01:00
|
|
|
# images result
|
2014-12-05 20:03:16 +01:00
|
|
|
if (parsed_url.netloc == google_hostname
|
|
|
|
and parsed_url.path == images_path):
|
2014-12-09 00:53:09 +01:00
|
|
|
# only thumbnail image provided,
|
|
|
|
# so skipping image results
|
|
|
|
# results = results + parse_images(result)
|
|
|
|
pass
|
2014-09-14 14:40:55 +02:00
|
|
|
else:
|
|
|
|
# normal result
|
|
|
|
content = extract_text(result.xpath(content_xpath)[0])
|
|
|
|
# append result
|
2014-12-05 20:03:16 +01:00
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
2014-09-14 14:40:55 +02:00
|
|
|
'content': content})
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# parse suggestion
|
|
|
|
for suggestion in dom.xpath(suggestion_xpath):
|
|
|
|
# append suggestion
|
|
|
|
results.append({'suggestion': 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
|
|
|
|
2014-09-14 14:40:55 +02:00
|
|
|
def parse_images(result):
|
|
|
|
results = []
|
|
|
|
for image in result.xpath(images_xpath):
|
|
|
|
url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
|
|
|
|
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,
|
|
|
|
'template': 'images.html'})
|
|
|
|
|
|
|
|
return results
|