#!/usr/bin/perl -w
use v5.10;
use WWW::Mechanize;


$ua='Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0';
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'}=0; # bug workaround


$www=WWW::Mechanize->new(agent=>$ua, stack_depth=>2, autocheck=>0);


# get list of all rackmount servers located in Canada, ordered by ends soonest
$www->get('http://www.ebay.ca/sch/Servers-/11211/i.html?_dcat=11211&'
	 .'Form%2520Factor=Rackmount&_sop=1&rt=nc&LH_PrefLoc=1');
&DoPage;


# "click" on next page button
$www->follow_link(text_regex=>qr/next page of results/i);
&DoPage;



sub DoPage {

  die $www->status if $www->status!=200;

  $_=$www->content;

  $regex=qr[
    <table \s listingId="(\d+)"
    .*?
    class="ittl" .*? <a \s href=[^>]+> \s* ([^<]+) \s*
    .*?
    class="g-b" .*? \$.{0,10}?([0-9,.]+)
  ]six;


  while (/$regex/g) {

    ($id,$title,$price)=($1,$2,$3);
    $price=~tr/,//d;

    next if $title!~/\bdell\b/i;



    printf "$id %8.2f %s\n",$price,substr($title,0,50);

  }

}
