Merge pull request #11 from canojim/finance-news

add method to get google finance news based on ticker
This commit is contained in:
Hongtao Cai 2016-04-20 22:57:12 -04:00
commit d5a6aa096c
3 changed files with 63 additions and 4 deletions

View File

@ -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"
}
]

6
example.py Normal file
View File

@ -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)

View File

@ -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=1000'):
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))