diff options
Diffstat (limited to 'modules')
-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")) |