Hpricot and woot!

January 13th, 2009 by comment Angel N. Sciortino

I know there are several sites for tracking wootoffs on Woot, but I wanted to write my own small program in Ruby. To do this, I used Hpricot by why the lucky stiff.

First, I need my require statements. I’ll need rubygems, since hpricot is a gem, and open-uri to access woot.

require 'rubygems'
require 'hpricot'
require 'open-uri'

Woot has an API that returns XML, so I used Hpricot::XML to parse what comes back.

doc = Hpricot::XML(open("http://www.woot.com/salerss.aspx"))

Now I want to know if it’s a wootoff. If it is, I’ll want to check more frequently. I use the at method to find the element I want, and inner_html to get the text inside that element. The element I’m interested in is woot:wootoff. I’m going to start putting the output into the text variable.

wootoff = doc.at("woot:wootoff").inner_html =~ /true/i
text = wootoff ? "wootoff! ^_^" : "no wootoff v_v"
text << "\n"

Next I want to know what the item is and the price and shipping.

text << doc.at("item > title").inner_html << "\n"
text << doc.at("woot:price").inner_html << " + "
text << doc.at("woot:shipping").inner_html << "\n"

I'll also want to know how much of the item is left, so I know if I need to rush to buy something I want.

percent_gone = doc.at("woot:soldoutpercentage").inner_html
percent_gone = percent_gone.to_f * 100
percent_left = (100 - percent_gone).round
text << "#{percent_left}% Left"

I took all that code and put it into a method, to be looped over every two minutes. I wanted it to print to the screen only if the text changed. This is what the completed program looks like.

#!/usr/bin/ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'

def woot_item
  doc = Hpricot::XML(open("http://www.woot.com/salerss.aspx"))
  wootoff = doc.at("woot:wootoff").inner_html =~ /true/i
  text = wootoff ? "wootoff! ^_^" : "no wootoff v_v"
  text << "\n"
  text << doc.at("item > title").inner_html << "\n"
  text << doc.at("woot:price").inner_html << " + "
  text << doc.at("woot:shipping").inner_html << "\n"
  percent_gone = doc.at("woot:soldoutpercentage").inner_html
  percent_gone = percent_gone.to_f * 100
  percent_left = (100 - percent_gone).round
  text << "#{percent_left}% Left"
end

woot_text = ''
while true
  new_woot_text = woot_item
  if woot_text != new_woot_text
    woot_text = new_woot_text
    puts "*****************************************************"
    puts woot_text
  end
  sleep 120
end

Here is some output from the program.

*****************************************************
no wootoff v_v
Lockjaw Self-Adjusting Locking Pliers - 2 Pack
$9.99 + $5 shipping
100% Left

Of course, this is only a beginning to what you can do. You can have it send a twitter, SMS, email, or any number of things if an item you want is available or a wootoff starts.

Angel

Comments

One Response to “Hpricot and woot!”

  1. Mike Zazaian says:


    Great article. Here’s a related one that uses hpricot and open-uri to auto-populate title and description information for links:

    http://doblock.com/articles/using-hpricot-to-auto-populate-link-information

Got something to say?