This commit is contained in:
Henrique Lopes 2015-08-22 23:05:13 -03:00
parent b023e98ec4
commit 0b63fbaa1d
2 changed files with 52 additions and 14 deletions

View File

@ -23,10 +23,12 @@ googleFinanceKeyToFullName = {
def buildUrl(symbols):
symbol_list = ','.join([symbol for symbol in symbols])
aux_symbols = filter(lambda value: value.strip() != "", symbols)
if len(aux_symbols) != len(symbols):
raise Exception("Does not input value empty.")
# a deprecated but still active & correct api
return 'http://finance.google.com/finance/info?client=ig&q=' \
+ symbol_list
return 'http://finance.google.com/finance/info?client=ig&q={0}'.format(
','.join(aux_symbols))
def request(symbols):
@ -53,21 +55,52 @@ def replaceKeys(quotes):
def getQuotes(symbols):
'''
get real-time quotes (index, last trade price, last trade time, etc) for stocks, using google api: http://finance.google.com/finance/info?client=ig&q=symbols
get real-time quotes (index, last trade price, last trade time, etc)
for stocks, using google api:
http://finance.google.com/finance/info?client=ig&q=symbols
Unlike python package 'yahoo-finance' (15 min delay), There is no delay for NYSE and NASDAQ stocks in 'googlefinance' package.
Unlike python package 'yahoo-finance' (15 min delay),
There is no delay for NYSE and NASDAQ stocks in 'googlefinance' package.
example:
quotes = getQuotes('AAPL')
return:
[{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'129.09', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'129.09', u'Yield': u'1.46', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'0.47', u'StockSymbol': u'AAPL', u'ID': u'22144'}]
example:
quotes = getQuotes('AAPL')
return:
[{u'Index': u'NASDAQ',
u'LastTradeWithCurrency': u'129.09',
u'LastTradeDateTime': u'2015-03-02T16:04:29Z',
u'LastTradePrice': u'129.09',
u'Yield': u'1.46',
u'LastTradeTime': u'4:04PM EST',
u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST',
u'Dividend': u'0.47',
u'StockSymbol': u'AAPL',
u'ID': u'22144'}]
quotes = getQuotes(['AAPL', 'GOOG'])
return:
[{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'129.09', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'129.09', u'Yield': u'1.46', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'0.47', u'StockSymbol': u'AAPL', u'ID': u'22144'}, {u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'571.34', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'571.34', u'Yield': u'', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'', u'StockSymbol': u'GOOG', u'ID': u'304466804484872'}]
quotes = getQuotes(['AAPL', 'GOOG'])
return:
[{u'Index': u'NASDAQ',
u'LastTradeWithCurrency': u'129.09',
u'LastTradeDateTime': u'2015-03-02T16:04:29Z',
u'LastTradePrice': u'129.09',
u'Yield': u'1.46',
u'LastTradeTime': u'4:04PM EST',
u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST',
u'Dividend': u'0.47',
u'StockSymbol': u'AAPL',
u'ID': u'22144'},
{u'Index': u'NASDAQ',
u'LastTradeWithCurrency': u'571.34',
u'LastTradeDateTime': u'2015-03-02T16:04:29Z',
u'LastTradePrice': u'571.34',
u'Yield': u'',
u'LastTradeTime': u'4:04PM EST',
u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST',
u'Dividend': u'',
u'StockSymbol': u'GOOG',
u'ID': u'304466804484872'}]
:param symbols: a single symbol or a list of stock symbols
:return: real-time quotes list
:param symbols: a single symbol or a list of stock symbols
:return: real-time quotes list
'''
if isinstance(symbols, str):
symbols = [symbols]

View File

@ -15,3 +15,8 @@ class TestQuotes(unittest.TestCase):
self.assertEqual(quotes[0]["StockSymbol"], "GOOG")
self.assertEqual(quotes[1]["Index"], "VIE")
self.assertEqual(quotes[1]["StockSymbol"], "BKS")
def test_empty(self):
with self.assertRaises(Exception) as e:
googlefinance.getQuotes("")
self.assertTrue("Can not be blank." in str(e.exception))