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