diff --git a/googlefinance/__init__.py b/googlefinance/__init__.py index f4193bb..ae25c59 100644 --- a/googlefinance/__init__.py +++ b/googlefinance/__init__.py @@ -10,37 +10,40 @@ 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's' : u'LastTradeSize', - u'c' : u'Change', - u'c' : u'ChangePercent', - u'el' : u'ExtHrsLastTradePrice', - u'el_cur' : u'ExtHrsLastTradeWithCurrency', - u'elt' : u'ExtHrsLastTradeDateTimeLong', - u'ec' : u'ExtHrsChange', - u'ecp' : u'ExtHrsChangePercent', + 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'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=' \ + symbol_list + def buildNewsUrl(symbol, qs='&start=0&num=1000'): - return 'http://www.google.com/finance/company_news?output=json&q=' \ + return 'http://www.google.com/finance/company_news?output=json&q=' \ + symbol + qs + def request(symbols): url = buildUrl(symbols) req = Request(url) @@ -50,16 +53,17 @@ def request(symbols): content = content[3:] return content + def requestNews(symbol): url = buildNewsUrl(symbol) - print "url: ", url + 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'] + # print "total news: ", content_json['total_number_of_news'] article_json = [] news_json = content_json['clusters'] @@ -70,6 +74,7 @@ def requestNews(symbol): return article_json + def replaceKeys(quotes): global googleFinanceKeyToFullName quotesWithReadableKey = [] @@ -81,6 +86,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 @@ -102,10 +108,12 @@ def getQuotes(symbols): if type(symbols) == type('str'): symbols = [symbols] content = json.loads(request(symbols)) - return replaceKeys(content); + return replaceKeys(content) + def getNews(symbol): - return requestNews(symbol); + return requestNews(symbol) + if __name__ == '__main__': try: @@ -116,4 +124,4 @@ if __name__ == '__main__': symbols = symbols.split(',') print(json.dumps(getNews("GOOG"), indent=2)) - print(json.dumps(getQuotes(symbols), indent=2)) + print(json.dumps(getQuotes(symbols), indent=2)) diff --git a/setup.py b/setup.py index 4236cd2..fcd3604 100644 --- a/setup.py +++ b/setup.py @@ -1,23 +1,25 @@ from distutils.core import setup setup( - name = 'googlefinance', - packages = ['googlefinance'], # this must be the same as the name above - version = '0.7', - description = 'Python module to get real-time (no delay) stock data from Google Finance API', - author = 'Hongtao Cai', - author_email = 'caiht.ht@gmail.com', - url = 'https://github.com/hongtaocai/googlefinance', # use the URL to the github repo - download_url = 'https://github.com/hongtaocai/googlefinance/tarball/0.4', # I'll explain this in a second - keywords = ['googlefinance', 'stock', 'price' , 'finance', 'google', 'real-time'], # arbitrary keywords - classifiers = [ - 'License :: OSI Approved :: MIT License', - 'Operating System :: OS Independent', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.6', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.3', - 'Programming Language :: Python :: 3.4', - ], + name='googlefinance', + packages=['googlefinance'], # this must be the same as the name above + version='0.7', + description='Python module to get real-time (no delay) stock data from Google Finance API', + author='Hongtao Cai', + author_email='caiht.ht@gmail.com', + url='https://github.com/hongtaocai/googlefinance', # use the URL to the github repo + download_url='https://github.com/hongtaocai/googlefinance/tarball/0.4', # I'll explain this in a second + keywords=['googlefinance', 'stock', 'price', 'finance', + 'google', 'real-time'], # arbitrary keywords + classifiers=[ + 'License :: OSI Approved :: MIT License', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + ], + install_requires=['demjson'] )