Wii Watching

Geek | Open Source
So I was trying to figure out how to watch the web for available Wii's and I came up with the idea of writing a little script to do it for me. My solution was to write the script in Ruby. Here's what it does:
  • Parse the Amazon Wii item page for an out of stock message
  • Send email when it fails to find any such message
  • Creates a file when the item is confirmed to exist. The script checks for the file. If the file exists, the script exits immediately. This prevents us from getting many extra emails from step two.
Here's the code (edited for privacy purposes):
require 'open-uri'
require 'net/smtp'

$stop_file="STOP"
exit if FileTest.exist?($stop_file)

def get_page
  result=nil open('http://url.com') do |wii|
    wii.each_line do |line|
      result=line if line['We are currently out of stock:']
    end
  end
  result
end

def send_msg(message)
  Net::SMTP.start('mailserver.com', 25, nil, 'user@domain', 'password', :login) do |smtp|
    smtp.open_message_stream('from@domain', ['to@domain', 'to@domain', 'to@domain']) do |f|
      f.puts
      f.puts message
    end
  end
end

def write_stop_file
  afile=File.new($stop_file, "w+")
  afile.write("Stop processing.")
  afile.close
end

instock=get_page

if instock.nil?
  send_msg("Amazon has the Wii in stock. Order now!")
  write_stop_file
end


I realize that it's ugly, but it's getting the job done (as far as I can tell, anyway). Possible enhancements include:

  • Scrapes of more websites than just Amazon
  • Variable search string to make it more generically useful
  • More robust error checking/handling - or any at all

Any Ruby programmers out there are welcome and encouraged to make suggestions. It's a one-trick pony at the moment. I don't know that it needs to do more. It was a good learning exercise (my first Ruby script), and it should give me notice when Amazon has a Wii available for me to buy.