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