| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  | """This module implements the engine loader.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Load and initialize the ``engines``, see :py:func:`load_engines` and register | 
					
						
							|  |  |  | :py:obj:`engine_shortcuts`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | usage:: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     load_engines( settings['engines'] ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2013-10-17 00:32:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-05 20:24:31 +01:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | import copy | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | from typing import Dict, List, Optional | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-06 22:20:20 +02:00
										 |  |  | from os.path import realpath, dirname | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  | from babel.localedata import locale_identifiers | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | from searx import logger, settings | 
					
						
							| 
									
										
										
										
											2020-10-05 13:50:33 +02:00
										 |  |  | from searx.data import ENGINES_LANGUAGES | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | from searx.network import get | 
					
						
							|  |  |  | from searx.utils import load_module, match_language, gen_useragent | 
					
						
							| 
									
										
										
										
											2015-01-09 04:13:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = logger.getChild('engines') | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | ENGINE_DIR = dirname(realpath(__file__)) | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  | BABEL_LANGS = [ | 
					
						
							|  |  |  |     lang_parts[0] + '-' + lang_parts[-1] if len(lang_parts) > 1 else lang_parts[0] | 
					
						
							|  |  |  |     for lang_parts in (lang_code.split('_') for lang_code in locale_identifiers()) | 
					
						
							|  |  |  | ] | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | ENGINE_DEFAULT_ARGS = { | 
					
						
							|  |  |  |     "engine_type": "online", | 
					
						
							|  |  |  |     "inactive": False, | 
					
						
							|  |  |  |     "disabled": False, | 
					
						
							|  |  |  |     "timeout": settings["outgoing"]["request_timeout"], | 
					
						
							|  |  |  |     "shortcut": "-", | 
					
						
							|  |  |  |     "categories": ["general"], | 
					
						
							|  |  |  |     "supported_languages": [], | 
					
						
							|  |  |  |     "language_aliases": {}, | 
					
						
							|  |  |  |     "paging": False, | 
					
						
							|  |  |  |     "safesearch": False, | 
					
						
							|  |  |  |     "time_range_support": False, | 
					
						
							|  |  |  |     "enable_http": False, | 
					
						
							| 
									
										
										
										
											2022-01-22 19:51:40 +01:00
										 |  |  |     "using_tor_proxy": False, | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     "display_error_messages": True, | 
					
						
							|  |  |  |     "tokens": [], | 
					
						
							| 
									
										
										
										
											2022-01-10 08:40:01 +01:00
										 |  |  |     "about": {}, | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-28 16:12:54 +01:00
										 |  |  | # set automatically when an engine does not have any tab category | 
					
						
							|  |  |  | OTHER_CATEGORY = 'other' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 15:28:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Engine:  # pylint: disable=too-few-public-methods | 
					
						
							|  |  |  |     """This class is currently never initialized and only used for type hinting.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     name: str | 
					
						
							|  |  |  |     engine: str | 
					
						
							|  |  |  |     shortcut: str | 
					
						
							|  |  |  |     categories: List[str] | 
					
						
							|  |  |  |     supported_languages: List[str] | 
					
						
							|  |  |  |     about: dict | 
					
						
							|  |  |  |     inactive: bool | 
					
						
							|  |  |  |     disabled: bool | 
					
						
							|  |  |  |     language_support: bool | 
					
						
							|  |  |  |     paging: bool | 
					
						
							|  |  |  |     safesearch: bool | 
					
						
							|  |  |  |     time_range_support: bool | 
					
						
							|  |  |  |     timeout: float | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | # Defaults for the namespace of an engine module, see :py:func:`load_engine` | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  | categories = {'general': []} | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | engines: Dict[str, Engine] = {} | 
					
						
							| 
									
										
										
										
											2014-01-31 15:45:18 +01:00
										 |  |  | engine_shortcuts = {} | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  | """Simple map of registered *shortcuts* to name of the engine (or ``None``).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: | 
					
						
							| 
									
										
										
										
											2014-01-31 15:45:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |     engine_shortcuts[engine.shortcut] = engine.name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-01-19 22:59:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | def load_engine(engine_data: dict) -> Optional[Engine]: | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |     """Load engine from ``engine_data``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     :param dict engine_data:  Attributes from YAML ``settings:engines/<engine>`` | 
					
						
							|  |  |  |     :return: initialized namespace of the ``<engine>``. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     1. create a namespace and load module of the ``<engine>`` | 
					
						
							|  |  |  |     2. update namespace with the defaults from :py:obj:`ENGINE_DEFAULT_ARGS` | 
					
						
							|  |  |  |     3. update namespace with values from ``engine_data`` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If engine *is active*, return namespace of the engine, otherwise return | 
					
						
							|  |  |  |     ``None``. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     This function also returns ``None`` if initialization of the namespace fails | 
					
						
							|  |  |  |     for one of the following reasons: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - engine name contains underscore | 
					
						
							|  |  |  |     - engine name is not lowercase | 
					
						
							|  |  |  |     - required attribute is not set :py:func:`is_missing_required_attributes` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:04:00 +02:00
										 |  |  |     engine_name = engine_data['name'] | 
					
						
							|  |  |  |     if '_' in engine_name: | 
					
						
							|  |  |  |         logger.error('Engine name contains underscore: "{}"'.format(engine_name)) | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2016-09-28 22:30:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-26 09:04:00 +02:00
										 |  |  |     if engine_name.lower() != engine_name: | 
					
						
							|  |  |  |         logger.warn('Engine name is not lowercase: "{}", converting to lowercase'.format(engine_name)) | 
					
						
							|  |  |  |         engine_name = engine_name.lower() | 
					
						
							|  |  |  |         engine_data['name'] = engine_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     # load_module | 
					
						
							| 
									
										
										
										
											2016-09-28 22:30:05 +02:00
										 |  |  |     engine_module = engine_data['engine'] | 
					
						
							| 
									
										
										
										
											2020-08-31 18:59:27 +02:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |         engine = load_module(engine_module + '.py', ENGINE_DIR) | 
					
						
							| 
									
										
										
										
											2020-11-16 09:37:13 +01:00
										 |  |  |     except (SyntaxError, KeyboardInterrupt, SystemExit, SystemError, ImportError, RuntimeError): | 
					
						
							| 
									
										
										
										
											2020-09-07 15:39:26 +02:00
										 |  |  |         logger.exception('Fatal exception in engine "{}"'.format(engine_module)) | 
					
						
							|  |  |  |         sys.exit(1) | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     except BaseException: | 
					
						
							| 
									
										
										
										
											2020-08-31 18:59:27 +02:00
										 |  |  |         logger.exception('Cannot load engine "{}"'.format(engine_module)) | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2014-01-31 04:35:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     update_engine_attributes(engine, engine_data) | 
					
						
							|  |  |  |     set_language_attributes(engine) | 
					
						
							|  |  |  |     update_attributes_for_tor(engine) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not is_engine_active(engine): | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if is_missing_required_attributes(engine): | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 17:21:48 +02:00
										 |  |  |     set_loggers(engine, engine_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 16:12:54 +01:00
										 |  |  |     if not any(cat in settings['categories_as_tabs'] for cat in engine.categories): | 
					
						
							|  |  |  |         engine.categories.append(OTHER_CATEGORY) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     return engine | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 17:21:48 +02:00
										 |  |  | def set_loggers(engine, engine_name): | 
					
						
							|  |  |  |     # set the logger for engine | 
					
						
							|  |  |  |     engine.logger = logger.getChild(engine_name) | 
					
						
							|  |  |  |     # the engine may have load some other engines | 
					
						
							|  |  |  |     # may sure the logger is initialized | 
					
						
							| 
									
										
										
										
											2022-06-18 07:38:36 +02:00
										 |  |  |     # use sys.modules.copy() to avoid "RuntimeError: dictionary changed size during iteration" | 
					
						
							|  |  |  |     # see https://github.com/python/cpython/issues/89516 | 
					
						
							|  |  |  |     # and https://docs.python.org/3.10/library/sys.html#sys.modules | 
					
						
							|  |  |  |     modules = sys.modules.copy() | 
					
						
							|  |  |  |     for module_name, module in modules.items(): | 
					
						
							| 
									
										
										
										
											2021-09-09 17:21:48 +02:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             module_name.startswith("searx.engines") | 
					
						
							|  |  |  |             and module_name != "searx.engines.__init__" | 
					
						
							|  |  |  |             and not hasattr(module, "logger") | 
					
						
							|  |  |  |         ): | 
					
						
							|  |  |  |             module_engine_name = module_name.split(".")[-1] | 
					
						
							|  |  |  |             module.logger = logger.getChild(module_engine_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | def update_engine_attributes(engine: Engine, engine_data): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     # set engine attributes from engine_data | 
					
						
							| 
									
										
										
										
											2020-11-16 12:44:07 +01:00
										 |  |  |     for param_name, param_value in engine_data.items(): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |         if param_name == 'categories': | 
					
						
							|  |  |  |             if isinstance(param_value, str): | 
					
						
							|  |  |  |                 param_value = list(map(str.strip, param_value.split(','))) | 
					
						
							|  |  |  |             engine.categories = param_value | 
					
						
							| 
									
										
										
										
											2021-12-22 09:13:23 +01:00
										 |  |  |         elif hasattr(engine, 'about') and param_name == 'about': | 
					
						
							|  |  |  |             engine.about = {**engine.about, **engine_data['about']} | 
					
						
							| 
									
										
										
										
											2021-10-06 18:02:29 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2020-11-16 12:44:07 +01:00
										 |  |  |             setattr(engine, param_name, param_value) | 
					
						
							| 
									
										
										
										
											2014-01-31 15:45:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     # set default attributes | 
					
						
							|  |  |  |     for arg_name, arg_value in ENGINE_DEFAULT_ARGS.items(): | 
					
						
							| 
									
										
										
										
											2016-02-19 15:13:01 +01:00
										 |  |  |         if not hasattr(engine, arg_name): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |             setattr(engine, arg_name, copy.deepcopy(arg_value)) | 
					
						
							| 
									
										
										
										
											2015-01-31 23:11:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-31 15:45:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | def set_language_attributes(engine: Engine): | 
					
						
							| 
									
										
										
										
											2016-12-15 07:34:43 +01:00
										 |  |  |     # assign supported languages from json file | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     if engine.name in ENGINES_LANGUAGES: | 
					
						
							|  |  |  |         engine.supported_languages = ENGINES_LANGUAGES[engine.name] | 
					
						
							| 
									
										
										
										
											2016-12-15 07:34:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 18:02:29 +02:00
										 |  |  |     elif engine.engine in ENGINES_LANGUAGES: | 
					
						
							|  |  |  |         # The key of the dictionary ENGINES_LANGUAGES is the *engine name* | 
					
						
							|  |  |  |         # configured in settings.xml.  When multiple engines are configured in | 
					
						
							|  |  |  |         # settings.yml to use the same origin engine (python module) these | 
					
						
							|  |  |  |         # additional engines can use the languages from the origin engine. | 
					
						
							|  |  |  |         # For this use the configured ``engine: ...`` from settings.yml | 
					
						
							|  |  |  |         engine.supported_languages = ENGINES_LANGUAGES[engine.engine] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if hasattr(engine, 'language'): | 
					
						
							|  |  |  |         # For an engine, when there is `language: ...` in the YAML settings, the | 
					
						
							|  |  |  |         # engine supports only one language, in this case | 
					
						
							|  |  |  |         # engine.supported_languages should contains this value defined in | 
					
						
							|  |  |  |         # settings.yml | 
					
						
							|  |  |  |         if engine.language not in engine.supported_languages: | 
					
						
							|  |  |  |             raise ValueError( | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 "settings.yml - engine: '%s' / language: '%s' not supported" % (engine.name, engine.language) | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-10-06 18:02:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if isinstance(engine.supported_languages, dict): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             engine.supported_languages = {engine.language: engine.supported_languages[engine.language]} | 
					
						
							| 
									
										
										
										
											2021-10-06 18:02:29 +02:00
										 |  |  |         else: | 
					
						
							|  |  |  |             engine.supported_languages = [engine.language] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  |     # find custom aliases for non standard language codes | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     for engine_lang in engine.supported_languages: | 
					
						
							|  |  |  |         iso_lang = match_language(engine_lang, BABEL_LANGS, fallback=None) | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         if ( | 
					
						
							|  |  |  |             iso_lang | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |             and iso_lang != engine_lang | 
					
						
							|  |  |  |             and not engine_lang.startswith(iso_lang) | 
					
						
							|  |  |  |             and iso_lang not in engine.supported_languages | 
					
						
							|  |  |  |         ): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |             engine.language_aliases[iso_lang] = engine_lang | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-24 09:58:57 +01:00
										 |  |  |     # language_support | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     engine.language_support = len(engine.supported_languages) > 0 | 
					
						
							| 
									
										
										
										
											2021-01-24 09:58:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 07:34:43 +01:00
										 |  |  |     # assign language fetching method if auxiliary method exists | 
					
						
							|  |  |  |     if hasattr(engine, '_fetch_supported_languages'): | 
					
						
							| 
									
										
										
										
											2021-02-26 07:20:50 +01:00
										 |  |  |         headers = { | 
					
						
							|  |  |  |             'User-Agent': gen_useragent(), | 
					
						
							| 
									
										
										
										
											2022-01-01 16:47:47 +01:00
										 |  |  |             'Accept-Language': "en-US,en;q=0.5",  # bing needs to set the English language | 
					
						
							| 
									
										
										
										
											2021-02-26 07:20:50 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |         engine.fetch_supported_languages = ( | 
					
						
							| 
									
										
										
										
											2021-10-06 18:02:29 +02:00
										 |  |  |             # pylint: disable=protected-access | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             lambda: engine._fetch_supported_languages(get(engine.supported_languages_url, headers=headers)) | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-05-19 07:38:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 19:51:40 +01:00
										 |  |  | def update_attributes_for_tor(engine: Engine) -> bool: | 
					
						
							|  |  |  |     if using_tor_proxy(engine) and hasattr(engine, 'onion_url'): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |         engine.search_url = engine.onion_url + getattr(engine, 'search_path', '') | 
					
						
							|  |  |  |         engine.timeout += settings['outgoing'].get('extra_proxy_timeout', 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def is_missing_required_attributes(engine): | 
					
						
							| 
									
										
										
										
											2021-06-01 13:34:41 +02:00
										 |  |  |     """An attribute is required when its name doesn't start with ``_`` (underline).
 | 
					
						
							|  |  |  |     Required attributes must not be ``None``. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     missing = False | 
					
						
							|  |  |  |     for engine_attr in dir(engine): | 
					
						
							|  |  |  |         if not engine_attr.startswith('_') and getattr(engine, engine_attr) is None: | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             logger.error('Missing engine config attribute: "{0}.{1}"'.format(engine.name, engine_attr)) | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |             missing = True | 
					
						
							|  |  |  |     return missing | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-22 19:51:40 +01:00
										 |  |  | def using_tor_proxy(engine: Engine): | 
					
						
							|  |  |  |     """Return True if the engine configuration declares to use Tor.""" | 
					
						
							|  |  |  |     return settings['outgoing'].get('using_tor_proxy') or getattr(engine, 'using_tor_proxy', False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | def is_engine_active(engine: Engine): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     # check if engine is inactive | 
					
						
							|  |  |  |     if engine.inactive is True: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # exclude onion engines if not using tor | 
					
						
							| 
									
										
										
										
											2022-01-22 19:51:40 +01:00
										 |  |  |     if 'onions' in engine.categories and not using_tor_proxy(engine): | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-19 15:13:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | def register_engine(engine: Engine): | 
					
						
							| 
									
										
										
										
											2021-06-01 16:24:31 +02:00
										 |  |  |     if engine.name in engines: | 
					
						
							|  |  |  |         logger.error('Engine config error: ambigious name: {0}'.format(engine.name)) | 
					
						
							|  |  |  |         sys.exit(1) | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     engines[engine.name] = engine | 
					
						
							| 
									
										
										
										
											2021-06-01 16:24:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-19 15:13:01 +01:00
										 |  |  |     if engine.shortcut in engine_shortcuts: | 
					
						
							|  |  |  |         logger.error('Engine config error: ambigious shortcut: {0}'.format(engine.shortcut)) | 
					
						
							|  |  |  |         sys.exit(1) | 
					
						
							|  |  |  |     engine_shortcuts[engine.shortcut] = engine.name | 
					
						
							| 
									
										
										
										
											2021-06-01 16:24:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     for category_name in engine.categories: | 
					
						
							|  |  |  |         categories.setdefault(category_name, []).append(engine) | 
					
						
							| 
									
										
										
										
											2014-01-31 15:45:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 22:59:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-21 14:27:25 +02:00
										 |  |  | def load_engines(engine_list): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     """usage: ``engine_list = settings['engines']``""" | 
					
						
							| 
									
										
										
										
											2017-07-21 14:27:25 +02:00
										 |  |  |     engines.clear() | 
					
						
							| 
									
										
										
										
											2016-05-19 07:38:43 +02:00
										 |  |  |     engine_shortcuts.clear() | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |     categories.clear() | 
					
						
							|  |  |  |     categories['general'] = [] | 
					
						
							| 
									
										
										
										
											2016-12-27 17:25:19 +01:00
										 |  |  |     for engine_data in engine_list: | 
					
						
							| 
									
										
										
										
											2020-08-31 18:59:27 +02:00
										 |  |  |         engine = load_engine(engine_data) | 
					
						
							| 
									
										
										
										
											2021-05-29 09:35:10 +02:00
										 |  |  |         if engine: | 
					
						
							|  |  |  |             register_engine(engine) | 
					
						
							| 
									
										
										
										
											2017-07-21 14:27:25 +02:00
										 |  |  |     return engines |