From a769c2267eae66020290e405e8151f49ab858e89 Mon Sep 17 00:00:00 2001 From: Chris Skalenda Date: Wed, 2 Feb 2011 13:39:39 +0800 Subject: [PATCH] Add a stock lookup plugin that uses google's api. --- plugins/stock.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 plugins/stock.py diff --git a/plugins/stock.py b/plugins/stock.py new file mode 100644 index 0000000..7389547 --- /dev/null +++ b/plugins/stock.py @@ -0,0 +1,35 @@ +import random + +from util import hook, http + + +@hook.command +def stock(inp): + '''.stock -- returns information on a stock symbol''' + + url = 'http://www.google.com/ig/api?stock=%s' + + parsed = http.get_xml(url, stock=inp) + + if len(parsed) != 1: + raise IOError('Error using the stock API') + + # Stuff the results in a dict for easy string formatting + results = {} + for elm in parsed.find("finance"): + results[elm.tag] = elm.get("data") + + # if we dont get a company name back, the symbol doesnt match a company + if results['company'] == "": + return "Unknown ticker symbol %s" % inp + + if results['change'][1] == '-': + results['color'] = "5" + else: + results['color'] = "3" + + format_str = "%(company)s - %(last)s %(currency)s " \ + "\x03%(color)s%(change)s (%(perc_change)s)\x03 " \ + "as of %(trade_timestamp)s (delayed %(delay)s minutes)" + + return format_str % results