]>
jfr.im git - erebus.git/blob - modules/weather.py
5157a7dcd232535953bf1037c4194672283c28c0
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
, urllib
, time
, rfc822
23 def location(person
, default
=None): return lib
.mod('userinfo')._get
(person
, 'location', default
=None)
25 @lib.hook(('weather','w'), needchan
=False)
26 @lib.help('[<location>]', 'show weather for your location')
27 def weather(bot
, user
, chan
, realtarget
, *args
):
29 place
= location(user
)
31 place
= ' '.join(args
)
34 weather
= json
.load(urllib
.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place
)))
36 if 'response' in weather
:
37 if 'error' in weather
['response']:
38 bot
.msg(chan
, "Error from Wunderground: %s" % (weather
['response']['error']['description']))
40 if 'results' in weather
['response']:
41 bot
.msg(chan
, "That search term is ambiguous. Please be more specific.")
44 current
= weather
['current_observation']
45 measuredat
= rfc822
.parsedate_tz(current
['observation_time_rfc822'])[:-1] # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT
46 measuredatTZ
= current
['local_tz_short']
47 output
= 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" % {
48 'location': current
['observation_location']['full'],
49 'time': time
.strftime("%a %H:%M", measuredat
), 'tz': measuredatTZ
,
50 'conditions': current
['weather'],
51 'cel': current
['temp_c'], 'far': current
['temp_f'],
52 'flcel': current
['feelslike_c'], 'flfar': current
['feelslike_f'],
53 'wind': current
['wind_string'],
54 'link': current
['forecast_url'],
58 bot
.msg(chan
, "I don't know where to look! Try SETINFO LOCATION <your location>")