require 'open-uri'
require 'hpricot'

SearchResult = Struct.new(:url, :title, :content)

class SearchYahoo
  @@urlinfo = [nil, 'ca.search.yahoo.com', 80, '/search', nil, nil]

  def initialize(term)
    @term = term
    @page = 1
    @results = nil
    @url = URI::HTTP.build(@@urlinfo)
  end

  def next_result
    if not @results
      @results = []
      fetch_results
    elsif @results.empty?
      next_page
    end
    @results.shift
  end

  def fetch_results
    @url.query = URI.escape( % [@term, @page])
    doc = open(@url) { |f| Hpricot(f) }  
    parse_html(doc)
  end

  def next_page
    @page += 10
    fetch_results
  end

  def parse_html(doc)
    doc.search().search().each do |div|
      next unless div.has_attribute?() and div.get_attribute().index() == 0
      result = SearchResult.new
      div.search().each do |link|
        next unless link.has_attribute?() and link.get_attribute() == 
        result.url = link.get_attribute()
        result.title = link.inner_text
      end
      div.search().each do |abstract|
        next unless abstract.has_attribute?() and abstract.get_attribute().index()
        result.content = abstract.inner_text
      end
      @results << result
    end
  end
end

s = SearchYahoo.new()
15.times do |i|
  result = s.next_result
  puts i+1
  puts result.title
  puts result.url
  puts result.content
  puts
end