online_dictionary.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Processors for engine-type: ``online_dictionary``
  3. """
  4. import re
  5. from searx.utils import is_valid_lang
  6. from .online import OnlineProcessor
  7. parser_re = re.compile('.*?([a-z]+)-([a-z]+) (.+)$', re.I)
  8. class OnlineDictionaryProcessor(OnlineProcessor):
  9. """Processor class used by ``online_dictionary`` engines."""
  10. engine_type = 'online_dictionary'
  11. def get_params(self, search_query, engine_category):
  12. """Returns a set of :ref:`request params <engine request online_dictionary>` or
  13. ``None`` if search query does not match to :py:obj:`parser_re`.
  14. """
  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