summaryrefslogtreecommitdiff
path: root/searx/search
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2021-09-06 08:49:13 +0200
committerAlexandre Flament <alex@al-f.net>2021-09-09 11:31:44 +0200
commit660c1801705e56a81cf02574f48cc194fec95b1f (patch)
tree5fd369a379dcd346198088fe54ec65fafd6915c1 /searx/search
parentfc20c561bf540d9b187a89a71730b6e3b0bee2da (diff)
[mod] plugin: call on_result after each engine from the ResultContainer
Currently, searx.search.Search calls on_result once the engine results have been merged (ResultContainer.order_results). on_result plugins can rewrite the results: once the URL(s) are modified, even they can be merged, it won't be the case since ResultContainer.order_results has already be called. This commit call on_result inside for each result of each engines. In addition the on_result function can return False to remove the result. Note: the on_result function now run on the engine thread instead of the Flask thread.
Diffstat (limited to 'searx/search')
-rw-r--r--searx/search/__init__.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/searx/search/__init__.py b/searx/search/__init__.py
index d8d3e1e1..6c750a3f 100644
--- a/searx/search/__init__.py
+++ b/searx/search/__init__.py
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
-# pylint: disable=missing-module-docstring
+# pylint: disable=missing-module-docstring, too-few-public-methods
import typing
import threading
@@ -179,7 +179,18 @@ class SearchWithPlugins(Search):
def __init__(self, search_query, ordered_plugin_list, request):
super().__init__(search_query)
self.ordered_plugin_list = ordered_plugin_list
- self.request = request
+ self.result_container.on_result = self._on_result
+ # pylint: disable=line-too-long
+ # get the "real" request to use it outside the Flask context.
+ # see
+ # * https://github.com/pallets/flask/blob/d01d26e5210e3ee4cbbdef12f05c886e08e92852/src/flask/globals.py#L55
+ # * https://github.com/pallets/werkzeug/blob/3c5d3c9bd0d9ce64590f0af8997a38f3823b368d/src/werkzeug/local.py#L548-L559
+ # * https://werkzeug.palletsprojects.com/en/2.0.x/local/#werkzeug.local.LocalProxy._get_current_object
+ # pylint: enable=line-too-long
+ self.request = request._get_current_object()
+
+ def _on_result(self, result):
+ return plugins.call(self.ordered_plugin_list, 'on_result', self.request, self, result)
def search(self):
if plugins.call(self.ordered_plugin_list, 'pre_search', self.request, self):
@@ -187,9 +198,6 @@ class SearchWithPlugins(Search):
plugins.call(self.ordered_plugin_list, 'post_search', self.request, self)
- results = self.result_container.get_ordered_results()
-
- for result in results:
- plugins.call(self.ordered_plugin_list, 'on_result', self.request, self, result)
+ self.result_container.close()
return self.result_container