fix stock.py colors, minor cosmetic tweaks

This commit is contained in:
Ryan Hitchman 2011-02-02 23:49:56 -06:00
parent a769c2267e
commit bad65ba236
1 changed files with 14 additions and 13 deletions

View File

@ -5,31 +5,32 @@ from util import hook, http
@hook.command @hook.command
def stock(inp): def stock(inp):
'''.stock <symbol> -- returns information on a stock symbol''' '''.stock <symbol> -- gets information about a stock symbol'''
url = 'http://www.google.com/ig/api?stock=%s' url = 'http://www.google.com/ig/api?stock=%s'
parsed = http.get_xml(url, stock=inp) parsed = http.get_xml(url, stock=inp)
if len(parsed) != 1: 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 # Stuff the results in a dict for easy string formatting
results = {} results = dict((el.tag, el.attrib['data']) for el in parsed.xpath('//finance/*'))
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 we dont get a company name back, the symbol doesn't match a company
if results['company'] == "": if results['company'] == '':
return "Unknown ticker symbol %s" % inp return "unknown ticker symbol %s" % inp
if results['change'][1] == '-': if results['change'][0] == '-':
results['color'] = "5" results['color'] = "5"
else: else:
results['color'] = "3" results['color'] = "3"
format_str = "%(company)s - %(last)s %(currency)s " \ ret = "%(company)s - %(last)s %(currency)s " \
"\x03%(color)s%(change)s (%(perc_change)s)\x03 " \ "\x03%(color)s%(change)s (%(perc_change)s)\x03 " \
"as of %(trade_timestamp)s (delayed %(delay)s minutes)" "as of %(trade_timestamp)s" % results
return format_str % results if results['delay'] != '0':
ret += " (delayed %s minutes)" % results['delay']
return ret