diff --git a/plugins/stock.py b/plugins/stock.py index 7389547..4af6665 100644 --- a/plugins/stock.py +++ b/plugins/stock.py @@ -5,31 +5,32 @@ from util import hook, http @hook.command def stock(inp): - '''.stock -- returns information on a stock symbol''' + '''.stock -- gets information about 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') + return "error getting stock info" # Stuff the results in a dict for easy string formatting - results = {} - for elm in parsed.find("finance"): - results[elm.tag] = elm.get("data") + results = dict((el.tag, el.attrib['data']) for el in parsed.xpath('//finance/*')) - # if we dont get a company name back, the symbol doesnt match a company - if results['company'] == "": - return "Unknown ticker symbol %s" % inp + # if we dont get a company name back, the symbol doesn't match a company + if results['company'] == '': + return "unknown ticker symbol %s" % inp - if results['change'][1] == '-': + if results['change'][0] == '-': 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)" + ret = "%(company)s - %(last)s %(currency)s " \ + "\x03%(color)s%(change)s (%(perc_change)s)\x03 " \ + "as of %(trade_timestamp)s" % results - return format_str % results + if results['delay'] != '0': + ret += " (delayed %s minutes)" % results['delay'] + + return ret