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/plugins/stock.py

41 lines
1.2 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import random
from util import hook, http
url = 'http://www.google.com/ig/api'
2011-11-20 10:23:31 +01:00
@hook.command
def stock(inp):
2012-05-16 05:07:27 +02:00
"stock <symbol> -- Gets information about stock symbol <symbol>."
2011-11-20 10:23:31 +01:00
parsed = http.get_xml(url, stock=inp)
if len(parsed) != 1:
return "error getting stock info"
# Stuff the results in a dict for easy string formatting
results = dict((el.tag, el.attrib['data'])
for el in parsed.xpath('//finance/*'))
# if we dont get a company name back, the symbol doesn't match a company
if results['company'] == '':
return "error: unknown ticker symbol (%s)" % inp
if results['last'] == '0.00':
2012-06-03 16:57:00 +02:00
return "%(company)s - last known stock value was 0.00 %(currency)s" \
" as of %(trade_timestamp)s" % (results)
2011-11-20 10:23:31 +01:00
if results['change'][0] == '-':
results['color'] = "5"
else:
results['color'] = "3"
ret = "%(company)s - %(last)s %(currency)s " \
"\x03%(color)s%(change)s (%(perc_change)s%%)\x03 " \
2011-11-20 10:23:31 +01:00
"as of %(trade_timestamp)s" % results
if results['delay'] != '0':
ret += " (delayed %s minutes)" % results['delay']
return ret