online_dictionary.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Processores for engine-type: ``online_dictionary``
  4. """
  5. import re
  6. from searx.utils import is_valid_lang
  7. from .online import OnlineProcessor
  8. parser_re = re.compile('.*?([a-z]+)-([a-z]+) (.+)$', re.I)
  9. class OnlineDictionaryProcessor(OnlineProcessor):
  10. """Processor class used by ``online_dictionary`` engines."""
  11. engine_type = 'online_dictionary'
  12. def get_params(self, search_query, engine_category):
  13. """Returns a set of *request params* or ``None`` if search query does not match
  14. to :py:obj:`parser_re`."""
  15. params = super().get_params(search_query, engine_category)
  16. if params is None:
  17. return None
  18. m = parser_re.match(search_query.query)
  19. if not m:
  20. return None
  21. from_lang, to_lang, query = m.groups()
  22. from_lang = is_valid_lang(from_lang)
  23. to_lang = is_valid_lang(to_lang)
  24. if not from_lang or not to_lang:
  25. return None
  26. params['from_lang'] = from_lang
  27. params['to_lang'] = to_lang
  28. params['query'] = query
  29. return params
  30. def get_default_tests(self):
  31. tests = {}
  32. if getattr(self.engine, 'paging', False):
  33. tests['translation_paging'] = {
  34. 'matrix': {'query': 'en-es house', 'pageno': (1, 2, 3)},
  35. 'result_container': ['not_empty', ('one_title_contains', 'house')],
  36. 'test': ['unique_results'],
  37. }
  38. else:
  39. tests['translation'] = {
  40. 'matrix': {'query': 'en-es house'},
  41. 'result_container': ['not_empty', ('one_title_contains', 'house')],
  42. }
  43. return tests