Woot Watcher Notification Script

From Bubba.org

Jump to: navigation, search


Woot Watcher Notification Script

#!/usr/bin/perl
#
# WootWatcher v1.0 
# 5/17/2009 - bubbaATbubba.org - made some changes for woot's new site. 
#
# 7/16/2005 - bubbaATbubba.org
#
# Monitor items on Woot.com and for Woot-Off's
# 
# For Woot-Offs:  $daemon_mode=1, $sleep=60*30, $notify_status_updates=1, $notify_first_time=0
# For Regular Daily Woot notification: $daemon_mode=0 (run from cron right after midnight CST)
#  
 
use LWP::UserAgent;
use Mail::Send;
use Time::localtime;
 
# regex to trigger an email to $email.  
$notify_regex = "(?:AGP.*DVI)|(?:DVI.*AGP)|iRobot|Upconversion";           
 
# email to send matched new items/status updates to (like your phone or pager).
$email = "myphonenumber\@att.com";     
 
# email to send ALL new items/status update to (set to "" for none).
$all_email = "annoyme\@blah.com";                     
 
# if your http connection requires a proxy, set it here
$proxy = "";                         
 
# 1 if you would like to know when an item sells out (requires you to run as a
# daemon or via a set schedule via cron).
$notify_status_updates=0;
 
# still email even if the item is sold out.
$notify_if_sold_out=0;
 
# notify on the first run (1) or wait till the next new item (0).
# must be 1 if not running in daemon_mode (0).
$notify_first_time=1;
 
# notify % left for wootoff's (can be annoying..)
$notify_progress=0;
 
# if 1, will run every $sleep seconds.  Helpful for woot-off's.  If 0, then 
# notify_first_time has to be 1.
$daemon_mode = 0;
 
# time in seconds to check in daemon mode.  Please do not make this value too 
# small for the sake of woot's servers.
$sleep = 60*2;
 
# print information to the screen (may want to set to 0 if you're running from 
# cron).
$print_output=0;
 
while(1) {
if ($daemon_mode == 0) {
    $notify_first_time=1;
}
if ($woot_ref = &getwoot) {
	$checks++;
	# load the last known woot we retrieved
	if ($pwoot_ref) {
	  foreach $item (keys %$pwoot_ref) {
			$p_item = $item;
		}
	}
 
	# check our current woot
	foreach $item ( keys %$woot_ref) {
		$time = $woot_ref->{$item}{'time'}; 
		if ($item eq $p_item) {
			if ($woot_ref->{$item}{'status'} eq $pwoot_ref->{$item}{'status'}) {
			   if ($print_output) {
			     print "[$time] Same item ($item), no status change\n";
			   }
			$prog =$woot_ref->{$item}{'progress'};
			$prev_prog=$pwoot_ref->{$item}{'progress'};
			if ($prog) {
				if ($print_output) {
					print "[$time] $prog% left of $item (previous $prev_prog)\n";
				}
 
				if ($notify_progress && ($prog < $prev_prog) && ($prog <= 10)) {
                    			&mail($woot_ref,"$prog% left of $item",$all_email,"progress");
					if ($print_output) {
						  print "Notifying All $all_email of progress\n";
					}
				}
			}
			} else {
			    if ($print_output) {
				  print "[$time] $item is no longer available\n";
				}
				if ($notify_status_updates) {
					if ($item =~ /$notify_regex/) {
						&mail($woot_ref,"$item is no longer available",
                                              $email);
                    }
                    &mail($woot_ref,"$item is no longer available",$all_email);
                }
			}
			$pwoot_ref = $woot_ref;
			last;
		} else { 
			if ($checks == 1 && !$notify_first_time) {
				$pwoot_ref = $woot_ref;
			} else {
				if ($print_output) {
				print "[$woot_ref->{$item}{'time'}] New Item: ".
					   "$item $woot_ref->{$item}{'price'} ".
					   "$woot_ref->{$item}{'condition'} ".
					   "$woot_ref->{$item}{'status'}\n";
				}
				if ($item =~ /$notify_regex/i) {
				   if (($woot_ref->{$item}{'status'} =~ /Sold Out/ && 
				      $notify_if_sold_out) || ($woot_ref->{$item}{'status'} !~
				      /Sold Out/)) {
				        if ($all_email) {
						&mail($woot_ref,"New Woot.com Item",$all_email);
				   	}
					if ($email) { 
						&mail($woot_ref,"New Woot Item",$email);
						if ($print_output) {
						  print "Notifying $email because of match\n";
						  print "Notifying All $all_email\n";
					        }
					}
				  }
				} else {
				   if (($woot_ref->{$item}{'status'} =~ /Sold Out/ && 
				      $notify_if_sold_out) || ($woot_ref->{$item}{'status'} !~ 
				      /Sold Out/)) {
				        if ($all_email) {
						&mail($woot_ref,"New Woot.com Item",$all_email);
						if ($print_output) {
						   print "Notifying All $all_email\n";
					    }
					}
				   }
				}
				$pwoot_ref = $woot_ref;
			}
		}
	}
 }
 
 if ($daemon_mode) {
	sleep($sleep);
 } else {
	exit;
 }
}
 
