2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-05-02 13:45:17 +00:00
|
|
|
"""
|
|
|
|
Google (News)
|
|
|
|
"""
|
2014-03-04 12:11:04 +00:00
|
|
|
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlencode
|
2016-12-11 00:03:03 +00:00
|
|
|
from lxml import html
|
2018-03-01 04:30:48 +00:00
|
|
|
from searx.utils import match_language
|
2020-11-16 08:43:23 +00:00
|
|
|
from searx.engines.google import _fetch_supported_languages, supported_languages_url # NOQA # pylint: disable=unused-import
|
2014-03-04 12:11:04 +00:00
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://news.google.com',
|
|
|
|
"wikidata_id": 'Q12020',
|
|
|
|
"official_api_documentation": None,
|
|
|
|
"use_official_api": False,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'HTML',
|
|
|
|
}
|
|
|
|
|
2014-09-01 13:10:05 +00:00
|
|
|
# search-url
|
2014-03-04 12:11:04 +00:00
|
|
|
categories = ['news']
|
2014-09-01 13:10:05 +00:00
|
|
|
paging = True
|
|
|
|
language_support = True
|
2016-12-11 00:03:03 +00:00
|
|
|
safesearch = True
|
|
|
|
time_range_support = True
|
|
|
|
number_of_results = 10
|
2014-03-04 12:11:04 +00:00
|
|
|
|
2016-12-11 00:03:03 +00:00
|
|
|
search_url = 'https://www.google.com/search'\
|
|
|
|
'?{query}'\
|
|
|
|
'&tbm=nws'\
|
|
|
|
'&gws_rd=cr'\
|
|
|
|
'&{search_options}'
|
|
|
|
time_range_attr = "qdr:{range}"
|
|
|
|
time_range_dict = {'day': 'd',
|
|
|
|
'week': 'w',
|
2016-12-11 15:39:12 +00:00
|
|
|
'month': 'm',
|
|
|
|
'year': 'y'}
|
2014-03-04 12:11:04 +00:00
|
|
|
|
|
|
|
|
2014-09-01 13:10:05 +00:00
|
|
|
# do search-request
|
2014-03-04 12:11:04 +00:00
|
|
|
def request(query, params):
|
2014-09-01 13:10:05 +00:00
|
|
|
|
2016-12-11 00:03:03 +00:00
|
|
|
search_options = {
|
|
|
|
'start': (params['pageno'] - 1) * number_of_results
|
|
|
|
}
|
|
|
|
|
|
|
|
if params['time_range'] in time_range_dict:
|
|
|
|
search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
|
|
|
|
|
|
|
|
if safesearch and params['safesearch']:
|
|
|
|
search_options['safe'] = 'on'
|
2014-09-01 13:10:05 +00:00
|
|
|
|
2016-12-11 00:03:03 +00:00
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}),
|
|
|
|
search_options=urlencode(search_options))
|
|
|
|
|
2019-01-06 14:27:46 +00:00
|
|
|
if params['language'] != 'all':
|
|
|
|
language = match_language(params['language'], supported_languages, language_aliases).split('-')[0]
|
|
|
|
if language:
|
2020-02-25 17:44:28 +00:00
|
|
|
params['url'] += '&hl=' + language
|
2014-09-01 13:10:05 +00:00
|
|
|
|
2014-03-04 12:11:04 +00:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-01 13:10:05 +00:00
|
|
|
# get response from search-request
|
2014-03-04 12:11:04 +00:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
2014-09-01 13:10:05 +00:00
|
|
|
|
2016-12-11 00:03:03 +00:00
|
|
|
dom = html.fromstring(resp.text)
|
2014-03-04 12:11:04 +00:00
|
|
|
|
2014-09-01 13:10:05 +00:00
|
|
|
# parse results
|
2016-12-11 00:03:03 +00:00
|
|
|
for result in dom.xpath('//div[@class="g"]|//div[@class="g _cy"]'):
|
2017-01-10 10:03:05 +00:00
|
|
|
try:
|
|
|
|
r = {
|
2018-04-09 01:56:05 +00:00
|
|
|
'url': result.xpath('.//a[@class="l lLrAF"]')[0].attrib.get("href"),
|
|
|
|
'title': ''.join(result.xpath('.//a[@class="l lLrAF"]//text()')),
|
2017-01-10 10:03:05 +00:00
|
|
|
'content': ''.join(result.xpath('.//div[@class="st"]//text()')),
|
|
|
|
}
|
|
|
|
except:
|
|
|
|
continue
|
2016-12-11 00:03:03 +00:00
|
|
|
|
2016-12-23 11:59:03 +00:00
|
|
|
imgs = result.xpath('.//img/@src')
|
|
|
|
if len(imgs) and not imgs[0].startswith('data'):
|
|
|
|
r['img_src'] = imgs[0]
|
2016-12-11 00:03:03 +00:00
|
|
|
|
|
|
|
results.append(r)
|
2014-09-01 13:10:05 +00:00
|
|
|
|
|
|
|
# return results
|
2014-03-04 12:11:04 +00:00
|
|
|
return results
|