.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.weather Namespace Reference

Classes

class  Compass
class  DateTime
class  GeoLocation
class  Pressure
class  RelativeHumidity
class  Temperature
class  WindSpeed

Functions

 get_WEATHER_DATA_CACHE ()
str _get_sxng_locale_tag ()
str|None symbol_url ("WeatherConditionType" condition)

Variables

list __all__
ExpireCache WEATHER_DATA_CACHE = None
str YR_WEATHER_SYMBOL_URL = "https://raw.githubusercontent.com/nrkno/yr-weather-symbols/refs/heads/master/symbols/outline"
 DateTimeFormats = typing.Literal["full", "long", "medium", "short"]
 DateTimeLocaleTypes = typing.Literal["UI"]
 WeatherConditionType
dict YR_WEATHER_SYMBOL_MAP
 condition
 _cache = get_WEATHER_DATA_CACHE()
str title = "cached weather condition symbols"

Detailed Description

Implementations used for weather conditions and forecast.

Function Documentation

◆ _get_sxng_locale_tag()

str searx.weather._get_sxng_locale_tag ( )
protected

Definition at line 58 of file weather.py.

58def _get_sxng_locale_tag() -> str:
59 # The function should return a locale (the sxng-tag: de-DE.en-US, ..) that
60 # can later be used to format and convert measured values for the output of
61 # weather data to the user.
62 #
63 # In principle, SearXNG only has two possible parameters for determining
64 # the locale: the UI language or the search- language/region. Since the
65 # conversion of weather data and time information is usually
66 # region-specific, the UI language is not suitable.
67 #
68 # It would probably be ideal to use the user's geolocation, but this will
69 # probably never be available in SearXNG (privacy critical).
70 #
71 # Therefore, as long as no "better" parameters are available, this function
72 # returns a locale based on the search region.
73
74 # pylint: disable=import-outside-toplevel,disable=cyclic-import
75 from searx import query
76 from searx.preferences import ClientPref
77
78 query = query.RawTextQuery(sxng_request.form.get("q", ""), [])
79 if query.languages and query.languages[0] not in ["all", "auto"]:
80 return query.languages[0]
81
82 search_lang = sxng_request.form.get("language")
83 if search_lang and search_lang not in ["all", "auto"]:
84 return search_lang
85
86 client_pref = ClientPref.from_http_request(sxng_request)
87 search_lang = client_pref.locale_tag
88 if search_lang and search_lang not in ["all", "auto"]:
89 return search_lang
90 return "en"
91
92

Referenced by searx.weather.Compass.l10n(), searx.weather.DateTime.l10n(), searx.weather.Pressure.l10n(), searx.weather.RelativeHumidity.l10n(), searx.weather.Temperature.l10n(), searx.weather.WindSpeed.l10n(), searx.weather.DateTime.l10n_date(), and searx.weather.GeoLocation.locale().

Here is the caller graph for this function:

◆ get_WEATHER_DATA_CACHE()

searx.weather.get_WEATHER_DATA_CACHE ( )

Definition at line 43 of file weather.py.

43def get_WEATHER_DATA_CACHE():
44
45 global WEATHER_DATA_CACHE # pylint: disable=global-statement
46
47 if WEATHER_DATA_CACHE is None:
48 WEATHER_DATA_CACHE = ExpireCache.build_cache(
49 ExpireCacheCfg(
50 name="WEATHER_DATA_CACHE",
51 MAX_VALUE_LEN=1024 * 200, # max. 200kB per icon (icons have most often 10-20kB)
52 MAXHOLD_TIME=60 * 60 * 24 * 7 * 4, # 4 weeks
53 )
54 )
55 return WEATHER_DATA_CACHE
56
57

Referenced by searx.weather.GeoLocation.by_query(), and symbol_url().

Here is the caller graph for this function:

◆ symbol_url()

str | None searx.weather.symbol_url ( "WeatherConditionType" condition)
Returns ``data:`` URL for the weather condition symbol or ``None`` if
the condition is not of type :py:obj:`WeatherConditionType`.

If symbol (SVG) is not already in the :py:obj:`WEATHER_DATA_CACHE` its
fetched from https://github.com/nrkno/yr-weather-symbols

Definition at line 93 of file weather.py.

93def symbol_url(condition: "WeatherConditionType") -> str | None:
94 """Returns ``data:`` URL for the weather condition symbol or ``None`` if
95 the condition is not of type :py:obj:`WeatherConditionType`.
96
97 If symbol (SVG) is not already in the :py:obj:`WEATHER_DATA_CACHE` its
98 fetched from https://github.com/nrkno/yr-weather-symbols
99 """
100 # Symbols for darkmode/lightmode? .. and day/night symbols? .. for the
101 # latter we need a geopoint (critical in sense of privacy)
102
103 fname = YR_WEATHER_SYMBOL_MAP.get(condition)
104 if fname is None:
105 return None
106
107 ctx = "weather_symbol_url"
108 cache = get_WEATHER_DATA_CACHE()
109 origin_url = f"{YR_WEATHER_SYMBOL_URL}/{fname}.svg"
110
111 data_url = cache.get(origin_url, ctx=ctx)
112 if data_url is not None:
113 return data_url
114
115 response = network.get(origin_url, timeout=3)
116 if response.status_code == 200:
117 mimetype = response.headers['Content-Type']
118 data_url = f"data:{mimetype};base64,{str(base64.b64encode(response.content), 'utf-8')}"
119 cache.set(key=origin_url, value=data_url, expire=None, ctx=ctx)
120 return data_url
121
122
123@dataclasses.dataclass

References get_WEATHER_DATA_CACHE().

Here is the call graph for this function:

Variable Documentation

◆ __all__

list searx.weather.__all__
private
Initial value:
1= [
2 "symbol_url",
3 "Temperature",
4 "Pressure",
5 "WindSpeed",
6 "RelativeHumidity",
7 "Compass",
8 "WeatherConditionType",
9 "DateTime",
10 "GeoLocation",
11]

Definition at line 5 of file weather.py.

◆ _cache

searx.weather._cache = get_WEATHER_DATA_CACHE()
protected

Definition at line 638 of file weather.py.

◆ condition

searx.weather.condition

Definition at line 636 of file weather.py.

◆ DateTimeFormats

searx.weather.DateTimeFormats = typing.Literal["full", "long", "medium", "short"]

Definition at line 200 of file weather.py.

◆ DateTimeLocaleTypes

searx.weather.DateTimeLocaleTypes = typing.Literal["UI"]

Definition at line 201 of file weather.py.

◆ title

str searx.weather.title = "cached weather condition symbols"

Definition at line 639 of file weather.py.

◆ WEATHER_DATA_CACHE

ExpireCache searx.weather.WEATHER_DATA_CACHE = None

Definition at line 37 of file weather.py.

◆ WeatherConditionType

searx.weather.WeatherConditionType

Definition at line 517 of file weather.py.

◆ YR_WEATHER_SYMBOL_MAP

dict searx.weather.YR_WEATHER_SYMBOL_MAP

Definition at line 575 of file weather.py.

◆ YR_WEATHER_SYMBOL_URL

str searx.weather.YR_WEATHER_SYMBOL_URL = "https://raw.githubusercontent.com/nrkno/yr-weather-symbols/refs/heads/master/symbols/outline"

Definition at line 40 of file weather.py.