From 4f13215cefae3bab8cf52ac0a8fd67ecd825383a Mon Sep 17 00:00:00 2001 From: canojim Date: Sat, 2 Apr 2016 13:59:52 +0800 Subject: [PATCH] add method to get google finance news based on ticker --- README.md | 27 +++++++++++++++++++++++++++ example.py | 6 ++++++ googlefinance/__init__.py | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 example.py diff --git a/README.md b/README.md index f675f61..5f7e968 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,30 @@ From development repo (requires git) "ID": "978541942832888" } ] + + >>> from googlefinance import getNews + >>> import json + >>> print json.dumps(getNews("GOOG"), indent=2) + [ + { + "usg": "AFQjCNEndnF6ktTO4yZ7DO6VWNNKuNLRqA", + "d": "Feb 26, 2016", + "tt": "1456499673", + "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.", + "s": "Financial Market News", + "u": "http://www.financial-market-news.com/alphabet-inc-goog-ceo-lawrence-page-sells-33332-shares/996645/", + "t": "Alphabet Inc (GOOG) CEO Lawrence Page Sells 33332 Shares", + "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" + }, + { + "usg": "AFQjCNHfaafHtJPn5GWu-6RiVG_J_1TYUw", + "d": "Mar 26, 2016", + "tt": "1458951075", + "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 ...", + "s": "Motley Fool", + "u": "http://www.fool.com/investing/general/2016/03/25/alphabet-inc-eyes-a-new-road-to-mobile-success-in.aspx", + "t": "Alphabet Inc Eyes a New Road to Mobile Success in the Most Unlikely Places", + "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" + } + ] + diff --git a/example.py b/example.py new file mode 100644 index 0000000..85878b6 --- /dev/null +++ b/example.py @@ -0,0 +1,6 @@ +from googlefinance import getQuotes +from googlefinance import getNews + +import json +print json.dumps(getQuotes('GOOG'), indent=2) +print json.dumps(getNews("GOOG"), indent=2) \ No newline at end of file diff --git a/googlefinance/__init__.py b/googlefinance/__init__.py index 0dd8c65..f758964 100644 --- a/googlefinance/__init__.py +++ b/googlefinance/__init__.py @@ -1,5 +1,6 @@ import json import sys +import demjson try: from urllib.request import Request, urlopen @@ -36,6 +37,10 @@ def buildUrl(symbols): return 'http://finance.google.com/finance/info?client=ig&q=' \ + symbol_list +def buildNewsUrl(symbol, qs=''): #&start=0&num=100 + return 'http://www.google.com/finance/company_news?output=json&q=' \ + + symbol + qs + def request(symbols): url = buildUrl(symbols) req = Request(url) @@ -45,6 +50,26 @@ 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) + + #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 = [] @@ -79,6 +104,9 @@ 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] @@ -87,7 +115,5 @@ if __name__ == '__main__': symbols = symbols.split(',') - try: - print(json.dumps(getQuotes(symbols), indent=2)) - except: - print("Fail") + print(json.dumps(getNews("GOOG"), indent=2)) + print(json.dumps(getQuotes(symbols), indent=2))