fix to handle dynamic result key set from google finance

This commit is contained in:
Austin Hartline 2016-05-10 15:22:16 -05:00
parent 6a541b7fda
commit ffdbe23ea2

View File

@ -7,7 +7,6 @@ try:
except ImportError: # python 2
from urllib2 import Request, urlopen
__author__ = 'Hongtao Cai'
googleFinanceKeyToFullName = {
u'id' : u'ID',
@ -31,15 +30,6 @@ googleFinanceKeyToFullName = {
u'pcls_fix': u'PreviousClosePrice'
}
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 buildNewsUrl(symbol, qs='&start=0&num=1000'):
return 'http://www.google.com/finance/company_news?output=json&q=' \
+ symbol + qs
def request(symbols):
url = buildUrl(symbols)
@ -50,36 +40,24 @@ def request(symbols):
content = content[3:]
return content
def requestNews(symbol):
url = buildNewsUrl(symbol)
print("url: ", url)
req = Request(url)
resp = urlopen(req)
content = resp.read()
content_json = demjson.decode(content)
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
#print "total news: ", content_json['total_number_of_news']
article_json = []
news_json = content_json['clusters']
for cluster in news_json:
for article in cluster:
if article == 'a':
article_json.extend(cluster[article])
return article_json
def replaceKeys(quotes):
global googleFinanceKeyToFullName
quotesWithReadableKey = []
for q in quotes:
qReadableKey = {}
print(q)
for k in googleFinanceKeyToFullName:
if k in q:
qReadableKey[googleFinanceKeyToFullName[k]] = q[k]
quotesWithReadableKey.append(qReadableKey)
return quotesWithReadableKey
q[googleFinanceKeyToFullName[k]] = q.pop(k)
return quotes
def getQuotes(symbols):
'''
@ -104,8 +82,6 @@ def getQuotes(symbols):
content = json.loads(request(symbols))
return replaceKeys(content);
def getNews(symbol):
return requestNews(symbol);
if __name__ == '__main__':
try:
@ -115,5 +91,4 @@ if __name__ == '__main__':
symbols = symbols.split(',')
print(json.dumps(getNews("GOOG"), indent=2))
print(json.dumps(getQuotes(symbols), indent=2))