From f49b2c94a9a9938133dbf94d686f00776ce96cdc Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Wed, 5 Mar 2025 17:50:22 +0100 Subject: [mod] migrate all key-value.html templates to KeyValue type The engines now all use KeyValue results and return the results in a EngineResults object. The sqlite engine can return MainResult results in addition to KeyValue results (based on engine's config in settings.yml), Signed-off-by: Markus Heiser --- searx/engines/sqlite.py | 64 ++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 25 deletions(-) (limited to 'searx/engines/sqlite.py') diff --git a/searx/engines/sqlite.py b/searx/engines/sqlite.py index e9694cf74..e3dd55829 100644 --- a/searx/engines/sqlite.py +++ b/searx/engines/sqlite.py @@ -2,6 +2,14 @@ """SQLite is a small, fast and reliable SQL database engine. It does not require any extra dependency. +Configuration +============= + +The engine has the following (additional) settings: + +- :py:obj:`result_type` + + Example ======= @@ -18,29 +26,32 @@ Query to test: ``!mediathekview concert`` .. code:: yaml - - name: mediathekview - engine: sqlite - disabled: False - categories: general - result_template: default.html - database: searx/data/filmliste-v2.db - query_str: >- - SELECT title || ' (' || time(duration, 'unixepoch') || ')' AS title, - COALESCE( NULLIF(url_video_hd,''), NULLIF(url_video_sd,''), url_video) AS url, - description AS content - FROM film - WHERE title LIKE :wildcard OR description LIKE :wildcard - ORDER BY duration DESC + - name: mediathekview + engine: sqlite + shortcut: mediathekview + categories: [general, videos] + result_type: MainResult + database: searx/data/filmliste-v2.db + query_str: >- + SELECT title || ' (' || time(duration, 'unixepoch') || ')' AS title, + COALESCE( NULLIF(url_video_hd,''), NULLIF(url_video_sd,''), url_video) AS url, + description AS content + FROM film + WHERE title LIKE :wildcard OR description LIKE :wildcard + ORDER BY duration DESC Implementations =============== """ - +import typing import sqlite3 import contextlib -engine_type = 'offline' +from searx.result_types import EngineResults +from searx.result_types import MainResult, KeyValue + +engine_type = "offline" database = "" """Filename of the SQLite DB.""" @@ -48,9 +59,11 @@ database = "" query_str = "" """SQL query that returns the result items.""" +result_type: typing.Literal["MainResult", "KeyValue"] = "KeyValue" +"""The result type can be :py:obj:`MainResult` or :py:obj:`KeyValue`.""" + limit = 10 paging = True -result_template = 'key-value.html' def init(engine_settings): @@ -80,9 +93,8 @@ def sqlite_cursor(): yield cursor -def search(query, params): - results = [] - +def search(query, params) -> EngineResults: + res = EngineResults() query_params = { 'query': query, 'wildcard': r'%' + query.replace(' ', r'%') + r'%', @@ -97,9 +109,11 @@ def search(query, params): col_names = [cn[0] for cn in cur.description] for row in cur.fetchall(): - item = dict(zip(col_names, map(str, row))) - item['template'] = result_template - logger.debug("append result --> %s", item) - results.append(item) - - return results + kvmap = dict(zip(col_names, map(str, row))) + if result_type == "MainResult": + item = MainResult(**kvmap) # type: ignore + else: + item = KeyValue(kvmap=kvmap) + res.add(item) + + return res -- cgit v1.2.3