abstract.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. from abc import abstractmethod, ABC
  3. from searx import logger
  4. logger = logger.getChild('searx.search.processor')
  5. class EngineProcessor(ABC):
  6. def __init__(self, engine, engine_name):
  7. self.engine = engine
  8. self.engine_name = engine_name
  9. def get_params(self, search_query, engine_category):
  10. # if paging is not supported, skip
  11. if search_query.pageno > 1 and not self.engine.paging:
  12. return None
  13. # if time_range is not supported, skip
  14. if search_query.time_range and not self.engine.time_range_support:
  15. return None
  16. params = {}
  17. params['category'] = engine_category
  18. params['pageno'] = search_query.pageno
  19. params['safesearch'] = search_query.safesearch
  20. params['time_range'] = search_query.time_range
  21. params['engine_data'] = search_query.engine_data.get(self.engine_name, {})
  22. if hasattr(self.engine, 'language') and self.engine.language:
  23. params['language'] = self.engine.language
  24. else:
  25. params['language'] = search_query.lang
  26. return params
  27. @abstractmethod
  28. def search(self, query, params, result_container, start_time, timeout_limit):
  29. pass
  30. def get_tests(self):
  31. tests = getattr(self.engine, 'tests', None)
  32. if tests is None:
  33. tests = getattr(self.engine, 'additional_tests', {})
  34. tests.update(self.get_default_tests())
  35. return tests
  36. else:
  37. return tests
  38. def get_default_tests(self):
  39. return {}