11 October 2008
Notes on Google Finance API
I recently started dabbling in shares (yeah, I know, I like swimming against the flow), and so I looked around for good stock-price trackers for KDE3. To my surprise, I couldn't find anything apart from the usual KMyMoney (which I like and use, but it's not really something you can keep open all day on your desktop). I had already put my stock info on Google Finance, and lo, the service has a GData API, so I decided to write a small script in Python to retrieve my portfolio and the daily variations.
The good thing about GData APIs is that they are all the same at a basic level, so even when client libraries don't explicitly expose new features (like in this case, as Finance is quite a new service), you can still use the APIs to get easy authentication and feed-parsing. So I went and downloaded the official (and excellent) gdata-python-client package, which is dead-easy to use:
from gdata.service import GDataService client = GDataService() client.email = 'your_email@gmail.com' client.password = 'your_password' client.service = 'finance' client.ProgrammaticLogin() baseURL = "http://finance.google.com/finance/feeds/%(email)s/" % {'email':client.email} # to get all data in one go: # bigFeed = client.GetFeed(baseURL + "portfolios?positions=true&returns=true") # and then positionsFeed is included in each Entry # positionsFeed = gdata.GDataFeedFromString(bigfeed.entry[0].FindExtensions('feedLink')[0].children[0].ToString()) # ... but I really just want the first portfolio positionsFeed = client.GetFeed(baseURL + "portfolios/1/positions?returns=true") for entry in positionsFeed.entry: details = entry.FindExtensions('symbol')[0] symbol = details.attributes['symbol'] name = details.attributes['fullName'] data = entry.FindExtensions('positionData')[0] totalReturn = round(float(data.attributes['returnOverall']) * 100,2) gainPerc = round(float(data.attributes['gainPercentage']) * 100,2) print name + " (" + symbol + ") Return: " + str(totalReturn) + "% - Gain: " + str(gainPerc) + "%"
One should really make an applet or something for KDE, but I'm leaving for Japan in about six hours so I can't be bothered...
Labels: foreverythingelsetherespython, GeekDiary, Google, GoogleFinance