diff options
| author | Markus Heiser <markus.heiser@darmarIT.de> | 2025-08-09 12:24:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-09 12:24:19 +0200 |
| commit | 341d718c7f8557e03e184102f63b2f4a4364b939 (patch) | |
| tree | 1fec4e3fcfa51d17545e37b432e9e7fe052985e9 /searx/engines/duckduckgo_weather.py | |
| parent | 6172beba1a324a9905fb3367caec2638a0fdc730 (diff) | |
[fix] duckduckgo weather: add type hints and fix WEATHERKIT_TO_CONDITION (#5101)
Diffstat (limited to 'searx/engines/duckduckgo_weather.py')
| -rw-r--r-- | searx/engines/duckduckgo_weather.py | 25 |
1 files 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", ) |