-
Notifications
You must be signed in to change notification settings - Fork 0
/
live_wttr.py
111 lines (94 loc) · 3.25 KB
/
live_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
103
104
105
106
107
108
109
110
111
from urllib.parse import quote, urlencode
import geocoder
import time
from libqtile.widget import GenPollUrl
class LiveWttr(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.
"""
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",
{"": ""},
"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, "
"``'u'`` - United States units",
),
(
"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, json=False, **config)
self.add_defaults(LiveWttr.defaults)
self.url = self._get_url()
def _get_url(self):
if not self.location:
return None
params = {
"format": self.format,
"lang": self.lang,
}
location = None
try:
location = geocoder.ip('me').json["city"]
except:
location = "London"
self.location = {location: 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