online_dictionary.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 :ref:`request params <engine request online_dictionary>` or
  14. ``None`` if search query does not match to :py:obj:`parser_re`.
  15. """
  16. params = super().get_params(search_query, engine_category)
  17. if params is None:
  18. return None
  19. m = parser_re.match(search_query.query)
  20. if not m:
  21. return None
  22. from_lang, to_lang, query = m.groups()
  23. from_lang = is_valid_lang(from_lang)
  24. to_lang = is_valid_lang(to_lang)
  25. if not from_lang or not to_lang:
  26. return None
  27. params['from_lang'] = from_lang
  28. params['to_lang'] = to_lang
  29. params['query'] = query
  30. return params
  31. def get_default_tests(self):
  32. tests = {}
  33. if getattr(self.engine, 'paging', False):
  34. tests['translation_paging'] = {
  35. 'matrix': {'query': 'en-es house', 'pageno': (1, 2, 3)},
  36. 'result_container': ['not_empty', ('one_title_contains', 'house')],
  37. 'test': ['unique_results'],
  38. }
  39. else:
  40. tests['translation'] = {
  41. 'matrix': {'query': 'en-es house'},
  42. 'result_container': ['not_empty', ('one_title_contains', 'house')],
  43. }
  44. return tests