refractored init

This commit is contained in:
hongtao 2015-03-02 23:13:40 +08:00
parent 45822feb6c
commit 10e1f7de10
3 changed files with 74 additions and 72 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
MANIFEST
dist/*
*.pyc

View File

@ -0,0 +1,71 @@
'''
MIT License
'''
from urllib2 import Request, urlopen
import json
import sys
__author__ = 'hongtaocai@gmail.com'
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'
}
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)
resp = urlopen(req)
content = resp.read().decode().strip()
content = content[3:]
return content
def replaceKeys(quotes):
global googleFinanceKeyToFullName
quotesWithReadableKey = []
for q in quotes:
qReadableKey = {}
for k in googleFinanceKeyToFullName:
if k in q:
qReadableKey[googleFinanceKeyToFullName[k]] = q[k]
quotesWithReadableKey.append(qReadableKey)
return quotesWithReadableKey
def getRealtimeQuotes(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', There is no delay for NYSE and NASDAQ stocks in googlefinance.
:param symbols: a list of stock symbols
:return: real-time quote
'''
content = json.loads(request(symbols))
return replaceKeys(content);
if __name__ == '__main__':
try:
symbols = sys.argv[1]
except:
symbols = "GOOG,AAPL"
symbols = symbols.split(',')
try:
print json.dumps(getRealtimeQuotes(symbols), indent=2)
except:
print "Fail"

View File

@ -1,72 +0,0 @@
'''
MIT License
'''
from urllib2 import Request, urlopen
import json
import sys
__author__ = 'hongtaocai on github'
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'
}
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)
resp = urlopen(req)
content = resp.read().decode().strip()
content = content[3:]
return content
def replaceKeys(quotes):
global googleFinanceKeyToFullName
quotesWithReadableKey = []
for q in quotes:
qReadableKey = {}
for k in googleFinanceKeyToFullName:
qReadableKey[googleFinanceKeyToFullName[k]] = q[k]
quotesWithReadableKey.append(qReadableKey)
return quotesWithReadableKey
def getRealtimeQuotes(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', There is no delay for NYSE and NASDAQ stocks in googlefinance.
:param symbols: a list of stock symbols
:return: real-time quote
'''
content = json.loads(request(symbols))
return replaceKeys(content);
if __name__ == '__main__':
try:
symbols = sys.argv[1]
except:
symbols = "GOOG,AAPL"
symbols = symbols.split(',')
try:
print json.dumps(getRealtimeQuotes(symbols), indent=2)
except:
print "Fail"