StupidGrep
[ class tree: StupidGrep ] [ index: StupidGrep ] [ all elements ]

Source for file check_for_guitars.php

Documentation is available at check_for_guitars.php

  1. <?php
  2. /**
  3.  * Stupid Deal Grep
  4.  * 
  5.  * Searches the stupid deal of the day for passed deals and sends an email if found.
  6.  *
  7.  * @author Eric Lamb <eric@ericlamb.net>
  8.  * @version .01
  9.  * @link http://blog.ericlamb.net/
  10.  * @copyright 2008 Eric Lamb
  11.  * @package    StupidGrep
  12.  * @filesource
  13.  * @example C:\ProjectFiles\php_cli>php check_for_guitars.php --search="guitar,method" --email="eric@ericlamb.net"
  14.  */
  15.  
  16. /**
  17.  * Include init script
  18.  */
  19. include 'inc.php';
  20.  
  21. /**
  22.  * Include snoopy library
  23.  */
  24. include 'includes/classes/snoopy/Snoopy.class.php';
  25.  
  26. /**
  27.  * Include e-mail library
  28.  */
  29. include 'includes/classes/mailer.class.php';
  30.  
  31. /**
  32.  * URI to the Stupid Deal page
  33.  * @global string    $uri_to_check 
  34.  */
  35. $uri_to_check 'http://www.musiciansfriend.com/stupid';
  36.  
  37. /**
  38.  * Template for email message
  39.  * @global string    $uri_to_check 
  40.  */
  41. $htmlmessage = <<<HTML
  42. Match found for <a href="$uri_to_check">%%search%%</a><br>
  43. Title: %%title%% <br>
  44. Sale Price: %%sale_price%%<br>
  45. Original Price: %%og_price%%<br>
  46. HTML;
  47.  
  48. /**
  49.  * Flag to set search validation
  50.  * @global bool    $validate_search 
  51.  */
  52. $validate_search FALSE;
  53. if(!array_key_exists('search',$input)){
  54.     $validate_search TRUE;
  55. else {
  56.     if(strlen($input['search']<= 2){
  57.         $validate_search TRUE;
  58.     }
  59. }
  60.  
  61. if($validate_search){
  62.     echo "Please enter what to search for:\n"
  63.     while(1){
  64.         
  65.         $input['search'trim(fgets(STDIN))// reads one line from STDIN
  66.         if(strlen($input['search']<= 2){//it's a valid string
  67.             break;
  68.         }
  69.         echo "Please enter a something to search for ";
  70.         echo "(at least 2 characters:\n";
  71.         echo "Example: \"guitar,bass,dvd\"\n";
  72.     }
  73. }
  74.  
  75. /**
  76.  * Flag to set email validation
  77.  * @global bool    $validate_email 
  78.  */
  79. $validate_email FALSE;
  80. if(!array_key_exists('email',$input)){
  81.     $validate_email TRUE;
  82. else {
  83.     if(!checkEmail_basic($input['email'])){
  84.         $validate_email TRUE;
  85.     }
  86. }
  87.  
  88. if($validate_email){
  89.     echo "Please enter an email to send the alert to:\n"
  90.     while(1){
  91.         
  92.         $input['email'trim(fgets(STDIN))// reads one line from STDIN
  93.         if(checkEmail_basic($input['email'])){//it's a valid email
  94.             break;
  95.         }
  96.         echo "Please enter a valid email address:\n";
  97.     }
  98. }
  99.  
  100.  
  101. //grab the webpage
  102. $snoopy new Snoopy;
  103. $snoopy->agent "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
  104. $snoopy->referer "http://www.yahoo.com/";
  105. $snoopy->fetch($uri_to_check);
  106. if($results $snoopy->results){
  107.  
  108.     //for now the page title is the only use of the <h1> tag so this is easy :)
  109.     $pattern "'<[^>]*h1[^>]*>(.*?)<[^>]*/h1[^>]*>'";
  110.     preg_match($pattern$results$match);
  111.     $page_title $match['1'];
  112.  
  113.     //$page_title = $page_title['0'];
  114.  
  115.     $input['search'explode(',',$input['search']);
  116.  
  117.     //check if there's a match in the passed $input['search'] array
  118.     $total count($input['search']);
  119.     $match_for array();
  120.     for($i=0;$i<$total;$i++){
  121.         if(stristr($page_titletrim($input['search'][$i])) !== FALSE{
  122.             $match_for[trim($input['search'][$i]);
  123.         
  124.     }
  125.  
  126.     //send email if successful
  127.     $total count($match_for);
  128.     if($total >= 1){
  129.  
  130.         //check if the search was done today...
  131.         $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";
  132.         $DB->query($sql);
  133.         if($DB->getNumRows(== '1')//alert has already been sent so break out...
  134.             echo "Already sent today... exiting...";
  135.             exit;
  136.         }
  137.  
  138.         //match was found so get the price now
  139.         $price_arr explode('<div style="font-size:3em;color:#FF0000;font-weight:normal;padding:20px 0;">',$results);
  140.         $price_arr explode("\n",$price_arr['1']);
  141.         $sale_price strip_tags($price_arr['0']);
  142.         $og_price str_replace('Reg ','',strip_tags($price_arr['1']));
  143.  
  144.         $htmlmessage str_replace(array('%%search%%','%%title%%','%%sale_price%%','%%og_price%%'),array('"'.implode(', ',$match_for).'"',$page_title,$sale_price,$og_price),$htmlmessage);
  145.         
  146.         $mail new Mailer();
  147.         $mail->From $input['email'];
  148.         $mail->FromName $input['email'];
  149.         $mail->Subject 'Found: '.$page_title;
  150.         $mail->AltBody strip_tags($htmlmessage);
  151.         $mail->MsgHTML($htmlmessage);
  152.         $mail->AddAddress($input['email']);
  153.         if($mail->Send()){
  154.             echo "Mail Sent";
  155.         else {
  156.             echo "Mail Not Sent";
  157.         }
  158.  
  159.         //add to the db 
  160.         $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'";
  161.         $DB->query($sql);
  162.     }
  163.  
  164. }
  165. ?>

Documentation generated on Thu, 01 Jan 2009 14:04:14 -0800 by phpDocumentor 1.4.2