]>
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 | |
9221e6f6 | 21 | import json, time, sys |
a28e2ae9 | 22 | from email.utils import parsedate |
2b2ae281 | 23 | |
9221e6f6 | 24 | if sys.version_info.major < 3: |
25 | from urllib import urlopen | |
26 | else: | |
27 | from urllib.request import urlopen | |
28 | ||
2b2ae281 | 29 | def location(person, default=None): return lib.mod('userinfo')._get(person, 'location', default=None) |
30 | ||
6e8f06d6 | 31 | def _dayofweek(dayname): |
32 | return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower()) | |
33 | ||
cdd662d8 | 34 | def _weather(place): |
2b2ae281 | 35 | if place is not None: |
9221e6f6 | 36 | weather = json.load(urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place))) |
cdd662d8 | 37 | if lib.parent.cfg.getboolean('debug', 'weather'): |
38 | lib.parent.log('*', "?", repr(weather)) | |
8b6595e1 | 39 | if 'response' in weather: |
40 | if 'error' in weather['response']: | |
cdd662d8 | 41 | return "Error from Wunderground: %s" % (weather['response']['error']['description']) |
8b6595e1 | 42 | if 'results' in weather['response']: |
cdd662d8 | 43 | return "That search term is ambiguous. Please be more specific." |
2b2ae281 | 44 | |
45 | current = weather['current_observation'] | |
b4dee2f6 | 46 | try: |
47 | measuredat = list(parsedate(current['observation_time_rfc822'])) # we have to turn this into a list so that we can assign to it. | |
48 | measuredat[6] = _dayofweek(current['observation_time_rfc822'][0:3]) | |
49 | measuredatTZ = current['local_tz_short'] | |
50 | except: | |
51 | measuredat = time.gmtime() | |
52 | measuredatTZ = '(actual time unknown)' | |
6112c0cd | 53 | loc = current['observation_location'] |
cc38d6f4 | 54 | if loc['city'] == "" or loc['state'] == "": loc = current['display_location'] |
cdd662d8 | 55 | 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 | 56 | 'location': loc['full'], |
9221e6f6 | 57 | 'time': time.strftime("%a %H:%M", tuple(measuredat)), # now we have to turn it back into a tuple because Py3's time.strftime requires it. |
58 | 'tz': measuredatTZ, | |
2b2ae281 | 59 | 'conditions': current['weather'], |
60 | 'cel': current['temp_c'], 'far': current['temp_f'], | |
61 | 'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'], | |
62 | 'wind': current['wind_string'], | |
63 | 'link': current['forecast_url'], | |
64 | } | |
2b2ae281 | 65 | else: |
7b1c0c93 | 66 | return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (lib.parent.trigger) |
cdd662d8 | 67 | |
68 | @lib.hook(('weather','w'), needchan=False, wantchan=True) | |
69 | @lib.help('[<location>]', 'show weather for your location') | |
70 | def weather(bot, user, chan, realtarget, *args): | |
71 | if chan is None: | |
72 | chan = user | |
73 | if len(args) == 0: | |
74 | place = location(user) | |
75 | else: | |
76 | place = ' '.join(args) | |
77 | bot.msg(chan, _weather(place)) | |
78 | ||
79 | @lib.hook(('weatheruser','wu')) | |
80 | @lib.help('<user>', 'show weather for <user>\'s location') | |
81 | def wu(bot, user, chan, realtarget, *args): | |
82 | bot.msg(chan, _weather(location(' '.join(args)))) |