forked from russellb/sopelmods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wunderground.py
executable file
·100 lines (89 loc) · 3.69 KB
/
wunderground.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
import urllib
import json
def wuweather(phenny, input):
loc = input.group(2)
#grab weather from api
try:
weather = json.loads(urllib.urlopen('http://api.wunderground.com/api/587a76efcc30bee2/conditions/almanac/astronomy/forecast/pws:0/q/' + loc +'.json').read())
except:
return
if not weather:
return
#does the response contain an error
if 'error' in weather['response']:
phenny.say("wuweather: %s" % weather['response']['error']['description'])
return
#does the response contain extra 'results'
elif 'results' in weather['response']:
output = 'wuweather: Ambigious Search, options: '
counter = 0
for index in weather['response']['results']:
if counter == 0:
output += "%s %s %s " % (index['city'], index['state'], index['country'])
else:
output += "/ %s %s %s " % (index['city'], index['state'], index['country'])
counter += 1
phenny.say(output)
return
#if we made it here, everything should be good
try:
#format the city name
city = "[%s, %s, %s]" % \
(weather['current_observation']['display_location']['city'],
weather['current_observation']['display_location']['state'],
weather['current_observation']['display_location']['country'])
#output the averages, sunrise, moon
#some locations don't have an almanac
if weather['almanac']['airport_code'] != "":
almanac_output = "Avg High: %sF, %sC Low: %sF, %sC " % \
(weather['almanac']['temp_high']['normal']['F'],
weather['almanac']['temp_high']['normal']['C'],
weather['almanac']['temp_low']['normal']['F'],
weather['almanac']['temp_low']['normal']['C'])
else:
almanac_output = ""
#output sunrise w/ almanac if available
phenny.say("%s %sSunrise: %s:%s Sunset: %s:%s" % \
(city,
almanac_output,
weather['moon_phase']['sunrise']['hour'],
weather['moon_phase']['sunrise']['minute'],
weather['moon_phase']['sunset']['hour'],
weather['moon_phase']['sunset']['minute']))
#output the current weather
phenny.say("%s Current: %s %sF, %sC Humidity: %s, Wind: %s" % \
(city,
weather['current_observation']['weather'],
weather['current_observation']['temp_f'],
weather['current_observation']['temp_c'],
weather['current_observation']['relative_humidity'],
weather['current_observation']['wind_string']))
#output first 3 days of forcast (today, tomorrow, second day)
counter = 0
for index in weather['forecast']['simpleforecast']['forecastday']:
if counter == 0:
day = 'Today'
elif counter == 1:
day = 'Tomorrow'
else:
day = index['date']['weekday']
phenny.say("%s %s: %s [POP: %s%%] [High: %sF, %sC Low: %sF, %sC] Humidity: [Max: %s%%, Min: %s%%, Avg: %s%%] Wind: [%smph, %skph]" % \
(city,
day,
index['conditions'],
index['pop'],
index['high']['fahrenheit'],
index['high']['celsius'],
index['low']['fahrenheit'],
index['low']['celsius'],
index['maxhumidity'],
index['minhumidity'],
index['avehumidity'],
index['avewind']['mph'],
index['avewind']['kph']))
counter +=1
if counter >= 3: break
phenny.say("%s URL: %s" % (city, weather['current_observation']['forecast_url']))
except:
return
wuweather.commands = ['wuweather']