diff options
Diffstat (limited to 'searx/result_types')
| -rw-r--r-- | searx/result_types/__init__.py | 6 | ||||
| -rw-r--r-- | searx/result_types/answer.py | 97 |
2 files changed, 72 insertions, 31 deletions
diff --git a/searx/result_types/__init__.py b/searx/result_types/__init__.py index 8a82cf8d4..6d47d3a4f 100644 --- a/searx/result_types/__init__.py +++ b/searx/result_types/__init__.py @@ -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 diff --git a/searx/result_types/answer.py b/searx/result_types/answer.py index d5793fac3..7ea0787a1 100644 --- a/searx/result_types/answer.py +++ b/searx/result_types/answer.py @@ -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 Item(msgspec.Struct, kw_only=True): + """Weather parameters valid for a specific point in time.""" + + 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``).""" - class DataItem(msgspec.Struct, kw_only=True): - """A container for weather data such as temperature, humidity, ...""" + # optional fields - time: str | None = None + 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. + """ + + 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. + """ + + pressure: weather.Pressure | None = None + """Air pressure at sea level (e.g. 1030 hPa) """ + + humidity: weather.RelativeHumidity | None = None + """Amount of relative humidity in the air at 2m above the ground. The + unit is ``%``, e.g. 60%) + """ + + wind_from: weather.Compass + """The directon which moves towards / direction the wind is coming from.""" - temperature: str - """Temperature string, e.g. `17°C`""" + wind_speed: weather.WindSpeed | None = None + """Speed of wind / wind speed at 10m above the ground (10 min average).""" - feelsLike: str | None = None - """Felt temperature string, should be formatted like ``temperature``""" + cloud_cover: int | None = None + """Amount of sky covered by clouds / total cloud cover for all heights + (cloudiness, unit: %)""" - humidity: str | None = None - """Humidity percentage string, e.g. `60%`""" + # attributes: dict[str, str | int] = {} + # """Key-Value dict of additional typeless weather attributes.""" - pressure: str | None = None - """Pressure string, e.g. `1030hPa`""" + 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()), + ) - wind: str | None = None - """Information about the wind, e.g. `W, 231°, 10 m/s`""" + @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. - attributes: dict[str] = [] - """Key-Value dict of additional weather attributes that are not available above""" + .. _data URL: + https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data + """ + return weather.symbol_url(self.condition) |