sub getwoot {
	my @data;
	my $woot_ref;
	my $ua = LWP::UserAgent->new;
	$ua->agent('Mozilla/4.0 (compatible; WootWatcher 1.0; Windows 3.1)');
	if ($proxy) { 
		$ua->proxy(['http'],$proxy);
	}
	my $req = HTTP::Request->new(GET => "http://www.woot.com");
	$req->header('Referrer' => 'http://www.woot.com');
	my $res = $ua->request($req);
	if ($res->is_success) {
	  $data = $res->content;	
          if ($data =~ m{<h2>([^<]*)</h2>[^<]*<h3>([^<]*)</h3>}) {
                $item = $1;
                $woot_ref->{"$item"}{'price'}=$2;
          }
          if ($data =~ m{id="shippingOptions">[^<]*<li>\+\s+([^<]*)\sshipping</li>}) {
                $woot_ref->{"$item"}{'shipping'}=$1;
          }
          if ($data =~ m{<dt>Condition:</dt>[^<]*<dd>([^<]*)</dd>}) {
                $woot_ref->{"$item"}{'condition'}=$1;
          }
 
          if ($data =~ m{<h5><a id="[^>]*>([^<]*)}) {
                if ($1 =~ "I Want One!") {
                        $woot_ref->{"$item"}{'status'}="Available";
                } else {
                        $woot_ref->{"$item"}{'status'}="Sold Out";
                }
          }
 
          if ($data =~ m{class="wootOffProgressBarValue" style="width:([^<]*)\;">}) {
                $woot_ref->{"$item"}{'progress'}=$1;
                $woot_ref->{"$item"}{'progress'} =~ s/\%//g;
          }
 
          #if ($data =~ m{class="boughtPerHour"
          $woot_ref->{"$item"}{'time'} = scalar(ctime());
 
        }
 
        if (!$woot_ref->{"$item"}{'status'}) {
                $woot_ref->{"$item"}{'status'}="Sold Out";
        }
	return $woot_ref unless !$woot_ref;
	return 0;
 
}
 
sub mail {
	my $woot_mref= shift;
	my $subject = shift;
	my $email = shift;
	my $type = shift;
	my $message;
	my $woot;
 
	if ($type ne "progress") {
	 foreach $woot (keys %$woot_mref) {
	  $message = $message . "New " .
	  	#. "\nThere is new WOOT item available as of ".
	       #"$woot_mref->{$woot}{'time'}:\n".
		   "Item: $woot\n" .
		   "Price: $woot_mref->{$woot}{'price'}\n".
		   "Shipping: $woot_mref->{$woot}{'shipping'}\n".
		   "Condition: $woot_mref->{$woot}{'condition'}\n".
		   "Availability: $woot_mref->{$woot}{'status'}\n\n";
	 }	
	} else {
	  $message = $subject;
	}
	#print "DEBUG: $email, $subject, $type\n";
	$msg = new Mail::Send;
	$msg->to($email);
	$msg->subject($subject);
	$msg->add("From","$email");
	$fh = $msg->open('sendmail');
	print $fh $message;
	$fh->close;
}