]>
Commit | Line | Data |
---|---|---|
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 | |
6 | modinfo = { | |
7 | 'author': 'Erebus Team', | |
8 | 'license': 'public domain', | |
fa93b933 | 9 | 'compatible': [0], |
2b2ae281 | 10 | 'depends': ['userinfo'], |
11 | 'softdeps': ['help'], | |
12 | } | |
13 | ||
14 | # preamble | |
15 | import modlib | |
16 | lib = modlib.modlib(__name__) | |
17 | modstart = lib.modstart | |
18 | modstop = lib.modstop | |
19 | ||
20 | # module code | |
a28e2ae9 | 21 | import json, urllib, time |
22 | from email.utils import parsedate | |
2b2ae281 | 23 | |
24 | def location(person, default=None): return lib.mod('userinfo')._get(person, 'location', default=None) | |
25 | ||
6e8f06d6 | 26 | def _dayofweek(dayname): |
27 | return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower()) | |
28 | ||
cdd662d8 | 29 | def _weather(place): |
2b2ae281 | 30 | if place is not None: |
31 | weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place))) | |
cdd662d8 | 32 | if lib.parent.cfg.getboolean('debug', 'weather'): |
33 | lib.parent.log('*', "?", repr(weather)) | |
8b6595e1 | 34 | if 'response' in weather: |
35 | if 'error' in weather['response']: | |
cdd662d8 | 36 | return "Error from Wunderground: %s" % (weather['response']['error']['description']) |
8b6595e1 | 37 | if 'results' in weather['response']: |
cdd662d8 | 38 | return "That search term is ambiguous. Please be more specific." |
2b2ae281 | 39 | |
40 | current = weather['current_observation'] | |
a28e2ae9 | 41 | measuredat = list(parsedate(current['observation_time_rfc822'])) |
6e8f06d6 | 42 | measuredat[6] = _dayofweek(current['observation_time_rfc822'][0:3]) |
2b2ae281 | 43 | measuredatTZ = current['local_tz_short'] |
6112c0cd | 44 | loc = current['observation_location'] |
cc38d6f4 | 45 | if loc['city'] == "" or loc['state'] == "": loc = current['display_location'] |
cdd662d8 | 46 | 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" % { |
6112c0cd | 47 | 'location': loc['full'], |
2b2ae281 | 48 | 'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ, |
49 | 'conditions': current['weather'], | |
50 | 'cel': current['temp_c'], 'far': current['temp_f'], | |
51 | 'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'], | |
52 | 'wind': current['wind_string'], | |
53 | 'link': current['forecast_url'], | |
54 | } | |
2b2ae281 | 55 | else: |
7b1c0c93 | 56 | return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (lib.parent.trigger) |
cdd662d8 | 57 | |
58 | @lib.hook(('weather','w'), needchan=False, wantchan=True) | |
59 | @lib.help('[<location>]', 'show weather for your location') | |
60 | def weather(bot, user, chan, realtarget, *args): | |
61 | if chan is None: | |
62 | chan = user | |
63 | if len(args) == 0: | |
64 | place = location(user) | |
65 | else: | |
66 | place = ' '.join(args) | |
67 | bot.msg(chan, _weather(place)) | |
68 | ||
69 | @lib.hook(('weatheruser','wu')) | |
70 | @lib.help('<user>', 'show weather for <user>\'s location') | |
71 | def wu(bot, user, chan, realtarget, *args): | |
72 | bot.msg(chan, _weather(location(' '.join(args)))) |