Advertisement
Gerst20051

Craigslist Monitor and Text Alert System

Jun 18th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.72 KB | None | 0 0
  1. <?php
  2. require_once 'phpdom.php';
  3.  
  4. function remove_non_numeric($string) {
  5.     return preg_replace('/\D/', '', $string);
  6. }
  7.  
  8. function sendAlert($body){
  9.     $header = array();
  10.     $headers[] = 'Content-Transfer-Encoding: 7bit';
  11.     $headers[] = 'From: ';
  12.     mail('', 'Craigslist', $body, implode("\r\n", $headers));
  13. }
  14.  
  15. function crawl(){
  16.     $ret = array();
  17.     $save = array();
  18.     $cities = array(
  19.         'hickory',
  20.         'charlotte'
  21.     );
  22.     $keywords = array(
  23.         'washer'=>250,
  24.         'dryer'=>250,
  25.         'stove'=>150,
  26.         'range'=>150,
  27.         'oven'=>150,
  28.         'refrigerator'=>250,
  29.         'refridgerator'=>250,
  30.         'fridge'=>250
  31.     );
  32.     $blacklist = array(
  33.         'gas',
  34.         'dishwasher',
  35.         'microwave',
  36.         'toaster'
  37.     );
  38.     $f='save.json';
  39.     $oldsavedata = file_get_contents($f);
  40.     if ($oldsavedata === false) echo "Cannot open file ($f)<br/><br/>";
  41.     $oldsave = json_decode($oldsavedata,true);
  42.     foreach ($cities as $city) {
  43.         $html = file_get_html('http://'.$city.'.craigslist.org/ppa/');
  44.         $ret[$city] = array();
  45.         foreach ($html->find('p.row') as $index=>$row) {
  46.             $valid = true;
  47.             $item = $row->innertext;
  48.             $link = $row->find('a', 0);
  49.             $linktext = $link->plaintext;
  50.             $smalltext = strtolower($linktext);
  51.             $href = $link->href;
  52.             $type = $row->find('a', 1)->plaintext;
  53.             $price = remove_non_numeric($row->find('span.itempp', 0)->plaintext);
  54.             if (0 === $index) $save[$city] = $href;
  55.             if ($href == $oldsave[$city]) break;
  56.             if ($type === 'appliances - by dealer') continue;
  57.             foreach ($blacklist as $black) {
  58.                 if (-1 < strpos($smalltext, $black)) {
  59.                     $valid = false;
  60.                     break;
  61.                 }
  62.             }
  63.             if ($valid !== true) continue;
  64.             foreach ($keywords as $keyword=>$value) {
  65.                 if (-1 < strpos($smalltext, $keyword) && (int)$price < $value) {
  66.                     array_push($ret[$city], '$'.$price.' - '.$linktext);
  67.                     break;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     $save = json_encode($save);
  73.     if ($oldsavedata == $save) die('No new postings.');
  74.     if (is_writable($f)) {
  75.         if (!$handle = fopen($f, 'w')) {
  76.              echo "Cannot open file ($f)";
  77.              exit;
  78.         }
  79.         if (fwrite($handle, $save) === false) {
  80.             echo "Cannot write to file ($f)";
  81.             exit;
  82.         }
  83.         echo "Success, wrote data to file ($f)";
  84.         fclose($handle);
  85.     } else echo "The file $f is not writable";
  86.     return $ret;
  87. }
  88.  
  89. $current_time = date('G');
  90. if ($current_time >= 7 && $current_time <= 24) {
  91.     $data = crawl();
  92.     $body = '';
  93.     $citycount = 1;
  94.     foreach ($data as $city=>$items) {
  95.         if (1 > count($items)) continue;
  96.         if ($citycount > 1) $body .= "\r\n\r\n".strtoupper($city)."\r\n\r\n";
  97.         else $body .= strtoupper($city)."\r\n\r\n";
  98.         foreach ($items as $item) {
  99.             $body .= ucwords(strtolower(htmlspecialchars_decode($item)))."\r\n";
  100.         }
  101.         $citycount++;
  102.     }
  103.     if (0 < strlen($body)) {
  104.         sendAlert($body);
  105.         print_r($data);
  106.     }
  107. }
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement