demo_offline.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Within this module we implement a *demo offline engine*. Do not look to
  3. close to the implementation, its just a simple example. To get in use of this
  4. *demo* engine add the following entry to your engines list in ``settings.yml``:
  5. .. code:: yaml
  6. - name: my offline engine
  7. engine: demo_offline
  8. shortcut: demo
  9. disabled: false
  10. """
  11. import json
  12. from searx.result_types import EngineResults
  13. engine_type = 'offline'
  14. categories = ['general']
  15. disabled = True
  16. timeout = 2.0
  17. about = {
  18. "wikidata_id": None,
  19. "official_api_documentation": None,
  20. "use_official_api": False,
  21. "require_api_key": False,
  22. "results": 'JSON',
  23. }
  24. # if there is a need for globals, use a leading underline
  25. _my_offline_engine = None
  26. def init(engine_settings=None):
  27. """Initialization of the (offline) engine. The origin of this demo engine is a
  28. simple json string which is loaded in this example while the engine is
  29. initialized.
  30. """
  31. global _my_offline_engine # pylint: disable=global-statement
  32. _my_offline_engine = (
  33. '[ {"value": "%s"}'
  34. ', {"value":"first item"}'
  35. ', {"value":"second item"}'
  36. ', {"value":"third item"}'
  37. ']' % engine_settings.get('name')
  38. )
  39. def search(query, request_params) -> EngineResults:
  40. """Query (offline) engine and return results. Assemble the list of results from
  41. your local engine. In this demo engine we ignore the 'query' term, usual
  42. you would pass the 'query' term to your local engine to filter out the
  43. results.
  44. """
  45. res = EngineResults()
  46. result_list = json.loads(_my_offline_engine)
  47. for row in result_list:
  48. entry = {
  49. 'query': query,
  50. 'language': request_params['searxng_locale'],
  51. 'value': row.get("value"),
  52. # choose a result template or comment out to use the *default*
  53. 'template': 'key-value.html',
  54. }
  55. res.append(entry)
  56. return res