diff --git a/searx/engines/wttr.py b/searx/engines/wttr.py new file mode 100644 index 000000000..d3da2748a --- /dev/null +++ b/searx/engines/wttr.py @@ -0,0 +1,136 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""wttr.in (weather forecast service)""" + +from json import loads +from urllib.parse import quote +from flask_babel import gettext + +about = { + "website": "https://wttr.in", + "wikidata_id": "Q107586666", + "official_api_documentation": "https://github.com/chubin/wttr.in#json-output", + "use_official_api": True, + "require_api_key": False, + "results": "JSON", +} + +categories = ["others"] + +url = "https://wttr.in/{query}?format=j1&lang={lang}" + + +def get_weather_condition_key(lang): + if lang == "en": + return "weatherDesc" + + return "lang_" + lang.lower() + + +def generate_day_table(day): + res = "" + + res += f"{gettext('Average temp.')}{day['avgtempC']}°C / {day['avgtempF']}°F" + res += f"{gettext('Min temp.')}{day['mintempC']}°C / {day['mintempF']}°F" + res += f"{gettext('Max temp.')}{day['maxtempC']}°C / {day['maxtempF']}°F" + res += f"{gettext('UV index')}{day['uvIndex']}" + res += f"{gettext('Sunrise')}{day['astronomy'][0]['sunrise']}" + res += f"{gettext('Sunset')}{day['astronomy'][0]['sunset']}" + + return res + + +def generate_condition_table(condition, lang, current=False): + res = "" + + if current: + key = "temp_" + else: + key = "temp" + + res += ( + f"{gettext('Condition')}" + f"{condition[get_weather_condition_key(lang)][0]['value']}" + ) + res += ( + f"{gettext('Temperature')}" + f"{condition[key+'C']}°C / {condition[key+'F']}°F" + ) + res += ( + f"{gettext('Feels like')}{condition['FeelsLikeC']}°C / {condition['FeelsLikeF']}°F" + ) + res += ( + f"{gettext('Wind')}{condition['winddir16Point']} — " + f"{condition['windspeedKmph']} km/h / {condition['windspeedMiles']} mph" + ) + res += ( + f"{gettext('Visibility')}{condition['visibility']} km / {condition['visibilityMiles']} mi" + ) + res += f"{gettext('Humidity')}{condition['humidity']}%" + + return res + + +def request(query, params): + if query.replace('/', '') in [":help", ":bash.function", ":translation"]: + return None + + if params["language"] == "all": + params["language"] = "en" + else: + params["language"] = params["language"].split("-")[0] + + params["url"] = url.format(query=quote(query), lang=params["language"]) + + params["raise_for_httperror"] = False + + return params + + +def response(resp): + results = [] + + if resp.status_code == 404: + return [] + + result = loads(resp.text) + + current = result["current_condition"][0] + location = result['nearest_area'][0] + + forecast_indices = {3: gettext('Morning'), 4: gettext('Noon'), 6: gettext('Evening'), 7: gettext('Night')} + + title = f"{location['areaName'][0]['value']}, {location['region'][0]['value']}" + + infobox = f"

{gettext('Current condition')}

" + + infobox += generate_condition_table(current, resp.search_params['language'], True) + + infobox += "
" + + for day in result["weather"]: + infobox += f"

{day['date']}

" + + infobox += "" + + infobox += generate_day_table(day) + + infobox += "
" + + infobox += "" + + for time in forecast_indices.items(): + infobox += f"" + + infobox += generate_condition_table(day['hourly'][time[0]], resp.search_params['language']) + + infobox += "
{time[1]}
" + + results.append( + { + "infobox": title, + "content": infobox, + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index e2e0d2266..a3e19768d 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -1846,6 +1846,11 @@ engines: results: HTML language: fr + - name: wttr.in + engine: wttr + shortcut: wttr + timeout: 9.0 + - name: brave shortcut: brave engine: xpath