mirror of
https://github.com/searxng/searxng.git
synced 2025-12-29 23:20:02 +00:00
[mod] addition of various type hints / engine processors
Continuation of #5147 .. typification of the engine processors. BTW: - removed obsolete engine property https_support - fixed & improved currency_convert - engine instances can now implement a engine.setup method [#5147] https://github.com/searxng/searxng/pull/5147 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
committed by
Markus Heiser
parent
23257bddce
commit
8f8343dc0d
@@ -1,45 +1,64 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Processors for engine-type: ``online_url_search``
|
||||
|
||||
"""
|
||||
"""Processor used for ``online_url_search`` engines."""
|
||||
|
||||
import typing as t
|
||||
import re
|
||||
from .online import OnlineProcessor
|
||||
|
||||
re_search_urls = {
|
||||
'http': re.compile(r'https?:\/\/[^ ]*'),
|
||||
'ftp': re.compile(r'ftps?:\/\/[^ ]*'),
|
||||
'data:image': re.compile('data:image/[^; ]*;base64,[^ ]*'),
|
||||
from .online import OnlineProcessor, OnlineParams
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from .abstract import EngineProcessor
|
||||
from searx.search.models import SearchQuery
|
||||
|
||||
|
||||
search_syntax = {
|
||||
"http": re.compile(r"https?:\/\/[^ ]*"),
|
||||
"ftp": re.compile(r"ftps?:\/\/[^ ]*"),
|
||||
"data:image": re.compile("data:image/[^; ]*;base64,[^ ]*"),
|
||||
}
|
||||
"""Search syntax used for a URL search."""
|
||||
|
||||
|
||||
class UrlParams(t.TypedDict):
|
||||
"""URL request parameters."""
|
||||
|
||||
search_urls: dict[str, str | None]
|
||||
|
||||
|
||||
class OnlineUrlSearchParams(UrlParams, OnlineParams): # pylint: disable=duplicate-bases
|
||||
"""Request parameters of a ``online_url_search`` engine."""
|
||||
|
||||
|
||||
class OnlineUrlSearchProcessor(OnlineProcessor):
|
||||
"""Processor class used by ``online_url_search`` engines."""
|
||||
|
||||
engine_type = 'online_url_search'
|
||||
engine_type: str = "online_url_search"
|
||||
|
||||
def get_params(self, search_query, engine_category):
|
||||
"""Returns a set of :ref:`request params <engine request online>` or ``None`` if
|
||||
search query does not match to :py:obj:`re_search_urls`.
|
||||
"""
|
||||
def get_params(self, search_query: "SearchQuery", engine_category: str) -> OnlineUrlSearchParams | None:
|
||||
"""Returns a dictionary with the :ref:`request params <engine request
|
||||
online_currency>` (:py:obj:`OnlineUrlSearchParams`). ``None`` is
|
||||
returned if the search query does not match :py:obj:`search_syntax`."""
|
||||
|
||||
params = super().get_params(search_query, engine_category)
|
||||
if params is None:
|
||||
online_params: OnlineParams | None = super().get_params(search_query, engine_category)
|
||||
if online_params is None:
|
||||
return None
|
||||
|
||||
url_match = False
|
||||
search_urls = {}
|
||||
search_urls: dict[str, str | None] = {}
|
||||
has_match: bool = False
|
||||
|
||||
for k, v in re_search_urls.items():
|
||||
m = v.search(search_query.query)
|
||||
v = None
|
||||
for url_schema, url_re in search_syntax.items():
|
||||
search_urls[url_schema] = None
|
||||
m = url_re.search(search_query.query)
|
||||
if m:
|
||||
url_match = True
|
||||
v = m[0]
|
||||
search_urls[k] = v
|
||||
has_match = True
|
||||
search_urls[url_schema] = m[0]
|
||||
|
||||
if not url_match:
|
||||
if not has_match:
|
||||
return None
|
||||
|
||||
params['search_urls'] = search_urls
|
||||
params: OnlineUrlSearchParams = {
|
||||
**online_params,
|
||||
"search_urls": search_urls,
|
||||
}
|
||||
|
||||
return params
|
||||
|
||||
Reference in New Issue
Block a user