mirror of
https://github.com/hongtaocai/googlefinance.git
synced 2025-02-25 18:55:20 -06:00
Merge bbda3eaa54
into b69040843f
This commit is contained in:
commit
32ab8bf957
@ -9,23 +9,27 @@ except ImportError: # python 2
|
||||
__author__ = 'Hongtao Cai'
|
||||
|
||||
googleFinanceKeyToFullName = {
|
||||
u'id' : u'ID',
|
||||
u't' : u'StockSymbol',
|
||||
u'e' : u'Index',
|
||||
u'l' : u'LastTradePrice',
|
||||
u'l_cur' : u'LastTradeWithCurrency',
|
||||
u'ltt' : u'LastTradeTime',
|
||||
u'lt_dts' : u'LastTradeDateTime',
|
||||
u'lt' : u'LastTradeDateTimeLong',
|
||||
u'div' : u'Dividend',
|
||||
u'yld' : u'Yield'
|
||||
u'id': u'ID',
|
||||
u't': u'StockSymbol',
|
||||
u'e': u'Index',
|
||||
u'l': u'LastTradePrice',
|
||||
u'l_cur': u'LastTradeWithCurrency',
|
||||
u'ltt': u'LastTradeTime',
|
||||
u'lt_dts': u'LastTradeDateTime',
|
||||
u'lt': u'LastTradeDateTimeLong',
|
||||
u'div': u'Dividend',
|
||||
u'yld': u'Yield'
|
||||
}
|
||||
|
||||
|
||||
def buildUrl(symbols):
|
||||
symbol_list = ','.join([symbol for symbol in symbols])
|
||||
aux_symbols = [s for s in symbols if s.strip() != ""]
|
||||
if len(aux_symbols) == 0 or len(aux_symbols) != len(symbols):
|
||||
raise Exception("Can not be blank.")
|
||||
# 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):
|
||||
url = buildUrl(symbols)
|
||||
@ -36,6 +40,7 @@ def request(symbols):
|
||||
content = content[3:]
|
||||
return content
|
||||
|
||||
|
||||
def replaceKeys(quotes):
|
||||
global googleFinanceKeyToFullName
|
||||
quotesWithReadableKey = []
|
||||
@ -47,28 +52,60 @@ def replaceKeys(quotes):
|
||||
quotesWithReadableKey.append(qReadableKey)
|
||||
return quotesWithReadableKey
|
||||
|
||||
|
||||
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 type(symbols) == type('str'):
|
||||
if isinstance(symbols, str):
|
||||
symbols = [symbols]
|
||||
content = json.loads(request(symbols))
|
||||
return replaceKeys(content);
|
||||
return replaceKeys(content)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@ -1,6 +1,7 @@
|
||||
import googlefinance
|
||||
import unittest
|
||||
|
||||
|
||||
class TestQuotes(unittest.TestCase):
|
||||
|
||||
def test_one_symbol(self):
|
||||
@ -15,3 +16,9 @@ class TestQuotes(unittest.TestCase):
|
||||
self.assertEqual(quotes[1]["Index"], "VIE")
|
||||
self.assertEqual(quotes[1]["StockSymbol"], "BKS")
|
||||
|
||||
def test_empty(self):
|
||||
try:
|
||||
googlefinance.getQuotes("")
|
||||
self.assertTrue(False)
|
||||
except Exception as e:
|
||||
self.assertTrue("Can not be blank." in str(e))
|
||||
|
Loading…
Reference in New Issue
Block a user