[refactor] typification of SearXNG / EngineResults

In [1] and [2] we discussed the need of a Result.results property and how we can
avoid unclear code.  This patch implements a class for the reslut-lists of
engines::

    searx.result_types.EngineResults

A simple example for the usage in engine development::

    from searx.result_types import EngineResults
    ...
    def response(resp) -> EngineResults:
        res = EngineResults()
        ...
        res.add( res.types.Answer(answer="lorem ipsum ..", url="https://example.org") )
        ...
        return res

[1] https://github.com/searxng/searxng/pull/4183#pullrequestreview-257400034
[2] https://github.com/searxng/searxng/pull/4183#issuecomment-2614301580
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2025-01-27 16:43:43 +01:00 committed by Markus Heiser
parent edfbf1e118
commit 36a1ef1239
26 changed files with 195 additions and 140 deletions

View file

@ -139,7 +139,7 @@ from searx.utils import (
get_embeded_stream_url,
)
from searx.enginelib.traits import EngineTraits
from searx.result_types import Answer
from searx.result_types import EngineResults
if TYPE_CHECKING:
import logging
@ -249,7 +249,7 @@ def _extract_published_date(published_date_raw):
return None
def response(resp):
def response(resp) -> EngineResults:
if brave_category in ('search', 'goggles'):
return _parse_search(resp)
@ -270,9 +270,9 @@ def response(resp):
raise ValueError(f"Unsupported brave category: {brave_category}")
def _parse_search(resp):
def _parse_search(resp) -> EngineResults:
result_list = EngineResults()
result_list = []
dom = html.fromstring(resp.text)
# I doubt that Brave is still providing the "answer" class / I haven't seen
@ -282,7 +282,7 @@ def _parse_search(resp):
url = eval_xpath_getindex(dom, '//div[@id="featured_snippet"]/a[@class="result-header"]/@href', 0, default=None)
answer = extract_text(answer_tag)
if answer is not None:
Answer(results=result_list, answer=answer, url=url)
result_list.add(result_list.types.Answer(answer=answer, url=url))
# xpath_results = '//div[contains(@class, "snippet fdb") and @data-type="web"]'
xpath_results = '//div[contains(@class, "snippet ")]'
@ -339,8 +339,8 @@ def _parse_search(resp):
return result_list
def _parse_news(json_resp):
result_list = []
def _parse_news(json_resp) -> EngineResults:
result_list = EngineResults()
for result in json_resp["results"]:
item = {
@ -356,8 +356,8 @@ def _parse_news(json_resp):
return result_list
def _parse_images(json_resp):
result_list = []
def _parse_images(json_resp) -> EngineResults:
result_list = EngineResults()
for result in json_resp["results"]:
item = {
@ -375,8 +375,8 @@ def _parse_images(json_resp):
return result_list
def _parse_videos(json_resp):
result_list = []
def _parse_videos(json_resp) -> EngineResults:
result_list = EngineResults()
for result in json_resp["results"]: