During the recovery from my previously mentioned osteotomy, I’ve been spending lots of time chatting with my physical therapist Larry Meyer. He’s an interesting and personable guy (and is really handling my PT well). In my dozens of appointments, we’ve talked about lots of stuff, including Lijit, Christmas presents, and something near and very dear to me, bargain hunting.
Along the way, I mentioned Steep & Cheap, which is basically like the woot of outdoor gear. They have single deal after single deal, about every 20 minutes or so, all day long. It’s fine for me because I work at a computer all day long and can see each deal as soon as it is posted. But Larry is not at a computer, though he does have access to one.
What Larry does have, though, is a pager with an email address. It got me to thinking, so I hacked together a little PHP code, mixed it gently with cygwin cron, to poll the SAC deal feed. If the deal changes, the application will send him a page.
So far, he’s not tired of the constant buzzing on his hip.
Here it is. Standard disclaimer applies: this is quick n’ dirty(tm):
<?
// Requires PEAR::Mail
require_once( 'Mail.php' );
$sac_rss = "http://www.steepandcheap.com/steepcheap/rss.xml";
$item_element = "ITEM";
$have_item = FALSE;
$in_item = FALSE;
$current_element = "";
$message = "";
$statusFile = "sacdeal.txt";
$to_list = array( "pager_email_address" );
$from = 'SAC Alert <sacalert@my.hosting.company>';
$subject = 'SAC deal alert';
$host = 'smtp.server.name';
$username = 'smtp.userid';
$password = 'smtp.password';
$to = "";
foreach( $to_list as $emailaddy )
{
$to .= "$emailaddy, ";
}
echo "$to\n";
function startElement( $parser, $name, $attrs )
{
global $current_element, $have_item, $in_item, $item_element;
$current_element = $name;
if ( $name == $item_element )
$in_item = TRUE;
}
function endElement( $parser, $name )
{
global $current_element, $have_item, $in_item, $item_element;
$current_element = "";
if ( $name == $item_element )
{
$in_item = FALSE;
$have_item = TRUE;
}
}
function elementData( $parser, $text )
{
global $current_element, $have_item, $in_item, $message;
if ( $in_item && !$have_item )
{
// the message content is only the title or the price
if ( $current_element == "TITLE" || $current_element == "SAC:PRICE" )
$message .= "$text\n";
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler( $xml_parser, "startElement", "endElement" );
xml_set_character_data_handler( $xml_parser, "elementData" );
// fetch the feed content and parse it
$xml = file_get_contents( $sac_rss );
xml_parse( $xml_parser, $xml );
xml_parser_free($xml_parser);
// compare the current item in the feed to the last one we sent email about
$oldcontents = file_get_contents( $statusFile );
if ( $oldcontents != $message )
{
$headers = array( 'From' => $from,
'To' => $to,
'Subject' => $subject );
$smtpParams = array( 'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password );
$smtp = Mail::factory( 'smtp', $smtpParams );
$mail = $smtp->send( $to, $headers, $message );
echo $message;
if (PEAR::isError($mail))
{
echo( $mail->getMessage() . "\n" );
}
else
{
echo( "Message successfully sent!\n" );
file_put_contents( $statusFile, $message );
}
}
else
{
echo "same deal as last time\n";
}
?>