From e769bad2b76bd4b9b155332de719db5b30c25308 Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Wed, 6 Nov 2013 09:54:38 -0800 Subject: [PATCH] stock.py: change to use yahoo API --- plugins/stock.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/plugins/stock.py b/plugins/stock.py index e05c6fc..101cd5e 100644 --- a/plugins/stock.py +++ b/plugins/stock.py @@ -5,33 +5,31 @@ from util import hook, http @hook.command def stock(inp): - '''.stock -- gets information about a stock symbol''' + '''.stock -- gets stock information''' - url = 'http://www.google.com/ig/api?stock=%s' + url = ('http://query.yahooapis.com/v1/public/yql?format=json&' + 'env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys') - parsed = http.get_xml(url, stock=inp) + parsed = http.get_json(url, q='select * from yahoo.finance.quote ' + 'where symbol in ("%s")' % inp) # heh, SQLI - if len(parsed) != 1: - return "error getting stock info" - - # Stuff the results in a dict for easy string formatting - results = dict((el.tag, el.attrib['data']) - for el in parsed.xpath('//finance/*')) + quote = parsed['query']['results']['quote'] # if we dont get a company name back, the symbol doesn't match a company - if results['company'] == '': + if quote['Change'] is None: return "unknown ticker symbol %s" % inp - if results['change'][0] == '-': - results['color'] = "5" + if quote['Change'][0] == '-': + quote['color'] = "5" else: - results['color'] = "3" + quote['color'] = "3" - ret = "%(company)s - %(last)s %(currency)s " \ - "\x03%(color)s%(change)s (%(perc_change)s%%)\x03 " \ - "as of %(trade_timestamp)s" % results + quote['Percent_Change'] = (100 * float(quote['Change']) / + float(quote['LastTradePriceOnly'])) - if results['delay'] != '0': - ret += " (delayed %s minutes)" % results['delay'] + ret = "%(Name)s - %(LastTradePriceOnly)s " \ + "\x03%(color)s%(Change)s (%(Percent_Change).2f%%)\x03 " \ + "Day Range: %(DaysRange)s " \ + "MCAP: %(MarketCapitalization)s" % quote return ret