]> jfr.im git - erebus.git/blame - modules/weather.py
revert apiversion to 0
[erebus.git] / modules / weather.py
CommitLineData
2b2ae281 1# Erebus IRC bot - Author: Erebus Team
2# weather module
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
fa93b933 9 'compatible': [0],
2b2ae281 10 'depends': ['userinfo'],
11 'softdeps': ['help'],
12}
13
14# preamble
15import modlib
16lib = modlib.modlib(__name__)
17modstart = lib.modstart
18modstop = lib.modstop
19
20# module code
21import json, urllib, time, rfc822
22
23def location(person, default=None): return lib.mod('userinfo')._get(person, 'location', default=None)
24
9d5b6eda 25@lib.hook(('weather','w'), needchan=False)
2b2ae281 26@lib.help('[<location>]', 'show weather for your location')
27def weather(bot, user, chan, realtarget, *args):
28 if len(args) == 0:
29 place = location(user)
30 else:
31 place = ' '.join(args)
32
33 if place is not None:
34 weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)))
6112c0cd 35 if bot.parent.cfg.getboolean('debug', 'weather'):
36 bot.log("?", repr(weather))
8b6595e1 37 if 'response' in weather:
38 if 'error' in weather['response']:
39 bot.msg(chan, "Error from Wunderground: %s" % (weather['response']['error']['description']))
40 return
41 if 'results' in weather['response']:
42 bot.msg(chan, "That search term is ambiguous. Please be more specific.")
43 return
2b2ae281 44
45 current = weather['current_observation']
9cd36ba7 46 measuredat = rfc822.parsedate(current['observation_time_rfc822']) # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT
2b2ae281 47 measuredatTZ = current['local_tz_short']
6112c0cd 48 loc = current['observation_location']
49 if loc['city'] == "": loc = current['display_location']
2b2ae281 50 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" % {
6112c0cd 51 'location': loc['full'],
2b2ae281 52 'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ,
53 'conditions': current['weather'],
54 'cel': current['temp_c'], 'far': current['temp_f'],
55 'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'],
56 'wind': current['wind_string'],
57 'link': current['forecast_url'],
58 }
59 bot.msg(chan, output)
60 else:
50bd6182 61 bot.msg(chan, "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (bot.parent.trigger))