online_dictionary.py 1.5 KB

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