Fork me on GitHub

Wednesday, August 27, 2008

Building a Google Reader API in Ruby


I was getting a little bored with the usual work and was trying to pep up my work by doing some hacking. I always wanted a API for Google Reader so that I can integrate with something or may be build a console app to read the posts :). So I started my journey into the mysterious land of APIs and service interfaces.

I found that there was no API published by Google for reader. So I thought may be I can put a prototype to read my subscriptions from the Google reader and print them. I used Mechanize for this and following is the code I wrote for this.

require "rubygems"
require "mechanize"



base_url = "http://www.google.com"
login_url ="https://www.google.com/accounts/Login?continue=http://www.google.com/&hl=en"
login_done_url = "https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fwww.google.com%2F&hl=en&chtml=LoginDoneHtml"
reader_subscriptions_url = "http://www.google.com/reader/settings?display=edit-subscriptions"



agent = WWW::Mechanize.new
base_page = agent.get base_url
login_page = agent.click(base_page.links.text(/Reader/))

login_form = login_page.forms.first
login_form.Email = "your google id"
login_form.Passwd = "your google password"

begin
agent.submit(login_form)
rescue WWW::Mechanize::ResponseCodeError
end

agent.get login_done_url

subscriptions_page = agent.get reader_subscriptions_url

subscription_names = []
feed_urls = []
names = subscriptions_page.search("//div[@class='subscription-title']")

names.each {|name| subscription_names << name.innerText}

urls = subscriptions_page.search("//input[@class='chkbox']")
urls.each{|feed| feed_urls << feed['value'].sub!('feed/','') unless feed['value'].grep(/feed/).empty?}

subscription_names.each_with_index{|feed_name,index| puts "#{feed_name} --- #{feed_urls[index]}"}


This code will show subscription names and the url of each subscription. I was planning to add the labels into it but it needs more work. As well there is some problem in the login, it logs in but it fails with a HTTP 501 exception and so the code is surrounded with exception handling (Need to figure that out). May be in future when I find time I will try to write this as a complete Google Reader API.

DISCLAIMER - THIS IS NOT OFFICIALLY ASSOCIATED WITH GOOGLE. THE CODE MAY NOT WORK IF THE GOOGLE READER INTERFACE CHANGES (which I seriously hope will not).

No comments: