stock.py: change to use yahoo API

This commit is contained in:
Ryan Hitchman 2013-11-06 09:54:38 -08:00
parent 248b220d9b
commit e769bad2b7
1 changed files with 16 additions and 18 deletions

View File

@ -5,33 +5,31 @@ from util import hook, http
@hook.command @hook.command
def stock(inp): def stock(inp):
'''.stock <symbol> -- gets information about a stock symbol''' '''.stock <symbol> -- 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: quote = parsed['query']['results']['quote']
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/*'))
# if we dont get a company name back, the symbol doesn't match a company # 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 return "unknown ticker symbol %s" % inp
if results['change'][0] == '-': if quote['Change'][0] == '-':
results['color'] = "5" quote['color'] = "5"
else: else:
results['color'] = "3" quote['color'] = "3"
ret = "%(company)s - %(last)s %(currency)s " \ quote['Percent_Change'] = (100 * float(quote['Change']) /
"\x03%(color)s%(change)s (%(perc_change)s%%)\x03 " \ float(quote['LastTradePriceOnly']))
"as of %(trade_timestamp)s" % results
if results['delay'] != '0': ret = "%(Name)s - %(LastTradePriceOnly)s " \
ret += " (delayed %s minutes)" % results['delay'] "\x03%(color)s%(Change)s (%(Percent_Change).2f%%)\x03 " \
"Day Range: %(DaysRange)s " \
"MCAP: %(MarketCapitalization)s" % quote
return ret return ret