From 2da87e8611522e54415021b339b2c2631f054e6f Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 15 Oct 2011 22:57:28 +0200 Subject: Added WolframAlpha Weather module. --- modules/weather.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 modules/weather.py 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")) -- cgit v1.2.3