├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST ├── README ├── README.md ├── dist └── googlefinance-0.4.tar.gz ├── example.py ├── googlefinance └── __init__.py ├── setup.py ├── tests └── test_googlefinance.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | python: 4 | - "2.6" 5 | - "2.7" 6 | - "pypy" 7 | - "3.3" 8 | - "3.4" 9 | 10 | install: 11 | - python setup.py install 12 | 13 | script: 14 | - nosetests 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Hongtao Cai 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | README 3 | setup.py 4 | googlefinance/__init__.py 5 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | googlefinance 2 | ============= 3 | 4 | Python module to get stock data from Google Finance API 5 | 6 | This module provides **no delay**, **real time** stock data in NYSE & 7 | NASDAQ. 8 | 9 | Another awesome module, 10 | `yahoo-finance `__'s 11 | data is delayed by 15 min, but it provides convenient apis to fetch 12 | historical day-by-day stock data. 13 | 14 | Install 15 | ------- 16 | 17 | From PyPI with pip: 18 | 19 | :: 20 | 21 | $pip install googlefinance 22 | 23 | From development repo (requires git) 24 | 25 | :: 26 | 27 | $git clone https://github.com/hongtaocai/googlefinance.git 28 | $cd googlefinance 29 | $python setup.py install 30 | 31 | Usage example 32 | ------------- 33 | 34 | :: 35 | 36 | >>> from googlefinance import getQuotes 37 | >>> import json 38 | >>> print json.dumps(getQuotes('AAPL'), indent=2) 39 | [ 40 | { 41 | "Index": "NASDAQ", 42 | "LastTradeWithCurrency": "129.09", 43 | "LastTradeDateTime": "2015-03-02T16:04:29Z", 44 | "LastTradePrice": "129.09", 45 | "Yield": "1.46", 46 | "LastTradeTime": "4:04PM EST", 47 | "LastTradeDateTimeLong": "Mar 2, 4:04PM EST", 48 | "Dividend": "0.47", 49 | "StockSymbol": "AAPL", 50 | "ID": "22144" 51 | } 52 | ] 53 | >>> print json.dumps(getQuotes(['AAPL', 'VIE:BKS']), indent=2) 54 | [ 55 | { 56 | "Index": "NASDAQ", 57 | "LastTradeWithCurrency": "129.36", 58 | "LastTradeDateTime": "2015-03-03T16:02:36Z", 59 | "LastTradePrice": "129.36", 60 | "LastTradeTime": "4:02PM EST", 61 | "LastTradeDateTimeLong": "Mar 3, 4:02PM EST", 62 | "StockSymbol": "AAPL", 63 | "ID": "22144" 64 | }, 65 | { 66 | "Index": "VIE", 67 | "LastTradeWithCurrency": "17.10", 68 | "LastTradeDateTime": "2015-03-03T13:30:30Z", 69 | "LastTradePrice": "17.10", 70 | "LastTradeTime": "1:30PM GMT+1", 71 | "LastTradeDateTimeLong": "Mar 3, 1:30PM GMT+1", 72 | "StockSymbol": "BKS", 73 | "ID": "978541942832888" 74 | } 75 | ] 76 | 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # googlefinance 2 | Python module to get stock data from Google Finance API 3 | 4 | This module provides **no delay**, **real time** stock data in NYSE & NASDAQ. 5 | 6 | Another awesome module, [yahoo-finance](https://github.com/lukaszbanasiak/yahoo-finance)'s data is delayed by 15 min, but it provides convenient apis to fetch historical day-by-day stock data. 7 | 8 | **Please note that Google has closed this endpoint -- so the module may not work properly** 9 | 10 | ##Install 11 | From PyPI with pip: 12 | 13 | $pip install googlefinance 14 | 15 | From development repo (requires git) 16 | 17 | $git clone https://github.com/hongtaocai/googlefinance.git 18 | $cd googlefinance 19 | $python setup.py install 20 | 21 | ##Usage example 22 | 23 | >>> from googlefinance import getQuotes 24 | >>> import json 25 | >>> print json.dumps(getQuotes('AAPL'), indent=2) 26 | [ 27 | { 28 | "Index": "NASDAQ", 29 | "LastTradeWithCurrency": "129.09", 30 | "LastTradeDateTime": "2015-03-02T16:04:29Z", 31 | "LastTradePrice": "129.09", 32 | "Yield": "1.46", 33 | "LastTradeTime": "4:04PM EST", 34 | "LastTradeDateTimeLong": "Mar 2, 4:04PM EST", 35 | "Dividend": "0.47", 36 | "StockSymbol": "AAPL", 37 | "ID": "22144" 38 | } 39 | ] 40 | >>> print json.dumps(getQuotes(['AAPL', 'VIE:BKS']), indent=2) 41 | [ 42 | { 43 | "Index": "NASDAQ", 44 | "LastTradeWithCurrency": "129.36", 45 | "LastTradeDateTime": "2015-03-03T16:02:36Z", 46 | "LastTradePrice": "129.36", 47 | "LastTradeTime": "4:02PM EST", 48 | "LastTradeDateTimeLong": "Mar 3, 4:02PM EST", 49 | "StockSymbol": "AAPL", 50 | "ID": "22144" 51 | }, 52 | { 53 | "Index": "VIE", 54 | "LastTradeWithCurrency": "17.10", 55 | "LastTradeDateTime": "2015-03-03T13:30:30Z", 56 | "LastTradePrice": "17.10", 57 | "LastTradeTime": "1:30PM GMT+1", 58 | "LastTradeDateTimeLong": "Mar 3, 1:30PM GMT+1", 59 | "StockSymbol": "BKS", 60 | "ID": "978541942832888" 61 | } 62 | ] 63 | 64 | >>> from googlefinance import getNews 65 | >>> import json 66 | >>> print json.dumps(getNews("GOOG"), indent=2) 67 | [ 68 | { 69 | "usg": "AFQjCNEndnF6ktTO4yZ7DO6VWNNKuNLRqA", 70 | "d": "Feb 26, 2016", 71 | "tt": "1456499673", 72 | "sp": "Alphabet logo Alphabet Inc (NASDAQ:GOOG) CEO Lawrence Page sold 33,332 shares of the firm's stock in a transaction dated Tuesday, March 22nd.", 73 | "s": "Financial Market News", 74 | "u": "http://www.financial-market-news.com/alphabet-inc-goog-ceo-lawrence-page-sells-33332-shares/996645/", 75 | "t": "Alphabet Inc (GOOG) CEO Lawrence Page Sells 33332 Shares", 76 | "sru": "http://news.google.com/news/url?sa=T&ct2=us&fd=S&url=http://www.financial-market-news.com/alphabet-inc-goog-ceo-lawrence-page-sells-33332-shares/996645/&cid=52779068349451&ei=jVn_VsrfApXcuATP4JvQBw&usg=AFQjCNHkHXAJIZRMHo9ciTAb7OWzj9pKvA" 77 | }, 78 | { 79 | "usg": "AFQjCNHfaafHtJPn5GWu-6RiVG_J_1TYUw", 80 | "d": "Mar 26, 2016", 81 | "tt": "1458951075", 82 | "sp": "You don't get to $300 billion without overcoming your fair share of problems. This truism certainly applies individually to tech titans Alphabet (NASDAQ:GOOG) (NASDAQ:GOOGL) and Apple (NASDAQ:AAPL) at different points in their respective corporate ...", 83 | "s": "Motley Fool", 84 | "u": "http://www.fool.com/investing/general/2016/03/25/alphabet-inc-eyes-a-new-road-to-mobile-success-in.aspx", 85 | "t": "Alphabet Inc Eyes a New Road to Mobile Success in the Most Unlikely Places", 86 | "sru": "http://news.google.com/news/url?sa=T&ct2=us&fd=S&url=http://www.fool.com/investing/general/2016/03/25/alphabet-inc-eyes-a-new-road-to-mobile-success-in.aspx&cid=52779068349451&ei=jVn_VsrfApXcuATP4JvQBw&usg=AFQjCNEFa7EPWB-kyl2C23OTRHOWRJN52w" 87 | } 88 | ] 89 | 90 | -------------------------------------------------------------------------------- /dist/googlefinance-0.4.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongtaocai/googlefinance/9f703d8d4e00d645320d49186eee4520341ec273/dist/googlefinance-0.4.tar.gz -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | from googlefinance import getQuotes 2 | from googlefinance import getNews 3 | 4 | import json 5 | print json.dumps(getQuotes('GOOG'), indent=2) 6 | print json.dumps(getNews("GOOG"), indent=2) -------------------------------------------------------------------------------- /googlefinance/__init__.py: -------------------------------------------------------------------------------- 1 | import json 2 | import sys 3 | import demjson 4 | 5 | try: 6 | from urllib.request import Request, urlopen 7 | except ImportError: # python 2 8 | from urllib2 import Request, urlopen 9 | 10 | __author__ = 'Hongtao Cai' 11 | 12 | googleFinanceKeyToFullName = { 13 | u'id' : u'ID', 14 | u't' : u'StockSymbol', 15 | u'e' : u'Index', 16 | u'l' : u'LastTradePrice', 17 | u'l_cur' : u'LastTradeWithCurrency', 18 | u'ltt' : u'LastTradeTime', 19 | u'lt_dts' : u'LastTradeDateTime', 20 | u'lt' : u'LastTradeDateTimeLong', 21 | u'div' : u'Dividend', 22 | u'yld' : u'Yield', 23 | u's' : u'LastTradeSize', 24 | u'c' : u'Change', 25 | u'cp' : u'ChangePercent', 26 | u'el' : u'ExtHrsLastTradePrice', 27 | u'el_cur' : u'ExtHrsLastTradeWithCurrency', 28 | u'elt' : u'ExtHrsLastTradeDateTimeLong', 29 | u'ec' : u'ExtHrsChange', 30 | u'ecp' : u'ExtHrsChangePercent', 31 | u'pcls_fix': u'PreviousClosePrice' 32 | } 33 | 34 | def buildUrl(symbols): 35 | symbol_list = ','.join([symbol for symbol in symbols]) 36 | # a deprecated but still active & correct api 37 | return 'http://finance.google.com/finance/info?client=ig&q=' \ 38 | + symbol_list 39 | 40 | def buildNewsUrl(symbol, qs='&start=0&num=1000'): 41 | return 'http://www.google.com/finance/company_news?output=json&q=' \ 42 | + symbol + qs 43 | 44 | def request(symbols): 45 | url = buildUrl(symbols) 46 | req = Request(url) 47 | resp = urlopen(req) 48 | # remove special symbols such as the pound symbol 49 | content = resp.read().decode('ascii', 'ignore').strip() 50 | content = content[3:] 51 | return content 52 | 53 | def requestNews(symbol): 54 | url = buildNewsUrl(symbol) 55 | print "url: ", url 56 | req = Request(url) 57 | resp = urlopen(req) 58 | content = resp.read() 59 | 60 | content_json = demjson.decode(content) 61 | 62 | #print "total news: ", content_json['total_number_of_news'] 63 | 64 | article_json = [] 65 | news_json = content_json['clusters'] 66 | for cluster in news_json: 67 | for article in cluster: 68 | if article == 'a': 69 | article_json.extend(cluster[article]) 70 | 71 | return article_json 72 | 73 | def replaceKeys(quotes): 74 | global googleFinanceKeyToFullName 75 | quotesWithReadableKey = [] 76 | for q in quotes: 77 | qReadableKey = {} 78 | for k in googleFinanceKeyToFullName: 79 | if k in q: 80 | qReadableKey[googleFinanceKeyToFullName[k]] = q[k] 81 | quotesWithReadableKey.append(qReadableKey) 82 | return quotesWithReadableKey 83 | 84 | def getQuotes(symbols): 85 | ''' 86 | 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 87 | 88 | Unlike python package 'yahoo-finance' (15 min delay), There is no delay for NYSE and NASDAQ stocks in 'googlefinance' package. 89 | 90 | example: 91 | quotes = getQuotes('AAPL') 92 | return: 93 | [{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'129.09', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'129.09', u'Yield': u'1.46', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'0.47', u'StockSymbol': u'AAPL', u'ID': u'22144'}] 94 | 95 | quotes = getQuotes(['AAPL', 'GOOG']) 96 | return: 97 | [{u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'129.09', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'129.09', u'Yield': u'1.46', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'0.47', u'StockSymbol': u'AAPL', u'ID': u'22144'}, {u'Index': u'NASDAQ', u'LastTradeWithCurrency': u'571.34', u'LastTradeDateTime': u'2015-03-02T16:04:29Z', u'LastTradePrice': u'571.34', u'Yield': u'', u'LastTradeTime': u'4:04PM EST', u'LastTradeDateTimeLong': u'Mar 2, 4:04PM EST', u'Dividend': u'', u'StockSymbol': u'GOOG', u'ID': u'304466804484872'}] 98 | 99 | :param symbols: a single symbol or a list of stock symbols 100 | :return: real-time quotes list 101 | ''' 102 | if type(symbols) == type('str'): 103 | symbols = [symbols] 104 | content = json.loads(request(symbols)) 105 | return replaceKeys(content); 106 | 107 | def getNews(symbol): 108 | return requestNews(symbol); 109 | 110 | if __name__ == '__main__': 111 | try: 112 | symbols = sys.argv[1] 113 | except: 114 | symbols = "GOOG,AAPL" 115 | 116 | symbols = symbols.split(',') 117 | 118 | print(json.dumps(getNews("GOOG"), indent=2)) 119 | print(json.dumps(getQuotes(symbols), indent=2)) 120 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | setup( 3 | name = 'googlefinance', 4 | packages = ['googlefinance'], # this must be the same as the name above 5 | version = '0.7', 6 | description = 'Python module to get real-time (no delay) stock data from Google Finance API', 7 | author = 'Hongtao Cai', 8 | author_email = 'caiht.ht@gmail.com', 9 | url = 'https://github.com/hongtaocai/googlefinance', # use the URL to the github repo 10 | download_url = 'https://github.com/hongtaocai/googlefinance/tarball/0.4', # I'll explain this in a second 11 | keywords = ['googlefinance', 'stock', 'price' , 'finance', 'google', 'real-time'], # arbitrary keywords 12 | classifiers = [ 13 | 'License :: OSI Approved :: MIT License', 14 | 'Operating System :: OS Independent', 15 | 'Programming Language :: Python', 16 | 'Programming Language :: Python :: 2', 17 | 'Programming Language :: Python :: 2.6', 18 | 'Programming Language :: Python :: 2.7', 19 | 'Programming Language :: Python :: 3', 20 | 'Programming Language :: Python :: 3.3', 21 | 'Programming Language :: Python :: 3.4', 22 | ], 23 | ) 24 | -------------------------------------------------------------------------------- /tests/test_googlefinance.py: -------------------------------------------------------------------------------- 1 | import googlefinance 2 | import unittest 3 | 4 | class TestQuotes(unittest.TestCase): 5 | 6 | def test_one_symbol(self): 7 | appl = googlefinance.getQuotes('GOOG')[0] 8 | self.assertEqual(appl["Index"], "NASDAQ") 9 | self.assertEqual(appl["StockSymbol"], "GOOG") 10 | 11 | def test_symbols(self): 12 | quotes = googlefinance.getQuotes(['GOOG', 'VIE:BKS']) 13 | self.assertEqual(quotes[0]["Index"], "NASDAQ") 14 | self.assertEqual(quotes[0]["StockSymbol"], "GOOG") 15 | self.assertEqual(quotes[1]["Index"], "VIE") 16 | self.assertEqual(quotes[1]["StockSymbol"], "BKS") 17 | 18 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist=py{26,27,33,34} 3 | 4 | [testenv] 5 | commands= 6 | nosetests 7 | deps= 8 | nose 9 | --------------------------------------------------------------------------------