h/plugins/stock.py

38 lines
1.1 KiB
Python
Raw Normal View History

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'''
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')
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
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
change = float(quote['Change'])
price = float(quote['LastTradePriceOnly'])
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)
2013-11-06 17:54:38 +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