Source for file check_for_guitars.php
Documentation is available at check_for_guitars.php
* Searches the stupid deal of the day for passed deals and sends an email if found.
* @author Eric Lamb <eric@ericlamb.net>
* @link http://blog.ericlamb.net/
* @copyright 2008 Eric Lamb
* @example C:\ProjectFiles\php_cli>php check_for_guitars.php --search="guitar,method" --email="eric@ericlamb.net"
include 'includes/classes/snoopy/Snoopy.class.php';
include 'includes/classes/mailer.class.php';
* URI to the Stupid Deal page
* @global string $uri_to_check
$uri_to_check = 'http://www.musiciansfriend.com/stupid';
* Template for email message
* @global string $uri_to_check
Match found for <a href=" $uri_to_check">%%search%%</a><br>
Sale Price: %%sale_price%%<br>
Original Price: %%og_price%%<br>
* Flag to set search validation
* @global bool $validate_search
$validate_search = FALSE;
if(strlen($input['search']) <= 2){
echo "Please enter what to search for:\n";
$input['search'] = trim(fgets(STDIN)); // reads one line from STDIN
if(strlen($input['search']) <= 2){//it's a valid string
echo "Please enter a something to search for ";
echo "(at least 2 characters:\n";
echo "Example: \"guitar,bass,dvd\"\n";
* Flag to set email validation
* @global bool $validate_email
echo "Please enter an email to send the alert to:\n";
$input['email'] = trim(fgets(STDIN)); // reads one line from STDIN
echo "Please enter a valid email address:\n";
$snoopy->agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
$snoopy->referer = "http://www.yahoo.com/";
$snoopy->fetch($uri_to_check);
if($results = $snoopy->results){
//for now the page title is the only use of the <h1> tag so this is easy :)
$pattern = "'<[^>]*h1[^>]*>(.*?)<[^>]*/h1[^>]*>'";
$page_title = $match['1'];
//$page_title = $page_title['0'];
$input['search'] = explode(',',$input['search']);
//check if there's a match in the passed $input['search'] array
$total = count($input['search']);
for($i= 0;$i< $total;$i++ ){
if(stristr($page_title, trim($input['search'][$i])) !== FALSE) {
$match_for[] = trim($input['search'][$i]);
//send email if successful
$total = count($match_for);
//check if the search was done today...
$sql = "SELECT * FROM mf_checks WHERE title = '". $DB->es($page_title). "' AND DATE_FORMAT(`date_checked`,'%m') = '". date('m'). "' AND DATE_FORMAT(`date_checked`,'%d') = '". date('d'). "' AND DATE_FORMAT(`date_checked`,'%Y') = '". date('Y'). "' LIMIT 1";
if($DB->getNumRows() == '1'){ //alert has already been sent so break out...
echo "Already sent today... exiting...";
//match was found so get the price now
$price_arr = explode('<div style="font-size:3em;color:#FF0000;font-weight:normal;padding:20px 0;">',$results);
$price_arr = explode("\n",$price_arr['1']);
$htmlmessage = str_replace(array('%%search%%','%%title%%','%%sale_price%%','%%og_price%%'),array('"'. implode(', ',$match_for). '"',$page_title,$sale_price,$og_price),$htmlmessage);
$mail->From = $input['email'];
$mail->FromName = $input['email'];
$mail->Subject = 'Found: '. $page_title;
$mail->MsgHTML($htmlmessage);
$mail->AddAddress($input['email']);
$sql = "INSERT INTO mf_checks SET term = '". $DB->es(implode(', ',$match_for)). "', title = '". $DB->es($page_title). "', sale_price = '". $DB->es($sale_price). "', og_price = '". $DB->es($og_price). "', date_checked = now(), alert_sent = '1'";
|