/
weather.py
74 lines (64 loc) · 1.81 KB
/
weather.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
#!/usr/bin/env python
'''
DAVID LETTIER
(C) 2015.
http://www.lettier.com/
Slackotron
'''
import requests
import weather_settings
import plugins.plugin_base
class Weather(plugins.plugin_base.PluginBase):
api_endpoint = weather_settings.API_ENDPOINT
zipcode = weather_settings.ZIP
use_fahrenheit = weather_settings.USE_FAHRENHEIT
activation_strings = [
'what\'s the weather like',
'what\'s the weather',
'how\'s the weather',
'whats the weather like',
'whats the weather',
'hows the weather'
]
def _callback(self, channel, user, message):
try:
response = requests.get(
self.api_endpoint,
params={
'inputstring': str(self.zipcode)
},
allow_redirects=False
)
self.location = response.headers['location']
response = requests.get(
self.location + '&FcstType=json'
)
except Exception as e:
self.error(e)
return 'I dunno.'
if response.status_code != 200:
self.warning('status_code != 200')
return 'I dunno.'
json = response.json()
temp = int(json['currentobservation']['Temp'])
description = json['currentobservation']['Weather']
temp_symbol = 'F'
if self.use_fahrenheit is False:
temp = self.__fahrenheit_to_celsius(temp)
temp_symbol = 'C'
return ''.join([
'I am seeing ',
description,
'. '
'The temp is ',
str(int(temp)),
temp_symbol,
'. ',
self.location
])
def __kelvin_to_celsius(self, kelvin_reading):
return float(kelvin_reading) - 273.15
def __celsius_to_fahrenheit(self, celsius_reading):
return ((float(celsius_reading) * 9) / 5) + 32.0
def __fahrenheit_to_celsius(self, fahrenheit_reading):
return (fahrenheit_reading - 32.0) * (5 / 9.0)