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