.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"]
 
 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 57 of file weather.py.

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

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(), 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 42 of file weather.py.

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

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 92 of file weather.py.

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

◆ _cache

searx.weather._cache = get_WEATHER_DATA_CACHE()
protected

Definition at line 596 of file weather.py.

◆ condition

searx.weather.condition

Definition at line 594 of file weather.py.

◆ DateTimeFormats

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

Definition at line 195 of file weather.py.

◆ title

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

Definition at line 597 of file weather.py.

◆ WEATHER_DATA_CACHE

ExpireCache searx.weather.WEATHER_DATA_CACHE = None

Definition at line 36 of file weather.py.

◆ WeatherConditionType

searx.weather.WeatherConditionType

Definition at line 481 of file weather.py.

◆ YR_WEATHER_SYMBOL_MAP

dict searx.weather.YR_WEATHER_SYMBOL_MAP

Definition at line 536 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 39 of file weather.py.