2011-02-02 05:39:39 +00:00
|
|
|
from util import hook, http
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command
|
|
|
|
def stock(inp):
|
2013-11-06 17:54:38 +00:00
|
|
|
'''.stock <symbol> -- gets stock information'''
|
2011-02-02 05:39:39 +00:00
|
|
|
|
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')
|
2011-02-02 05:39:39 +00:00
|
|
|
|
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
|
2011-02-02 05:39:39 +00:00
|
|
|
|
2013-11-06 17:54:38 +00:00
|
|
|
quote = parsed['query']['results']['quote']
|
2011-02-02 05:39:39 +00:00
|
|
|
|
2011-02-03 05:49:56 +00:00
|
|
|
# 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:
|
2011-02-03 05:49:56 +00:00
|
|
|
return "unknown ticker symbol %s" % inp
|
2011-02-02 05:39:39 +00:00
|
|
|
|
2013-11-07 18:11:09 +00:00
|
|
|
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
|
2013-11-07 18:11:09 +00:00
|
|
|
|
|
|
|
if change < 0:
|
2013-11-06 17:54:38 +00:00
|
|
|
quote['color'] = "5"
|
2011-02-02 05:39:39 +00:00
|
|
|
else:
|
2013-11-06 17:54:38 +00:00
|
|
|
quote['color'] = "3"
|
2011-02-02 05:39:39 +00:00
|
|
|
|
2013-11-07 18:11:09 +00:00
|
|
|
quote['PercentChange'] = 100 * change / (price - change)
|
2011-02-02 05:39:39 +00:00
|
|
|
|
2015-10-22 22:22:08 +00:00
|
|
|
ret = "%(Name)s - %(LastTradePriceOnly)s " \
|
2013-11-07 18:11:09 +00:00
|
|
|
"\x03%(color)s%(Change)s (%(PercentChange).2f%%)\x03 " \
|
2013-11-06 17:54:38 +00:00
|
|
|
"Day Range: %(DaysRange)s " \
|
|
|
|
"MCAP: %(MarketCapitalization)s" % quote
|
2011-02-03 05:49:56 +00:00
|
|
|
|
|
|
|
return ret
|