[mod] weather results: add types, i18n/l10n, symbols & unit conversions

The types necessary for weather information such as GeoLocation, DateTime,
Temperature,Pressure, WindSpeed, RelativeHumidity, Compass (wind direction) and
symbols for the weather have been implemented.

There are unit conversions and translations for weather property labels.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser
2025-05-29 11:38:34 +02:00
committed by Markus Heiser
parent a800dd0473
commit ff206e9679
13 changed files with 980 additions and 181 deletions

View File

@@ -13,14 +13,14 @@
from __future__ import annotations
__all__ = ["Result", "MainResult", "KeyValue", "EngineResults", "AnswerSet", "Answer", "Translations", "Weather"]
__all__ = ["Result", "MainResult", "KeyValue", "EngineResults", "AnswerSet", "Answer", "Translations", "WeatherAnswer"]
import abc
from searx import enginelib
from ._base import Result, MainResult, LegacyResult
from .answer import AnswerSet, Answer, Translations, Weather
from .answer import AnswerSet, Answer, Translations, WeatherAnswer
from .keyvalue import KeyValue
@@ -35,7 +35,7 @@ class ResultList(list, abc.ABC):
MainResult = MainResult
Result = Result
Translations = Translations
Weather = Weather
WeatherAnswer = WeatherAnswer
# for backward compatibility
LegacyResult = LegacyResult

View File

@@ -18,7 +18,7 @@ template.
:members:
:show-inheritance:
.. autoclass:: Weather
.. autoclass:: WeatherAnswer
:members:
:show-inheritance:
@@ -30,10 +30,12 @@ template.
from __future__ import annotations
__all__ = ["AnswerSet", "Answer", "Translations", "Weather"]
__all__ = ["AnswerSet", "Answer", "Translations", "WeatherAnswer"]
from flask_babel import gettext
import msgspec
from searx import weather
from ._base import Result
@@ -149,49 +151,88 @@ class Translations(BaseAnswer, kw_only=True):
"""List of synonyms for the requested translation."""
class Weather(BaseAnswer, kw_only=True):
class WeatherAnswer(BaseAnswer, kw_only=True):
"""Answer type for weather data."""
template: str = "answer/weather.html"
"""The template is located at :origin:`answer/weather.html
<searx/templates/simple/answer/weather.html>`"""
location: str
"""The geo-location the weather data is from (e.g. `Berlin, Germany`)."""
current: Weather.DataItem
current: WeatherAnswer.Item
"""Current weather at ``location``."""
forecasts: list[Weather.DataItem] = []
forecasts: list[WeatherAnswer.Item] = []
"""Weather forecasts for ``location``."""
def __post_init__(self):
if not self.location:
raise ValueError("Weather answer is missing a location")
service: str = ""
"""Weather service from which this information was provided."""
class DataItem(msgspec.Struct, kw_only=True):
"""A container for weather data such as temperature, humidity, ..."""
class Item(msgspec.Struct, kw_only=True):
"""Weather parameters valid for a specific point in time."""
time: str | None = None
location: weather.GeoLocation
"""The geo-location the weather data is from (e.g. `Berlin, Germany`)."""
temperature: weather.Temperature
"""Air temperature at 2m above the ground."""
condition: weather.WeatherConditionType
"""Standardized designations that summarize the weather situation
(e.g. ``light sleet showers and thunder``)."""
# optional fields
datetime: weather.DateTime | None = None
"""Time of the forecast - not needed for the current weather."""
condition: str
"""Weather condition, e.g. `cloudy`, `rainy`, `sunny` ..."""
summary: str | None = None
"""One-liner about the weather forecast / current weather conditions.
If unset, a summary is build up from temperature and current weather
conditions.
"""
temperature: str
"""Temperature string, e.g. `17°C`"""
feels_like: weather.Temperature | None = None
"""Apparent temperature, the temperature equivalent perceived by
humans, caused by the combined effects of air temperature, relative
humidity and wind speed. The measure is most commonly applied to the
perceived outdoor temperature.
"""
feelsLike: str | None = None
"""Felt temperature string, should be formatted like ``temperature``"""
pressure: weather.Pressure | None = None
"""Air pressure at sea level (e.g. 1030 hPa) """
humidity: str | None = None
"""Humidity percentage string, e.g. `60%`"""
humidity: weather.RelativeHumidity | None = None
"""Amount of relative humidity in the air at 2m above the ground. The
unit is ``%``, e.g. 60%)
"""
pressure: str | None = None
"""Pressure string, e.g. `1030hPa`"""
wind_from: weather.Compass
"""The directon which moves towards / direction the wind is coming from."""
wind: str | None = None
"""Information about the wind, e.g. `W, 231°, 10 m/s`"""
wind_speed: weather.WindSpeed | None = None
"""Speed of wind / wind speed at 10m above the ground (10 min average)."""
attributes: dict[str] = []
"""Key-Value dict of additional weather attributes that are not available above"""
cloud_cover: int | None = None
"""Amount of sky covered by clouds / total cloud cover for all heights
(cloudiness, unit: %)"""
# attributes: dict[str, str | int] = {}
# """Key-Value dict of additional typeless weather attributes."""
def __post_init__(self):
if not self.summary:
self.summary = gettext("{location}: {temperature}, {condition}").format(
location=self.location,
temperature=self.temperature,
condition=gettext(self.condition.capitalize()),
)
@property
def url(self) -> str | None:
"""Determines a `data URL`_ with a symbol for the weather
conditions. If no symbol can be assigned, ``None`` is returned.
.. _data URL:
https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
"""
return weather.symbol_url(self.condition)