diff options
| author | Markus Heiser <markus.heiser@darmarit.de> | 2025-01-27 16:43:43 +0100 |
|---|---|---|
| committer | Markus Heiser <markus.heiser@darmarIT.de> | 2025-01-28 07:07:08 +0100 |
| commit | 36a1ef12399d529f210ceb4f8b28f497fabd0834 (patch) | |
| tree | 4015a316c6505c3e52d43eea7137338331cbd4eb /searx/engines/brave.py | |
| parent | edfbf1e1183815cea3b723f3b66260bc55679f32 (diff) | |
[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>
Diffstat (limited to 'searx/engines/brave.py')
| -rw-r--r-- | searx/engines/brave.py | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/searx/engines/brave.py b/searx/engines/brave.py index 584d2d95c..828f6154e 100644 --- a/searx/engines/brave.py +++ b/searx/engines/brave.py @@ -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"]: |