This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/stock.py

31 lines
921 B
Python
Raw Normal View History

2013-12-01 08:44:04 +01:00
from util import hook, web
2011-11-20 10:23:31 +01:00
2013-09-04 12:30:04 +02:00
2011-11-20 10:23:31 +01:00
@hook.command
def stock(inp):
2013-11-10 21:46:09 +01:00
"""stock <symbol> -- gets stock information"""
sym = inp.strip().lower()
2011-11-20 10:23:31 +01:00
2013-11-10 21:46:09 +01:00
query = "SELECT * FROM yahoo.finance.quote WHERE symbol=@symbol LIMIT 1"
quote = web.query(query, {"symbol": sym}).one()
2011-11-20 10:23:31 +01:00
# if we don't get a company name back, the symbol doesn't match a company
2013-11-10 21:13:15 +01:00
if quote['Change'] is None:
2013-12-01 08:44:04 +01:00
return "Unknown ticker symbol: {}".format(sym)
2013-11-10 21:13:15 +01:00
change = float(quote['Change'])
price = float(quote['LastTradePriceOnly'])
if change < 0:
quote['color'] = "5"
2011-11-20 10:23:31 +01:00
else:
2013-11-10 21:13:15 +01:00
quote['color'] = "3"
2011-11-20 10:23:31 +01:00
2013-11-10 21:13:15 +01:00
quote['PercentChange'] = 100 * change / (price - change)
2013-12-01 08:44:04 +01:00
print quote
2011-11-20 10:23:31 +01:00
2013-12-01 08:44:04 +01:00
return u"\x02{Name}\x02 (\x02{symbol}\x02) - {LastTradePriceOnly} " \
"\x03{color}{Change} ({PercentChange:.2f}%)\x03 " \
"Day Range: {DaysRange} " \
"MCAP: {MarketCapitalization}".format(**quote)