]>
jfr.im git - erebus.git/blob - modules/weatherstack_weather.py
1 # Erebus IRC bot - Author: Erebus Team
2 # vim: fileencoding=utf-8
4 # This file is released into the public domain; see http://unlicense.org/
8 'author': 'Erebus Team',
9 'license': 'public domain',
11 'depends': ['userinfo'],
17 lib
= modlib
.modlib(__name__
)
18 modstart
= lib
.modstart
26 if sys
.version_info
.major
< 3:
27 from urllib
import urlopen
, quote_plus
29 from urllib
.request
import urlopen
30 from urllib
.parse
import quote_plus
32 def location(person
, default
=None): return lib
.mod('userinfo').get(person
, 'location', default
=None)
34 def _dayofweek(dayname
):
35 return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname
.lower())
38 t
= d
['current']['observation_time']
40 #mo = re.match(r"(\d\d):(\d\d) (AM|PM)", t)
46 return round(celsius
* 9.0/5 + 32, 2)
49 return round(kmh
/ 1.60934, 2)
52 if not lib
.parent
.cfg
.get('weatherstack_weather', 'key'):
53 return "Weather is not enabled - please set the API key in the config file"
56 url
= 'http://api.weatherstack.com/current?access_key=%s&query=%s' % (lib
.parent
.cfg
.get('weatherstack_weather', 'key'), quote_plus(place
))
57 if sys
.version_info
.major
< 3:
58 url
= url
.encode('utf8')
59 weather
= json
.load(urlopen(url
))
60 if lib
.parent
.cfg
.getboolean('debug', 'weather'):
61 lib
.parent
.log('*', "?", repr(weather
))
62 if 'error' in weather
:
63 return "Error from WeatherStack: (%d) %s" % (weather
['error']['code'], weather
['error']['info'])
65 return u
"Weather in %(location)s, %(region)s, %(country)s: As of %(time)s, %(conditions)s, %(cel)s°C (%(far)s°F) (feels like %(flcel)s°C (%(flfar)s°F)). Wind %(windk)skm/h (%(windm)smph) %(winddir)s." % {
66 'location': weather
['location']['name'],
67 'region': weather
['location']['region'],
68 'country': weather
['location']['country'],
69 'time': _time_adjust(weather
),
70 'conditions': ', '.join(weather
['current']['weather_descriptions']),
71 'cel': weather
['current']['temperature'], 'far': _c2f(weather
['current']['temperature']),
72 'flcel': weather
['current']['feelslike'], 'flfar': _c2f(weather
['current']['feelslike']),
73 'windk': weather
['current']['wind_speed'], 'windm': _kmh2mph(weather
['current']['wind_speed']),
74 'winddir': weather
['current']['wind_dir'],
77 return "I don't know where to look! Try %ssetinfo location <your location>" % (lib
.parent
.trigger
,)
79 @lib.hook(('weather','w'), needchan
=False, wantchan
=True)
80 @lib.help('[<location>]', 'show weather for your location')
81 def weather(bot
, user
, chan
, realtarget
, *args
):
85 place
= location(user
)
87 place
= ' '.join(args
)
88 bot
.msg(chan
, _weather(place
))
90 @lib.hook(('weatheruser','wu'))
91 @lib.help('<user>', 'show weather for <user>\'s location')
92 def wu(bot
, user
, chan
, realtarget
, *args
):
97 bot
.msg(chan
, _weather(location(u
)))