-
Notifications
You must be signed in to change notification settings - Fork 0
/
wttr.py
102 lines (86 loc) · 3.09 KB
/
wttr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from urllib.parse import quote, urlencode
from libqtile.widget import GenPollUrl, base
class Wttr(GenPollUrl):
"""Display weather widget provided by wttr.in_.
.. _wttr.in: https://github.com/chubin/wttr.in/
To specify your own custom output format, use the special %-notation
(example: 'My_city: %t(%f), wind: %w'):
- %c Weather condition,
- %C Weather condition textual name,
- %h Humidity,
- %t Temperature (Actual),
- %f Temperature (Feels Like),
- %w Wind,
- %l Location,
- %m Moonphase 🌑🌒🌓🌔🌕🌖🌗🌘,
- %M Moonday,
- %p precipitation (mm),
- %P pressure (hPa),
- %D Dawn !,
- %S Sunrise !,
- %z Zenith !,
- %s Sunset !,
- %d Dusk !. (!times are shown in the local timezone)
Add the character ``~`` at the beginning to get weather for some special
location: ``~Vostok Station`` or ``~Eiffel Tower``.
Also can use IP-addresses (direct) or domain names (prefixed with @) to
specify a location:
``@github.com``, ``123.456.678.123``
Specify multiple locations as dictionary ::
location={
'Minsk': 'Minsk',
'64.127146,-21.873472': 'Reykjavik',
}
Cities will change randomly every update.
"""
orientation = base.ORIENTATION_HORIZONTAL
defaults = [
(
'format', '3',
'Display text format. Choose presets in range 1-4 (Ex. ``"1"``) '
'or build your own custom output format, use the special '
'%-notation. See https://github.com/chubin/wttr.in#one-line-output'
),
('json', False, 'Is Json?'),
(
'lang', 'en',
'Display text language. List of supported languages '
'https://wttr.in/:translation'
),
(
'location', None,
'Dictionary. Key is a city or place name, or GPS coordinates. '
'Value is a display name.'
),
(
'units', 'm',
"``'m'`` - metric, ``'M'`` - show wind speed in m/s, "
"``'s'`` - imperial"
),
(
'update_interval', 600,
'Update interval in seconds. Recommendation: if you want to '
'display multiple locations alternately, maybe set a smaller '
'interval, ex. ``30``.'
)
]
def __init__(self, **config):
GenPollUrl.__init__(self, **config)
self.add_defaults(Wttr.defaults)
self.url = self._get_url()
def _get_url(self):
if not self.location:
return None
params = {
'format': self.format,
'lang': self.lang,
}
location = ':'.join(
quote(loc) for loc in self.location
)
url = f'https://wttr.in/{location}?{self.units}&{urlencode(params)}'
return url
def parse(self, response):
for coord in self.location:
response = response.strip().replace(coord, self.location[coord])
return response