mirror of
https://github.com/hongtaocai/googlefinance.git
synced 2025-02-25 18:55:20 -06:00
Corrected api url
This commit is contained in:
parent
717818df57
commit
ce41648b80
@ -1,45 +1,38 @@
|
||||
import json
|
||||
import sys
|
||||
import demjson
|
||||
|
||||
try:
|
||||
from urllib.request import Request, urlopen
|
||||
except ImportError: # python 2
|
||||
from urllib2 import Request, urlopen
|
||||
|
||||
__author__ = 'Hongtao Cai'
|
||||
|
||||
googleFinanceKeyToFullName = {
|
||||
u'id' : u'ID',
|
||||
u't' : u'StockSymbol',
|
||||
u'e' : u'Index',
|
||||
u'l' : u'LastTradePrice',
|
||||
u'lo' : u'LowestTradePrice',
|
||||
u'hi' : u'HighestTradePrice',
|
||||
u'op' : u'Open',
|
||||
u'mc' : u'MarketCapacity',
|
||||
u'c' : u'Change',
|
||||
u'cp' : u'ChangeInPercent',
|
||||
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's' : u'LastTradeSize',
|
||||
u'c' : u'Change',
|
||||
u'cp' : u'ChangePercent',
|
||||
u'el' : u'ExtHrsLastTradePrice',
|
||||
u'el_cur' : u'ExtHrsLastTradeWithCurrency',
|
||||
u'elt' : u'ExtHrsLastTradeDateTimeLong',
|
||||
u'ec' : u'ExtHrsChange',
|
||||
u'ecp' : u'ExtHrsChangePercent',
|
||||
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=' \
|
||||
return 'http://finance.google.com/finance?output=json&q=' \
|
||||
+ symbol_list
|
||||
|
||||
def buildNewsUrl(symbol, qs='&start=0&num=1000'):
|
||||
return 'http://www.google.com/finance/company_news?output=json&q=' \
|
||||
+ symbol + qs
|
||||
# https://finance.google.com/finance/historical?q=aapl&output=csv
|
||||
# https://finance.google.com/finance/historical?q=aapl&startdate=Dec+14%2C+2016&enddate=Dec+13%2C+2017
|
||||
|
||||
def request(symbols):
|
||||
url = buildUrl(symbols)
|
||||
@ -48,28 +41,9 @@ def request(symbols):
|
||||
# remove special symbols such as the pound symbol
|
||||
content = resp.read().decode('ascii', 'ignore').strip()
|
||||
content = content[3:]
|
||||
# print(content)
|
||||
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)
|
||||
|
||||
#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 = []
|
||||
@ -104,9 +78,6 @@ def getQuotes(symbols):
|
||||
content = json.loads(request(symbols))
|
||||
return replaceKeys(content);
|
||||
|
||||
def getNews(symbol):
|
||||
return requestNews(symbol);
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
symbols = sys.argv[1]
|
||||
@ -115,5 +86,7 @@ if __name__ == '__main__':
|
||||
|
||||
symbols = symbols.split(',')
|
||||
|
||||
print(json.dumps(getNews("GOOG"), indent=2))
|
||||
print(json.dumps(getQuotes(symbols), indent=2))
|
||||
try:
|
||||
print(json.dumps(getQuotes(symbols), indent=2))
|
||||
except:
|
||||
print("Fail")
|
||||
|
Loading…
Reference in New Issue
Block a user