]>
jfr.im git - erebus.git/blob - modules/weather.py
9fb0f4231e562d5001cd592cfb2c37fe23b7e02a
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)
27 weather
= json
.load(urllib
.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place
)))
28 if lib
.parent
.cfg
.getboolean('debug', 'weather'):
29 lib
.parent
.log('*', "?", repr(weather
))
30 if 'response' in weather
:
31 if 'error' in weather
['response']:
32 return "Error from Wunderground: %s" % (weather
['response']['error']['description'])
33 if 'results' in weather
['response']:
34 return "That search term is ambiguous. Please be more specific."
36 current
= weather
['current_observation']
37 measuredat
= rfc822
.parsedate(current
['observation_time_rfc822']) # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT
38 measuredatTZ
= current
['local_tz_short']
39 loc
= current
['observation_location']
40 if loc
['city'] == "" or loc
['state'] == "": loc
= current
['display_location']
41 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" % {
42 'location': loc
['full'],
43 'time': time
.strftime("%a %H:%M", measuredat
), 'tz': measuredatTZ
,
44 'conditions': current
['weather'],
45 'cel': current
['temp_c'], 'far': current
['temp_f'],
46 'flcel': current
['feelslike_c'], 'flfar': current
['feelslike_f'],
47 'wind': current
['wind_string'],
48 'link': current
['forecast_url'],
51 return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (bot
.parent
.trigger
)
53 @lib.hook(('weather','w'), needchan
=False, wantchan
=True)
54 @lib.help('[<location>]', 'show weather for your location')
55 def weather(bot
, user
, chan
, realtarget
, *args
):
59 place
= location(user
)
61 place
= ' '.join(args
)
62 bot
.msg(chan
, _weather(place
))
64 @lib.hook(('weatheruser','wu'))
65 @lib.help('<user>', 'show weather for <user>\'s location')
66 def wu(bot
, user
, chan
, realtarget
, *args
):
67 bot
.msg(chan
, _weather(location(' '.join(args
))))