[mod] engine: bing images - use internal API & type hints (#6699)

Switching to internal APIs such as ``EngineResults`` and
adding more type hints.  No functional changes.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser
2026-09-12 09:25:56 +02:00
committed by Markus Heiser
parent 923a307454
commit 56b1f64541

View File

@@ -1,14 +1,20 @@
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
"""Bing-Images: description see :py:obj:`searx.engines.bing`.""" """Bing-Images: description see :py:obj:`searx.engines.bing`."""
import typing as t
import json import json
from urllib.parse import urlencode from urllib.parse import urlencode
from lxml import html from lxml import html
from searx.engines.bing import fetch_traits # pylint: disable=unused-import from searx.engines.bing import fetch_traits # pylint: disable=unused-import
from searx.result_types import EngineResults
if t.TYPE_CHECKING:
from searx.extended_types import SXNG_Response
from searx.search.processors import OnlineParams
# about
about = { about = {
"website": "https://www.bing.com/images", "website": "https://www.bing.com/images",
"wikidata_id": "Q182496", "wikidata_id": "Q182496",
@@ -18,7 +24,6 @@ about = {
"results": "HTML", "results": "HTML",
} }
# engine dependent config
categories = ["images", "web"] categories = ["images", "web"]
paging = True paging = True
enable_http3 = True enable_http3 = True
@@ -35,13 +40,13 @@ base_url = "https://www.bing.com"
"""Bing-Image search URL""" """Bing-Image search URL"""
def request(query, params): def request(query: str, params: "OnlineParams"):
"""Assemble a Bing-Image request.""" """Assemble a Bing-Image request."""
engine_region = traits.get_region(params["searxng_locale"], traits.all_locale) engine_region = traits.get_region(params["searxng_locale"], traits.all_locale)
# build URL query # build URL query / example:
# - example: https://www.bing.com/images/async?q=foo&mmasync=1&first=1&count=35 # https://www.bing.com/images/async?q=foo&mmasync=1&first=1&count=35
query_params = { query_params = {
"q": query, "q": query,
@@ -65,10 +70,10 @@ def request(query, params):
params["url"] = base_url + "/images/async?" + urlencode(query_params) params["url"] = base_url + "/images/async?" + urlencode(query_params)
def response(resp): def response(resp: "SXNG_Response") -> EngineResults:
"""Get response from Bing-Image""" """Get response from Bing-Image"""
results = [] res = EngineResults()
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
@@ -84,17 +89,17 @@ def response(resp):
img_format = " ".join(result.xpath('.//div[@class="imgpt"]/div/span/text()')).strip().split(" · ") img_format = " ".join(result.xpath('.//div[@class="imgpt"]/div/span/text()')).strip().split(" · ")
source = " ".join(result.xpath('.//div[@class="imgpt"]//div[@class="lnkw"]//a/text()')).strip() source = " ".join(result.xpath('.//div[@class="imgpt"]//div[@class="lnkw"]//a/text()')).strip()
results.append(
{ res.add(
"template": "images.html", res.types.Image(
"url": metadata["purl"], title=title,
"thumbnail_src": metadata["turl"], url=metadata["purl"],
"img_src": metadata["murl"], thumbnail_src=metadata["turl"],
"content": metadata.get("desc"), img_src=metadata["murl"],
"title": title, content=metadata.get("desc"),
"source": source, source=source,
"resolution": img_format[0], resolution=img_format[0],
"img_format": img_format[1] if len(img_format) >= 2 else None, img_format=img_format[1] if len(img_format) >= 2 else "",
}
) )
return results )
return res