2011-02-02 05:39:39 +00:00
|
|
|
import random
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-06 17:54:38 +00:00
|
|
|
parsed = http.get_json(url, q='select * from yahoo.finance.quote '
|
|
|
|
'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
|
|
|
change = float(quote['Change'])
|
|
|
|
price = float(quote['LastTradePriceOnly'])
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-11-06 17:54:38 +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
|