2020-12-16 12:41:32 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-27 13:13:39 +00:00
|
|
|
# lint: pylint
|
2023-09-15 07:53:03 +00:00
|
|
|
"""Processors for engine-type: ``online_dictionary``
|
2021-04-27 13:13:39 +00:00
|
|
|
|
|
|
|
"""
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from searx.utils import is_valid_lang
|
|
|
|
from .online import OnlineProcessor
|
|
|
|
|
2022-07-04 17:20:30 +00:00
|
|
|
parser_re = re.compile('.*?([a-z]+)-([a-z]+) (.+)$', re.I)
|
2020-12-16 12:41:32 +00:00
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2020-12-16 12:41:32 +00:00
|
|
|
class OnlineDictionaryProcessor(OnlineProcessor):
|
2021-06-03 16:02:47 +00:00
|
|
|
"""Processor class used by ``online_dictionary`` engines."""
|
2020-12-16 12:41:32 +00:00
|
|
|
|
2021-06-03 16:02:47 +00:00
|
|
|
engine_type = 'online_dictionary'
|
2020-12-16 12:41:32 +00:00
|
|
|
|
|
|
|
def get_params(self, search_query, engine_category):
|
2022-09-29 18:54:46 +00:00
|
|
|
"""Returns a set of :ref:`request params <engine request online_dictionary>` or
|
|
|
|
``None`` if search query does not match to :py:obj:`parser_re`.
|
|
|
|
"""
|
2020-12-16 12:41:32 +00:00
|
|
|
params = super().get_params(search_query, engine_category)
|
|
|
|
if params is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
m = parser_re.match(search_query.query)
|
|
|
|
if not m:
|
|
|
|
return None
|
|
|
|
|
|
|
|
from_lang, to_lang, query = m.groups()
|
|
|
|
|
|
|
|
from_lang = is_valid_lang(from_lang)
|
|
|
|
to_lang = is_valid_lang(to_lang)
|
|
|
|
|
|
|
|
if not from_lang or not to_lang:
|
|
|
|
return None
|
|
|
|
|
|
|
|
params['from_lang'] = from_lang
|
|
|
|
params['to_lang'] = to_lang
|
|
|
|
params['query'] = query
|
|
|
|
|
|
|
|
return params
|
2020-12-24 08:28:16 +00:00
|
|
|
|
|
|
|
def get_default_tests(self):
|
|
|
|
tests = {}
|
|
|
|
|
|
|
|
if getattr(self.engine, 'paging', False):
|
|
|
|
tests['translation_paging'] = {
|
2021-12-27 08:26:22 +00:00
|
|
|
'matrix': {'query': 'en-es house', 'pageno': (1, 2, 3)},
|
2020-12-24 08:28:16 +00:00
|
|
|
'result_container': ['not_empty', ('one_title_contains', 'house')],
|
2021-12-27 08:26:22 +00:00
|
|
|
'test': ['unique_results'],
|
2020-12-24 08:28:16 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
tests['translation'] = {
|
|
|
|
'matrix': {'query': 'en-es house'},
|
|
|
|
'result_container': ['not_empty', ('one_title_contains', 'house')],
|
|
|
|
}
|
|
|
|
|
|
|
|
return tests
|