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
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:
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