This repository has been archived on 2023-04-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
CloudBot/plugins/stock.py
2013-11-11 09:13:15 +13:00

37 lines
No EOL
1.1 KiB
Python
Executable file

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