From 341d718c7f8557e03e184102f63b2f4a4364b939 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sat, 9 Aug 2025 12:24:19 +0200 Subject: [PATCH] [fix] duckduckgo weather: add type hints and fix WEATHERKIT_TO_CONDITION (#5101) --- searx/engines/duckduckgo_weather.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/searx/engines/duckduckgo_weather.py b/searx/engines/duckduckgo_weather.py index 40e39eecd..51743d8eb 100644 --- a/searx/engines/duckduckgo_weather.py +++ b/searx/engines/duckduckgo_weather.py @@ -3,8 +3,9 @@ DuckDuckGo Weather ~~~~~~~~~~~~~~~~~~ """ +from __future__ import annotations -from typing import TYPE_CHECKING +import typing as t from json import loads from urllib.parse import quote @@ -14,10 +15,11 @@ from searx.engines.duckduckgo import fetch_traits # pylint: disable=unused-impo from searx.engines.duckduckgo import get_ddg_lang from searx.enginelib.traits import EngineTraits -from searx.result_types import EngineResults, WeatherAnswer +from searx.result_types import EngineResults +from searx.extended_types import SXNG_Response from searx import weather -if TYPE_CHECKING: +if t.TYPE_CHECKING: import logging logger: logging.Logger @@ -41,13 +43,13 @@ categories = ["weather"] base_url = "https://duckduckgo.com/js/spice/forecast/{query}/{lang}" # adapted from https://gist.github.com/mikesprague/048a93b832e2862050356ca233ef4dc1 -WEATHERKIT_TO_CONDITION = { +WEATHERKIT_TO_CONDITION: dict[str, weather.WeatherConditionType] = { "BlowingDust": "fog", - "Clear": "clear", + "Clear": "clear sky", "Cloudy": "cloudy", "Foggy": "fog", "Haze": "fog", - "MostlyClear": "clear", + "MostlyClear": "clear sky", "MostlyCloudy": "partly cloudy", "PartlyCloudy": "partly cloudy", "Smoky": "fog", @@ -79,8 +81,9 @@ WEATHERKIT_TO_CONDITION = { } -def _weather_data(location, data): - return WeatherAnswer.Item( +def _weather_data(location: weather.GeoLocation, data: dict[str, t.Any]): + + return EngineResults.types.WeatherAnswer.Item( location=location, temperature=weather.Temperature(unit="°C", value=data['temperature']), condition=WEATHERKIT_TO_CONDITION[data["conditionCode"]], @@ -93,7 +96,7 @@ def _weather_data(location, data): ) -def request(query, params): +def request(query: str, params: dict[str, t.Any]): eng_region = traits.get_region(params['searxng_locale'], traits.all_locale) eng_lang = get_ddg_lang(traits, params['searxng_locale']) @@ -108,7 +111,7 @@ def request(query, params): return params -def response(resp): +def response(resp: SXNG_Response): res = EngineResults() if resp.text.strip() == "ddg_spice_forecast();": @@ -118,7 +121,7 @@ def response(resp): geoloc = weather.GeoLocation.by_query(resp.search_params["query"]) - weather_answer = WeatherAnswer( + weather_answer = EngineResults.types.WeatherAnswer( current=_weather_data(geoloc, json_data["currentWeather"]), service="duckduckgo weather", )