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

Public Member Functions

 __str__ (self)
 
babel.Locale locale (self)
 
GeoLocation by_query (cls, str search_term)
 

Public Attributes

 country_code
 

Protected Member Functions

dict _query_open_meteo (cls, str search_term)
 

Detailed Description

Minimal implementation of Geocoding.

Definition at line 123 of file weather.py.

Member Function Documentation

◆ __str__()

◆ _query_open_meteo()

dict searx.weather.GeoLocation._query_open_meteo ( cls,
str search_term )
protected

Definition at line 183 of file weather.py.

183 def _query_open_meteo(cls, search_term: str) -> dict:
184 url = f"https://geocoding-api.open-meteo.com/v1/search?name={quote_plus(search_term)}"
185 resp = network.get(url, timeout=3)
186 if resp.status_code != 200:
187 raise ValueError(f"unknown geo location: '{search_term}'")
188 results = resp.json().get("results")
189 if not results:
190 raise ValueError(f"unknown geo location: '{search_term}'")
191 location = results[0]
192 return {field.name: location[field.name] for field in dataclasses.fields(cls)}
193
194

Referenced by by_query().

+ Here is the caller graph for this function:

◆ by_query()

GeoLocation searx.weather.GeoLocation.by_query ( cls,
str search_term )
Factory method to get a GeoLocation object by a search term.  If no
location can be determined for the search term, a :py:obj:`ValueError`
is thrown.

Definition at line 166 of file weather.py.

166 def by_query(cls, search_term: str) -> GeoLocation:
167 """Factory method to get a GeoLocation object by a search term. If no
168 location can be determined for the search term, a :py:obj:`ValueError`
169 is thrown.
170 """
171
172 ctx = "weather_geolocation_by_query"
173 cache = get_WEATHER_DATA_CACHE()
174 geo_props = cache.get(search_term, ctx=ctx)
175
176 if not geo_props:
177 geo_props = cls._query_open_meteo(search_term=search_term)
178 cache.set(key=search_term, value=geo_props, expire=None, ctx=ctx)
179
180 return cls(**geo_props)
181

References _query_open_meteo(), and searx.weather.get_WEATHER_DATA_CACHE().

+ Here is the call graph for this function:

◆ locale()

babel.Locale searx.weather.GeoLocation.locale ( self)

Definition at line 143 of file weather.py.

143 def locale(self) -> babel.Locale:
144
145 # by region of the search language
146 sxng_tag = _get_sxng_locale_tag()
147 if "-" in sxng_tag:
148 locale = babel.Locale.parse(_get_sxng_locale_tag(), sep='-')
149 return locale
150
151 # by most popular language in the region (country code)
152 for lang in babel.languages.get_official_languages(self.country_code):
153 try:
154 locale = babel.Locale.parse(f"{lang}_{self.country_code}")
155 return locale
156 except babel.UnknownLocaleError:
157 continue
158
159 # No locale could be determined. This does not actually occur, but if
160 # it does, the English language is used by default. But not region US.
161 # US has some units that are only used in US but not in the rest of the
162 # world (e.g. °F instead of °C)
163 return babel.Locale("en", territory="DE")
164

References searx.weather._get_sxng_locale_tag().

+ Here is the call graph for this function:

Member Data Documentation

◆ country_code

searx.weather.GeoLocation.country_code

Definition at line 152 of file weather.py.


The documentation for this class was generated from the following file: