__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """.. sidebar:: Further reading ..
  3. - :ref:`plugins admin`
  4. - :ref:`SearXNG settings <settings plugins>`
  5. Plugins can extend or replace functionality of various components of SearXNG.
  6. Entry points (hooks) define when a plugin runs. Right now only three hooks are
  7. implemented. So feel free to implement a hook if it fits the behaviour of your
  8. plugin / a plugin doesn't need to implement all the hooks.
  9. - pre search: :py:obj:`Plugin.pre_search`
  10. - post search: :py:obj:`Plugin.post_search`
  11. - on each result item: :py:obj:`Plugin.on_result`
  12. Below you will find some examples, for more coding examples have a look at the
  13. built-in plugins :origin:`searx/plugins/` or `Only show green hosted results`_.
  14. .. _Only show green hosted results:
  15. https://github.com/return42/tgwf-searx-plugins/
  16. Add Answer example
  17. ==================
  18. Here is an example of a very simple plugin that adds a "Hello World" into the
  19. answer area:
  20. .. code:: python
  21. from flask_babel import gettext as _
  22. from searx.plugins import Plugin
  23. from searx.result_types import Answer
  24. class MyPlugin(Plugin):
  25. id = "hello world"
  26. def __init__(self, plg_cfg):
  27. super().__init__(plg_cfg)
  28. self.info = PluginInfo(id=self.id, name=_("Hello"), description=_("demo plugin"))
  29. def post_search(self, request, search):
  30. return [ Answer(answer="Hello World") ]
  31. .. _filter urls example:
  32. Filter URLs example
  33. ===================
  34. .. sidebar:: Further reading ..
  35. - :py:obj:`Result.filter_urls(..) <searx.result_types._base.Result.filter_urls>`
  36. The :py:obj:`Result.filter_urls(..) <searx.result_types._base.Result.filter_urls>`
  37. can be used to filter and/or modify URL fields. In the following example, the
  38. filter function ``my_url_filter``:
  39. .. code:: python
  40. def my_url_filter(result, field_name, url_src) -> bool | str:
  41. if "google" in url_src:
  42. return False # remove URL field from result
  43. if "facebook" in url_src:
  44. new_url = url_src.replace("facebook", "fb-dummy")
  45. return new_url # return modified URL
  46. return True # leave URL in field unchanged
  47. is applied to all URL fields in the :py:obj:`Plugin.on_result` hook:
  48. .. code:: python
  49. class MyUrlFilter(Plugin):
  50. ...
  51. def on_result(self, request, search, result) -> bool:
  52. result.filter_urls(my_url_filter)
  53. return True
  54. Implementation
  55. ==============
  56. .. autoclass:: Plugin
  57. :members:
  58. .. autoclass:: PluginInfo
  59. :members:
  60. .. autoclass:: PluginStorage
  61. :members:
  62. .. autoclass:: PluginCfg
  63. :members:
  64. """
  65. from __future__ import annotations
  66. __all__ = ["PluginInfo", "Plugin", "PluginStorage", "PluginCfg"]
  67. import searx
  68. from ._core import PluginInfo, Plugin, PluginStorage, PluginCfg
  69. STORAGE: PluginStorage = PluginStorage()
  70. def initialize(app):
  71. STORAGE.load_settings(searx.get_setting("plugins"))
  72. STORAGE.init(app)