.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
wttr.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""wttr.in (weather forecast service)"""
3
4from json import loads
5from urllib.parse import quote
6from flask_babel import gettext
7
8about = {
9 "website": "https://wttr.in",
10 "wikidata_id": "Q107586666",
11 "official_api_documentation": "https://github.com/chubin/wttr.in#json-output",
12 "use_official_api": True,
13 "require_api_key": False,
14 "results": "JSON",
15}
16
17categories = ["weather"]
18
19url = "https://wttr.in/{query}?format=j1&lang={lang}"
20
21
23 if lang == "en":
24 return "weatherDesc"
25
26 return "lang_" + lang.lower()
27
28
30 res = ""
31
32 res += f"<tr><td>{gettext('Average temp.')}</td><td>{day['avgtempC']}°C / {day['avgtempF']}°F</td></tr>"
33 res += f"<tr><td>{gettext('Min temp.')}</td><td>{day['mintempC']}°C / {day['mintempF']}°F</td></tr>"
34 res += f"<tr><td>{gettext('Max temp.')}</td><td>{day['maxtempC']}°C / {day['maxtempF']}°F</td></tr>"
35 res += f"<tr><td>{gettext('UV index')}</td><td>{day['uvIndex']}</td></tr>"
36 res += f"<tr><td>{gettext('Sunrise')}</td><td>{day['astronomy'][0]['sunrise']}</td></tr>"
37 res += f"<tr><td>{gettext('Sunset')}</td><td>{day['astronomy'][0]['sunset']}</td></tr>"
38
39 return res
40
41
42def generate_condition_table(condition, lang, current=False):
43 res = ""
44
45 if current:
46 key = "temp_"
47 else:
48 key = "temp"
49
50 res += (
51 f"<tr><td><b>{gettext('Condition')}</b></td>"
52 f"<td><b>{condition[get_weather_condition_key(lang)][0]['value']}</b></td></tr>"
53 )
54 res += (
55 f"<tr><td><b>{gettext('Temperature')}</b></td>"
56 f"<td><b>{condition[key+'C']}°C / {condition[key+'F']}°F</b></td></tr>"
57 )
58 res += (
59 f"<tr><td>{gettext('Feels like')}</td><td>{condition['FeelsLikeC']}°C / {condition['FeelsLikeF']}°F</td></tr>"
60 )
61 res += (
62 f"<tr><td>{gettext('Wind')}</td><td>{condition['winddir16Point']} — "
63 f"{condition['windspeedKmph']} km/h / {condition['windspeedMiles']} mph</td></tr>"
64 )
65 res += (
66 f"<tr><td>{gettext('Visibility')}</td><td>{condition['visibility']} km / {condition['visibilityMiles']} mi</td>"
67 )
68 res += f"<tr><td>{gettext('Humidity')}</td><td>{condition['humidity']}%</td></tr>"
69
70 return res
71
72
73def request(query, params):
74 if query.replace('/', '') in [":help", ":bash.function", ":translation"]:
75 return None
76
77 if params["language"] == "all":
78 params["language"] = "en"
79 else:
80 params["language"] = params["language"].split("-")[0]
81
82 params["url"] = url.format(query=quote(query), lang=params["language"])
83
84 params["raise_for_httperror"] = False
85
86 return params
87
88
89def response(resp):
90 results = []
91
92 if resp.status_code == 404:
93 return []
94
95 result = loads(resp.text)
96
97 current = result["current_condition"][0]
98 location = result['nearest_area'][0]
99
100 forecast_indices = {3: gettext('Morning'), 4: gettext('Noon'), 6: gettext('Evening'), 7: gettext('Night')}
101
102 title = f"{location['areaName'][0]['value']}, {location['region'][0]['value']}"
103
104 infobox = f"<h3>{gettext('Current condition')}</h3><table><tbody>"
105
106 infobox += generate_condition_table(current, resp.search_params['language'], True)
107
108 infobox += "</tbody></table>"
109
110 for day in result["weather"]:
111 infobox += f"<h3>{day['date']}</h3>"
112
113 infobox += "<table><tbody>"
114
115 infobox += generate_day_table(day)
116
117 infobox += "</tbody></table>"
118
119 infobox += "<table><tbody>"
120
121 for time in forecast_indices.items():
122 infobox += f"<tr><td rowspan=\"7\"><b>{time[1]}</b></td></tr>"
123
124 infobox += generate_condition_table(day['hourly'][time[0]], resp.search_params['language'])
125
126 infobox += "</tbody></table>"
127
128 results.append(
129 {
130 "infobox": title,
131 "content": infobox,
132 }
133 )
134
135 return results
get_weather_condition_key(lang)
Definition wttr.py:22
generate_day_table(day)
Definition wttr.py:29
response(resp)
Definition wttr.py:89
generate_condition_table(condition, lang, current=False)
Definition wttr.py:42
request(query, params)
Definition wttr.py:73