-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathweather.py
More file actions
207 lines (163 loc) · 7.02 KB
/
weather.py
File metadata and controls
207 lines (163 loc) · 7.02 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import plugin
import urllib.request
import urllib.error
import urllib.parse
from urllib.parse import quote
import json
defaults = {
'wunderground_key': '',
'forecasts_per_message': 3,
'days_ahead': 6,
}
def f_to_c(f):
return (f - 32) * (5./9.)
class Plugin(plugin.Plugin):
shelf_required = True
def register_commands(self):
self.commands = [('weather current <<location>>', self.current),
('weather forecast <<location>>', self.forecast),
('weather today <<location>>', self.today),
('set weather <<location>>', self.define),
('weather current', self.foruser(self.current)),
('weather forecast', self.foruser(self.forecast)),
('weather today', self.foruser(self.today)),
('weather', self.foruser(self.today))]
def prepare(self):
self.url = (
"http://api.wunderground.com/api/"
+ self.conf['wunderground_key'] + "/%s/q/%s.json")
self.current_msg = (
"\x02%s\x02 \x03#|\x03 %s\u00b0F \x03#:\x03 %s\u00b0C \x03#|\x03 "
"humidity \x03#:\x03 %s \x03#|\x03 wind \x03#:\x03 %s at %s mph")
self.suggestions_msg = "\x02matches\x02 \x03#|\x03 %s"
self.conditions_str = (
'\x02%s\x02 \x03#:\x03 %s%s \x03#:\x03 high %s\u00b0F %s\u00b0C '
'\x03#:\x03 low %s\u00b0F %s\u00b0C \x03#:\x03 %s%% humid \x03#:'
'\x03 %s at %s mph')
# self.conditions_str = "%s \x03#:\x03 %s"
def loc_string(self, location):
"""
Creates a human-readable location string based on a wunderground
results.
"""
if 'full' in location:
return location['full']
return ', '.join([
location[size] for size in ['name', 'state', 'country']
if location[size] != ''])
def suggest(self, data):
results = data['response']['results']
suggestions = [self.loc_string(r) for r in results]
suggestions_str = ' \x03#|\x03 '.join(suggestions)
msg = self.suggestions_msg % suggestions_str
return msg
def current(self, message, args):
"""
Fetches the current weather in `<location>`.
$<comchar>we c stafford, uk
>\x02Stafford, Staffordshire\x02 \x03#|\x03 61\u00b0F \x03#:\x03
16\u00b0C \x03#|\x03 humidity \x03#:\x03 68% \x03#|\x03 wind
\x03#:\x03 W at 9 mph
"""
url = self.url % ('conditions', quote(args["location"]))
print(url)
data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
if 'results' in data['response']:
msg = self.suggest(data)
else:
conditions = data['current_observation']
city = self.loc_string(conditions['display_location'])
tempf = conditions['temp_f']
tempc = conditions['temp_c']
humidity = conditions['relative_humidity']
wind_mph = conditions['wind_mph']
wind_dir = conditions['wind_dir'].lower()
msg = self.current_msg % (
city, tempf, tempc, humidity, wind_dir, wind_mph)
self.irc.privmsg(message.source, msg)
def forecast(self, message, args):
"""
Fetches the weather forecast for `<location>`.
$<comchar>we f nantwich and crewe
>\x02Crewe, Cheshire East\x02 \x03#|\x03 \x02mon\x02 \x03#:\x03 high
70\u00b0F 21\u00b0C \x03#:\x03 low 48\u00b0F 8\u00b0C \x03#:\x03 partly
sunny \x03#|\x03 \x02tue\x02 \x03#:\x03 high 75\u00b0F 23\u00b0C
\x03#:\x03 low 50\u00b0F 10\u00b0C \x03#:\x03 clear \x03#|\x03
\x02wed\x02 \x03#:\x03 high 82\u00b0F 27\u00b0C \x03#:\x03 low
59\u00b0F 15\u00b0C \x03#:\x03 partly sunny \x03#|\x03 \x02thu\x02
\x03#:\x03 high 77\u00b0F 25\u00b0C \x03#:\x03 low 52\u00b0F 11\u00b0C
\x03#:\x03 cloudy
"""
url = self.url % ('forecast10day', quote(args["location"]))
data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
if 'results' in data['response']:
msg = self.suggest(data)
self.irc.privmsg(message.source, msg)
else:
forecast = data['forecast']['simpleforecast']['forecastday']
summaries = []
for day in forecast:
precip_str = ''
qpf_str = ' \x03#:\x03 %s" %s'
qpf = day['qpf_allday']['in']
snow = day['snow_allday']['in']
if qpf > 0:
precip_str = qpf_str % (qpf, 'rain')
if snow > 0:
precip_str = qpf_str % (snow, 'snow') + precip_str
summaries.append(self.conditions_str % (
day['date']['weekday'].lower(),
day['conditions'].lower(),
precip_str,
day['high']['fahrenheit'],
day['high']['celsius'],
day['low']['fahrenheit'],
day['low']['celsius'],
day['avehumidity'],
day['avewind']['dir'].lower(),
day['avewind']['mph']
))
if 'today' in args:
summaries = summaries[:1]
else:
summaries = summaries[:self.conf['days_ahead']]
while summaries:
msg = ' \x03#|\x03 '.join(
summaries[0:self.conf['forecasts_per_message']])
summaries = summaries[self.conf['forecasts_per_message']:]
self.irc.privmsg(message.source, msg)
def today(self, message, args):
""" Like the above, but for one day only. """
args['today'] = True
self.forecast(message, args)
def define(self, message, args):
"""
Tell <pyfoot> who you are so you can use the weather commands
without specifying a location.
"""
url = self.url % ('forecast10day', quote(args["location"]))
data = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
if 'results' in data['response']:
msg = self.suggest(data)
self.irc.privmsg(message.source, msg)
return
elif not 'forecast' in data:
raise
self.shelf[message.nick.lower()] = args['location']
self.shelf.sync()
self.irc.privmsg(message.source,
'\x02%s\x02 lives in \x02%s\x02'
% (message.nick, args['location']))
def foruser(self, command):
"""
Return a wrapped command that will add a user's location to a query.
"""
def wrapped(message, args):
nick = message.nick.lower()
if nick in self.shelf:
args['location'] = self.shelf[nick]
command(message, args)
else:
self.irc.privmsg(message.source,
"i don't know where you live :<")
return wrapped