]>
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', | |
9 | 'compatible': [2], | |
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 | |
21 | import json, urllib, time, rfc822 | |
22 | ||
23 | def 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') |
27 | def 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))) | |
8b6595e1 | 35 | if 'response' in weather: |
36 | if 'error' in weather['response']: | |
37 | bot.msg(chan, "Error from Wunderground: %s" % (weather['response']['error']['description'])) | |
38 | return | |
39 | if 'results' in weather['response']: | |
40 | bot.msg(chan, "That search term is ambiguous. Please be more specific.") | |
41 | return | |
2b2ae281 | 42 | |
43 | current = weather['current_observation'] | |
9cd36ba7 | 44 | measuredat = rfc822.parsedate(current['observation_time_rfc822']) # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT |
2b2ae281 | 45 | measuredatTZ = current['local_tz_short'] |
46 | 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" % { | |
47 | 'location': current['observation_location']['full'], | |
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 | } | |
55 | bot.msg(chan, output) | |
56 | else: | |
50bd6182 | 57 | bot.msg(chan, "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (bot.parent.trigger)) |