]>
jfr.im git - erebus.git/blob - modules/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
22 import json
, time
, sys
23 from email
.utils
import parsedate
25 if sys
.version_info
.major
< 3:
26 from urllib
import urlopen
28 from urllib
.request
import urlopen
30 def location(person
, default
=None): return lib
.mod('userinfo').get(person
, 'location', default
=None)
32 def _dayofweek(dayname
):
33 return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname
.lower())
37 weather
= json
.load(urlopen(('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place
)).encode('utf8')))
38 if lib
.parent
.cfg
.getboolean('debug', 'weather'):
39 lib
.parent
.log('*', "?", repr(weather
))
40 if 'response' in weather
:
41 if 'error' in weather
['response']:
42 return "Error from Wunderground: %s" % (weather
['response']['error']['description'])
43 if 'results' in weather
['response']:
44 return "That search term is ambiguous. Please be more specific."
46 current
= weather
['current_observation']
48 measuredat
= list(parsedate(current
['observation_time_rfc822'])) # we have to turn this into a list so that we can assign to it.
49 measuredat
[6] = _dayofweek(current
['observation_time_rfc822'][0:3])
50 measuredatTZ
= current
['local_tz_short']
52 measuredat
= time
.gmtime()
53 measuredatTZ
= '(actual time unknown)'
54 loc
= current
['observation_location']
55 if loc
['city'] == "" or loc
['state'] == "": loc
= current
['display_location']
56 return u
"Weather in %(location)s: As of %(time)s %(tz)s, %(conditions)s, %(cel)s°C (%(far)s°F) (feels like %(flcel)s°C (%(flfar)s°F)). Wind %(wind)s. %(link)s" % {
57 'location': loc
['full'],
58 'time': time
.strftime("%a %H:%M", tuple(measuredat
)), # now we have to turn it back into a tuple because Py3's time.strftime requires it.
60 'conditions': current
['weather'],
61 'cel': current
['temp_c'], 'far': current
['temp_f'],
62 'flcel': current
['feelslike_c'], 'flfar': current
['feelslike_f'],
63 'wind': current
['wind_string'],
64 'link': current
['forecast_url'],
67 return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (lib
.parent
.trigger
,)
69 @lib.hook(('weather','w'), needchan
=False, wantchan
=True)
70 @lib.help('[<location>]', 'show weather for your location')
71 def weather(bot
, user
, chan
, realtarget
, *args
):
75 place
= location(user
)
77 place
= ' '.join(args
)
78 bot
.msg(chan
, _weather(place
))
80 @lib.hook(('weatheruser','wu'))
81 @lib.help('<user>', 'show weather for <user>\'s location')
83 def wu(bot
, user
, chan
, realtarget
, *args
):
84 bot
.msg(chan
, _weather(location(' '.join(args
))))