2020-12-16 13:41:32 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-27 15:13:39 +02:00
|
|
|
# lint: pylint
|
|
|
|
|
|
|
|
"""Processores for engine-type: ``online``
|
|
|
|
|
|
|
|
"""
|
2023-02-10 13:40:12 +01:00
|
|
|
# pylint: disable=use-dict-literal
|
2020-12-16 13:41:32 +01:00
|
|
|
|
2021-05-05 13:08:54 +02:00
|
|
|
from timeit import default_timer
|
2021-03-18 19:59:01 +01:00
|
|
|
import asyncio
|
2022-07-19 23:40:11 +02:00
|
|
|
import ssl
|
2021-03-18 19:59:01 +01:00
|
|
|
import httpx
|
2020-12-16 13:41:32 +01:00
|
|
|
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 10:43:33 +02:00
|
|
|
import searx.network
|
2020-12-16 13:41:32 +01:00
|
|
|
from searx.utils import gen_useragent
|
2021-04-27 15:13:39 +02:00
|
|
|
from searx.exceptions import (
|
|
|
|
SearxEngineAccessDeniedException,
|
|
|
|
SearxEngineCaptchaException,
|
|
|
|
SearxEngineTooManyRequestsException,
|
|
|
|
)
|
2021-04-14 17:23:15 +02:00
|
|
|
from searx.metrics.error_recorder import count_error
|
2021-04-27 15:13:39 +02:00
|
|
|
from .abstract import EngineProcessor
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
|
2020-12-17 16:49:48 +01:00
|
|
|
def default_request_params():
|
2021-04-27 15:13:39 +02:00
|
|
|
"""Default request parameters for ``online`` engines."""
|
2020-12-17 16:49:48 +01:00
|
|
|
return {
|
2021-12-27 09:16:03 +01:00
|
|
|
# fmt: off
|
2020-12-17 16:49:48 +01:00
|
|
|
'method': 'GET',
|
|
|
|
'headers': {},
|
|
|
|
'data': {},
|
|
|
|
'url': '',
|
|
|
|
'cookies': {},
|
|
|
|
'auth': None
|
2021-12-27 09:16:03 +01:00
|
|
|
# fmt: on
|
2020-12-17 16:49:48 +01:00
|
|
|
}
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class OnlineProcessor(EngineProcessor):
|
2021-04-27 15:13:39 +02:00
|
|
|
"""Processor class for ``online`` engines."""
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
engine_type = 'online'
|
|
|
|
|
2021-05-05 13:08:54 +02:00
|
|
|
def initialize(self):
|
|
|
|
# set timeout for all HTTP requests
|
|
|
|
searx.network.set_timeout_for_thread(self.engine.timeout, start_time=default_timer())
|
|
|
|
# reset the HTTP total time
|
|
|
|
searx.network.reset_time_for_thread()
|
|
|
|
# set the network
|
|
|
|
searx.network.set_context_network_name(self.engine_name)
|
|
|
|
super().initialize()
|
|
|
|
|
2020-12-16 13:41:32 +01:00
|
|
|
def get_params(self, search_query, engine_category):
|
|
|
|
params = super().get_params(search_query, engine_category)
|
|
|
|
if params is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# add default params
|
2020-12-17 16:49:48 +01:00
|
|
|
params.update(default_request_params())
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
# add an user agent
|
|
|
|
params['headers']['User-Agent'] = gen_useragent()
|
|
|
|
|
2022-08-01 17:01:59 +02:00
|
|
|
# add Accept-Language header
|
|
|
|
if self.engine.send_accept_language_header and search_query.locale:
|
|
|
|
ac_lang = search_query.locale.language
|
|
|
|
if search_query.locale.territory:
|
|
|
|
ac_lang = "%s-%s,%s;q=0.9,*;q=0.5" % (
|
|
|
|
search_query.locale.language,
|
|
|
|
search_query.locale.territory,
|
|
|
|
search_query.locale.language,
|
|
|
|
)
|
|
|
|
params['headers']['Accept-Language'] = ac_lang
|
|
|
|
|
2020-12-16 13:41:32 +01:00
|
|
|
return params
|
|
|
|
|
|
|
|
def _send_http_request(self, params):
|
|
|
|
# create dictionary which contain all
|
2022-09-27 17:01:00 +02:00
|
|
|
# information about the request
|
2022-07-19 23:40:11 +02:00
|
|
|
request_args = dict(headers=params['headers'], cookies=params['cookies'], auth=params['auth'])
|
|
|
|
|
|
|
|
# verify
|
|
|
|
# if not None, it overrides the verify value defined in the network.
|
|
|
|
# use False to accept any server certificate
|
|
|
|
# use a path to file to specify a server certificate
|
|
|
|
verify = params.get('verify')
|
|
|
|
if verify is not None:
|
|
|
|
request_args['verify'] = params['verify']
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
# max_redirects
|
|
|
|
max_redirects = params.get('max_redirects')
|
|
|
|
if max_redirects:
|
|
|
|
request_args['max_redirects'] = max_redirects
|
|
|
|
|
2021-02-09 12:07:19 +01:00
|
|
|
# allow_redirects
|
|
|
|
if 'allow_redirects' in params:
|
|
|
|
request_args['allow_redirects'] = params['allow_redirects']
|
|
|
|
|
2020-12-16 13:41:32 +01:00
|
|
|
# soft_max_redirects
|
|
|
|
soft_max_redirects = params.get('soft_max_redirects', max_redirects or 0)
|
|
|
|
|
|
|
|
# raise_for_status
|
2021-02-09 11:27:41 +01:00
|
|
|
request_args['raise_for_httperror'] = params.get('raise_for_httperror', True)
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
# specific type of request (GET or POST)
|
|
|
|
if params['method'] == 'GET':
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 10:43:33 +02:00
|
|
|
req = searx.network.get
|
2020-12-16 13:41:32 +01:00
|
|
|
else:
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 10:43:33 +02:00
|
|
|
req = searx.network.post
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
request_args['data'] = params['data']
|
|
|
|
|
|
|
|
# send the request
|
|
|
|
response = req(params['url'], **request_args)
|
|
|
|
|
|
|
|
# check soft limit of the redirect count
|
|
|
|
if len(response.history) > soft_max_redirects:
|
|
|
|
# unexpected redirect : record an error
|
|
|
|
# but the engine might still return valid results.
|
|
|
|
status_code = str(response.status_code or '')
|
2021-03-18 19:59:01 +01:00
|
|
|
reason = response.reason_phrase or ''
|
|
|
|
hostname = response.url.host
|
2021-12-27 09:26:22 +01:00
|
|
|
count_error(
|
|
|
|
self.engine_name,
|
|
|
|
'{} redirects, maximum: {}'.format(len(response.history), soft_max_redirects),
|
|
|
|
(status_code, reason, hostname),
|
|
|
|
secondary=True,
|
|
|
|
)
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def _search_basic(self, query, params):
|
|
|
|
# update request parameters dependent on
|
|
|
|
# search-engine (contained in engines folder)
|
|
|
|
self.engine.request(query, params)
|
|
|
|
|
|
|
|
# ignoring empty urls
|
|
|
|
if params['url'] is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not params['url']:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# send request
|
|
|
|
response = self._send_http_request(params)
|
|
|
|
|
|
|
|
# parse the response
|
|
|
|
response.search_params = params
|
|
|
|
return self.engine.response(response)
|
|
|
|
|
|
|
|
def search(self, query, params, result_container, start_time, timeout_limit):
|
|
|
|
# set timeout for all HTTP requests
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 10:43:33 +02:00
|
|
|
searx.network.set_timeout_for_thread(timeout_limit, start_time=start_time)
|
2020-12-16 13:41:32 +01:00
|
|
|
# reset the HTTP total time
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 10:43:33 +02:00
|
|
|
searx.network.reset_time_for_thread()
|
|
|
|
# set the network
|
|
|
|
searx.network.set_context_network_name(self.engine_name)
|
2020-12-16 13:41:32 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
# send requests and parse the results
|
|
|
|
search_results = self._search_basic(query, params)
|
2021-04-13 15:21:53 +02:00
|
|
|
self.extend_container(result_container, start_time, search_results)
|
2022-07-19 23:40:11 +02:00
|
|
|
except ssl.SSLError as e:
|
|
|
|
# requests timeout (connect or read)
|
|
|
|
self.handle_exception(result_container, e, suspend=True)
|
|
|
|
self.logger.error("SSLError {}, verify={}".format(e, searx.network.get_network(self.engine_name).verify))
|
2021-04-13 15:21:53 +02:00
|
|
|
except (httpx.TimeoutException, asyncio.TimeoutError) as e:
|
|
|
|
# requests timeout (connect or read)
|
2021-04-26 11:12:02 +02:00
|
|
|
self.handle_exception(result_container, e, suspend=True)
|
2021-09-06 19:46:08 +02:00
|
|
|
self.logger.error(
|
2021-12-27 09:26:22 +01:00
|
|
|
"HTTP requests timeout (search duration : {0} s, timeout: {1} s) : {2}".format(
|
|
|
|
default_timer() - start_time, timeout_limit, e.__class__.__name__
|
2021-09-06 19:46:08 +02:00
|
|
|
)
|
|
|
|
)
|
2021-04-13 15:21:53 +02:00
|
|
|
except (httpx.HTTPError, httpx.StreamError) as e:
|
|
|
|
# other requests exception
|
2021-04-26 11:12:02 +02:00
|
|
|
self.handle_exception(result_container, e, suspend=True)
|
2021-09-06 19:46:08 +02:00
|
|
|
self.logger.exception(
|
2021-12-27 09:26:22 +01:00
|
|
|
"requests exception (search duration : {0} s, timeout: {1} s) : {2}".format(
|
|
|
|
default_timer() - start_time, timeout_limit, e
|
2021-09-06 19:46:08 +02:00
|
|
|
)
|
|
|
|
)
|
2021-04-13 15:21:53 +02:00
|
|
|
except SearxEngineCaptchaException as e:
|
2021-04-26 11:12:02 +02:00
|
|
|
self.handle_exception(result_container, e, suspend=True)
|
2021-09-06 19:46:08 +02:00
|
|
|
self.logger.exception('CAPTCHA')
|
2021-04-13 15:21:53 +02:00
|
|
|
except SearxEngineTooManyRequestsException as e:
|
2022-06-11 17:20:56 +02:00
|
|
|
if "google" in self.engine_name:
|
2022-07-05 23:39:23 +02:00
|
|
|
self.logger.warn(
|
|
|
|
"Set to 'true' the use_mobile_ui parameter in the 'engines:'"
|
|
|
|
" section of your settings.yml file if google is blocked for you."
|
|
|
|
)
|
2021-04-26 11:12:02 +02:00
|
|
|
self.handle_exception(result_container, e, suspend=True)
|
2021-09-06 19:46:08 +02:00
|
|
|
self.logger.exception('Too many requests')
|
2021-04-13 15:21:53 +02:00
|
|
|
except SearxEngineAccessDeniedException as e:
|
2021-04-26 11:12:02 +02:00
|
|
|
self.handle_exception(result_container, e, suspend=True)
|
2021-09-06 19:46:08 +02:00
|
|
|
self.logger.exception('Searx is blocked')
|
2021-04-27 15:13:39 +02:00
|
|
|
except Exception as e: # pylint: disable=broad-except
|
2021-04-26 11:12:02 +02:00
|
|
|
self.handle_exception(result_container, e)
|
2021-09-06 19:46:08 +02:00
|
|
|
self.logger.exception('exception : {0}'.format(e))
|
2020-12-24 09:28:16 +01:00
|
|
|
|
|
|
|
def get_default_tests(self):
|
|
|
|
tests = {}
|
|
|
|
|
|
|
|
tests['simple'] = {
|
2021-01-08 19:05:56 +01:00
|
|
|
'matrix': {'query': ('life', 'computer')},
|
2020-12-24 09:28:16 +01:00
|
|
|
'result_container': ['not_empty'],
|
|
|
|
}
|
|
|
|
|
|
|
|
if getattr(self.engine, 'paging', False):
|
|
|
|
tests['paging'] = {
|
2021-12-27 09:26:22 +01:00
|
|
|
'matrix': {'query': 'time', 'pageno': (1, 2, 3)},
|
2020-12-24 09:28:16 +01:00
|
|
|
'result_container': ['not_empty'],
|
2021-12-27 09:26:22 +01:00
|
|
|
'test': ['unique_results'],
|
2020-12-24 09:28:16 +01:00
|
|
|
}
|
2021-01-08 19:05:56 +01:00
|
|
|
if 'general' in self.engine.categories:
|
|
|
|
# avoid documentation about HTML tags (<time> and <input type="time">)
|
|
|
|
tests['paging']['matrix']['query'] = 'news'
|
2020-12-24 09:28:16 +01:00
|
|
|
|
|
|
|
if getattr(self.engine, 'time_range', False):
|
|
|
|
tests['time_range'] = {
|
2021-12-27 09:26:22 +01:00
|
|
|
'matrix': {'query': 'news', 'time_range': (None, 'day')},
|
2020-12-24 09:28:16 +01:00
|
|
|
'result_container': ['not_empty'],
|
2021-12-27 09:26:22 +01:00
|
|
|
'test': ['unique_results'],
|
2020-12-24 09:28:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 21:29:31 +01:00
|
|
|
if getattr(self.engine, 'supported_languages', []):
|
2020-12-24 09:28:16 +01:00
|
|
|
tests['lang_fr'] = {
|
|
|
|
'matrix': {'query': 'paris', 'lang': 'fr'},
|
2021-01-19 21:29:31 +01:00
|
|
|
'result_container': ['not_empty', ('has_language', 'fr')],
|
2020-12-24 09:28:16 +01:00
|
|
|
}
|
|
|
|
tests['lang_en'] = {
|
|
|
|
'matrix': {'query': 'paris', 'lang': 'en'},
|
2021-01-19 21:29:31 +01:00
|
|
|
'result_container': ['not_empty', ('has_language', 'en')],
|
2020-12-24 09:28:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if getattr(self.engine, 'safesearch', False):
|
2021-12-27 09:26:22 +01:00
|
|
|
tests['safesearch'] = {'matrix': {'query': 'porn', 'safesearch': (0, 2)}, 'test': ['unique_results']}
|
2020-12-24 09:28:16 +01:00
|
|
|
|
|
|
|
return tests
|