h/plugins/stock.py

36 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
2013-11-06 17:54:38 +00:00
if quote['Change'][0] == '-':
quote['color'] = "5"
else:
2013-11-06 17:54:38 +00:00
quote['color'] = "3"
2013-11-06 17:54:38 +00:00
quote['Percent_Change'] = (100 * float(quote['Change']) /
float(quote['LastTradePriceOnly']))
2013-11-06 17:54:38 +00:00
ret = "%(Name)s - %(LastTradePriceOnly)s " \
"\x03%(color)s%(Change)s (%(Percent_Change).2f%%)\x03 " \
"Day Range: %(DaysRange)s " \
"MCAP: %(MarketCapitalization)s" % quote
return ret