diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-10-15 22:57:28 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-10-15 22:57:28 +0200 |
commit | 2da87e8611522e54415021b339b2c2631f054e6f (patch) | |
tree | b1154a378c85867f6ece061fa68c2091176c16b6 | |
parent | 938c31f3462679a76060017ac6ed0b9ad8f18b65 (diff) |
Added WolframAlpha Weather module.
-rw-r--r-- | modules/weather.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/modules/weather.py b/modules/weather.py new file mode 100644 index 0000000..9e44721 --- /dev/null +++ b/modules/weather.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- + +info = { + 'author': 'Atle H. Havsø', + 'title': 'WolframAlpha Weather', + 'description': 'WolframAlpha weather search', +} + +import urllib2 +import xml.etree.ElementTree as xml + + +server = "http://api.wolframalpha.com/v2/query?" + + +class Module: + def __init__(self, bot): + self.appid = config.get('module/weather', 'api_key') + self.irc = bot + self.irc.register_keyword('!weather', self) + + def keyword(self, nick, channel, kw, msg): + target = channel if not channel == self.irc.nickname else nick.split('!')[0] + args = msg.split() + if len(args) == 0: + self.irc.msg(target, 'Usage: !weather search') + return + results = self.search(' '.join(args)) + if results: + self.irc.msg(target, results.encode('utf-8')) + + + + def search(self, input): + query = server + "appid=" + self.appid + "&input=weather%20" + input + "&format=plaintext" + try: + result = urllib2.urlopen(query) + result = result.read() + result = self.parseXml(result) + except: + result = 'Error: Try again' + + return result + + def parseXml(self, result): + root = xml.XML(result) + #root = tree.getroot() + data = [] + for pod in root: + for subpod in pod: + for plain in subpod: + if plain.text != None: data.append(plain.text) + result = data[0] + result += '\n' + "Today: " + data[3].replace('\n', ' - ') + result += '\n' + "Tonight: " + data[4].replace('\n', ' - ') + return result + + +if __name__ == '__main__': + import sys + m = Module() + search = ' '.join(sys.argv[1:]) + result = m.search(search) + print(result.encode("utf-8")) |