]>
jfr.im git - erebus.git/blob - modules/weather.py
1 # Erebus IRC bot - Author: Erebus Team
3 # This file is released into the public domain; see http://unlicense.org/
7 'author': 'Erebus Team',
8 'license': 'public domain',
10 'depends': ['userinfo'],
16 lib
= modlib
.modlib(__name__
)
17 modstart
= lib
.modstart
21 import json
, time
, sys
22 from email
.utils
import parsedate
24 if sys
.version_info
.major
< 3:
25 from urllib
import urlopen
27 from urllib
.request
import urlopen
29 def location(person
, default
=None): return lib
.mod('userinfo')._get
(person
, 'location', default
=None)
31 def _dayofweek(dayname
):
32 return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname
.lower())
36 weather
= json
.load(urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place
)))
37 if lib
.parent
.cfg
.getboolean('debug', 'weather'):
38 lib
.parent
.log('*', "?", repr(weather
))
39 if 'response' in weather
:
40 if 'error' in weather
['response']:
41 return "Error from Wunderground: %s" % (weather
['response']['error']['description'])
42 if 'results' in weather
['response']:
43 return "That search term is ambiguous. Please be more specific."
45 current
= weather
['current_observation']
46 measuredat
= list(parsedate(current
['observation_time_rfc822'])) # we have to turn this into a list so that we can assign to it.
47 measuredat
[6] = _dayofweek(current
['observation_time_rfc822'][0:3])
48 measuredatTZ
= current
['local_tz_short']
49 loc
= current
['observation_location']
50 if loc
['city'] == "" or loc
['state'] == "": loc
= current
['display_location']
51 return u
"Weather in %(location)s: As of %(time)s %(tz)s, %(conditions)s, %(cel)s\u00B0C (%(far)s\u00B0F) (feels like %(flcel)s\u00B0C (%(flfar)s\u00B0F)). Wind %(wind)s. %(link)s" % {
52 'location': loc
['full'],
53 '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.
55 'conditions': current
['weather'],
56 'cel': current
['temp_c'], 'far': current
['temp_f'],
57 'flcel': current
['feelslike_c'], 'flfar': current
['feelslike_f'],
58 'wind': current
['wind_string'],
59 'link': current
['forecast_url'],
62 return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (lib
.parent
.trigger
)
64 @lib.hook(('weather','w'), needchan
=False, wantchan
=True)
65 @lib.help('[<location>]', 'show weather for your location')
66 def weather(bot
, user
, chan
, realtarget
, *args
):
70 place
= location(user
)
72 place
= ' '.join(args
)
73 bot
.msg(chan
, _weather(place
))
75 @lib.hook(('weatheruser','wu'))
76 @lib.help('<user>', 'show weather for <user>\'s location')
77 def wu(bot
, user
, chan
, realtarget
, *args
):
78 bot
.msg(chan
, _weather(location(' '.join(args
))))