mirror of
https://github.com/hongtaocai/googlefinance.git
synced 2025-02-25 18:55:20 -06:00
flak8
This commit is contained in:
parent
b69040843f
commit
b023e98ec4
@ -9,24 +9,26 @@ 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])
|
||||
# a deprecated but still active & correct api
|
||||
return 'http://finance.google.com/finance/info?client=ig&q=' \
|
||||
+ symbol_list
|
||||
|
||||
|
||||
def request(symbols):
|
||||
url = buildUrl(symbols)
|
||||
req = Request(url)
|
||||
@ -36,6 +38,7 @@ def request(symbols):
|
||||
content = content[3:]
|
||||
return content
|
||||
|
||||
|
||||
def replaceKeys(quotes):
|
||||
global googleFinanceKeyToFullName
|
||||
quotesWithReadableKey = []
|
||||
@ -47,6 +50,7 @@ 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
|
||||
@ -65,10 +69,10 @@ def getQuotes(symbols):
|
||||
: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:
|
||||
|
@ -1,6 +1,7 @@
|
||||
import googlefinance
|
||||
import unittest
|
||||
|
||||
|
||||
class TestQuotes(unittest.TestCase):
|
||||
|
||||
def test_one_symbol(self):
|
||||
@ -14,4 +15,3 @@ class TestQuotes(unittest.TestCase):
|
||||
self.assertEqual(quotes[0]["StockSymbol"], "GOOG")
|
||||
self.assertEqual(quotes[1]["Index"], "VIE")
|
||||
self.assertEqual(quotes[1]["StockSymbol"], "BKS")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user