h/plugins/stock.py

47 lines
1.5 KiB
Python
Raw Normal View History

from util import hook, http
@hook.command
def stock(inp):
2013-11-06 17:54:38 +00:00
'''.stock <symbol> -- gets stock information'''
2013-11-06 17:54:38 +00:00
url = ('http://query.yahooapis.com/v1/public/yql?format=json&'
'env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
2015-10-22 21:25:06 +00:00
parsed = http.get_json(url, q='select * from yahoo.finance.quotes '
2013-11-06 17:54:38 +00:00
'where symbol in ("%s")' % inp) # heh, SQLI
2013-11-06 17:54:38 +00:00
quote = parsed['query']['results']['quote']
# if we dont get a company name back, the symbol doesn't match a company
2013-11-06 17:54:38 +00:00
if quote['Change'] is None:
return "unknown ticker symbol %s" % inp
price = float(quote['LastTradePriceOnly'])
2015-10-22 21:25:06 +00:00
change = float(quote['Change'])
if quote['Open'] and quote['Bid'] and quote['Ask']:
open_price = float(quote['Open'])
bid = float(quote['Bid'])
ask = float(quote['Ask'])
if price < bid:
price = bid
elif price > ask:
price = ask
change = price - open_price
2015-10-22 22:22:08 +00:00
quote['LastTradePriceOnly'] = "%.2f" % price
2015-10-22 21:25:06 +00:00
quote['Change'] = ("+%.2f" % change) if change >= 0 else change
if change < 0:
2013-11-06 17:54:38 +00:00
quote['color'] = "5"
else:
2013-11-06 17:54:38 +00:00
quote['color'] = "3"
quote['PercentChange'] = 100 * change / (price - change)
2015-10-22 22:22:08 +00:00
ret = "%(Name)s - %(LastTradePriceOnly)s " \
"\x03%(color)s%(Change)s (%(PercentChange).2f%%)\x03 " \
2013-11-06 17:54:38 +00:00
"Day Range: %(DaysRange)s " \
"MCAP: %(MarketCapitalization)s" % quote
return ret