[mod] swisscows engines: add language / region support

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser
2026-06-05 13:51:21 +02:00
committed by Bnyro
parent ed369ac0ec
commit e7cf57e9ae
2 changed files with 72 additions and 15 deletions
+18 -6
View File
@@ -7,7 +7,9 @@ from urllib.parse import urlencode
import typing as t
from searx.utils import html_to_text
from searx.result_types import EngineResults
from searx.engines.swisscows import appropriate_locale
if t.TYPE_CHECKING:
from searx.extended_types import SXNG_Response
@@ -33,8 +35,17 @@ paging = True
base_url = "https://api.swisscows.com"
time_range_map = {"day": "Day", "week": "Week", "month": "Month", "year": "Year"}
swisscows_regions: list[str] = ["DE"]
"""Regions supported by swisscows News."""
def request(query: str, params: "OnlineParams") -> None:
sxng_locale = params["searxng_locale"].split("-", maxsplit=1)[0]
locale: str = appropriate_locale(sxng_locale, swisscows_regions, default="de-DE")
if not locale:
return
freshness = "All"
if params["time_range"]:
freshness = time_range_map[params["time_range"]]
@@ -42,8 +53,8 @@ def request(query: str, params: "OnlineParams") -> None:
args = {
"query": query,
"itemsCount": results_per_page,
"region": "de-DE",
"language": "de",
"region": locale,
"language": locale.split("-", maxsplit=1)[0],
"offset": (params["pageno"] - 1) * results_per_page,
"freshness": freshness,
"sortOrder": "Desc",
@@ -54,17 +65,18 @@ def request(query: str, params: "OnlineParams") -> None:
params["url"] = base_url + url_path
def response(resp: "SXNG_Response"):
def response(resp: "SXNG_Response") -> EngineResults:
res = EngineResults()
for result in resp.json()["items"]:
result: dict[str, str]
for result in resp.json()["items"]: # pyright: ignore[reportAny]
res.add(
res.types.MainResult(
url=result["uri"],
title=result["title"],
title=html_to_text(result["title"]),
content=result["description"],
publishedDate=datetime.fromisoformat(result["created"]),
thumbnail=result.get("og:image"),
thumbnail=result.get("og:image") or "",
)
)