# 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"]) return params def response(resp): results = [] 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 += "
" infobox += f"

{day['date']}

" infobox += f"

{gettext('Overview')}

" infobox += "" infobox += generate_day_table(day) infobox += "
" infobox += f"

{gettext('Forecast')}

" 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