Google News library for Ruby

I was surprised that Google News has no API, so I built my own: Using HTTParty, I wrote a small Ruby library for Google News. Put this in a ‘googlenews.rb’ file in the /lib/ directory of your Rails application to grab google news stories and topic lists.

Haven’t figured out how to get more than 10 items… Safari seems to be able to with feed://… but I’m not sure how.

gem "httparty"
require "httparty"

class Googlenews
  include HTTParty
  base_uri ''
  def self.items
    options = { :query => { :output => 'rss' } }
    features = self.get('/news', options)
  def self.extract_topic(item)
    # ncl=duPfj30A5WoxBHMupoOtlciO1jY1M&

  def self.topic(item)
    options = { :query => { :output => 'rss', :ncl => self.extract_topic(item) } }
    features = self.get('/news/more', options)

  # store in database (unused)
  # def self.story(item)
    # item['category']
    # item['title']
    # item['guid']
    # item['description']
    # item['link']
    # item['pubDate']

    # t.string :category, :default => ''
    # t.string :title, :default => ''
    # t.string :guid, :default => ''
    # t.text :description, :default => ''
    # t.string :link, :default => ''
  # end


Once you’ve dropped this in /lib/, you can use it in a Rails controller like this (I needed JSON, to work with the feed in JavaScript):

class DataController < ApplicationController

  def latest
    render :json => Googlenews.items


Update: to get more than 10 items, add a GET parameter ‘num=20’ to the feed URL. This works both for topic and for the main feed. I haven’t confirmed this but I think it’s still limited to 30 items. Again, Safari manages to get more, but perhaps it does repeated queries. Still investigating. Anyways, that makes it:

 def self.items
    options = { :query => { :output => 'rss', :num => '20' } }
    features = self.get('/news', options)

Update: This now has a real home on Github, where it’s called ruby-googlenews.

2 thoughts on “Google News library for Ruby”

  1. I have searched how to use google news with Ruby. This materail gave me some help. Thank you for your job. However, I want to search more material how to use google news interoperability with Ruby. Do you have other information for this? If you have, would you give me some advice?
    Thank you.

Comments are closed.