Jump to content

webstar

Members
  • Posts

    26
  • Joined

  • Last visited

Everything posted by webstar

  1. [code]<?php // Declare variables // $debug = false; $url = "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm"; //$url = "./roadclosed1/rdclosure.htm"; #$email_recip = "****"; $email_recip = "*****"; $email_subject = "Closed Road Update" . date("    (G:i:s)"); $email_from = "WxServer Roads"; $email_from_addr = "*****"; $message_header = "A new/updated road closure has been posted.  Below is a summary,\n====START====\n"; $message_footer = "\n"; $eventfilename = "roadclosed.txt"; // DON'T EDIT PAST HERE UNLESS YOU KNOW WHAT YOU'RE DOING // // MAIN PROGRAM /// $webpage = read_web($url); $content = find_data($webpage, "<DIV CLASS=\"content\">", "</DIV>");  // All content is in the <DIV></DIV> $content = find_data($content, "<TABLE", "</TABLE>");    // Look for the events HTML table $curr_events = read_html_table($content,true);      // Parse the events HTML table $old_content = read_events_file();            // Read the old events HTML table if ($old_content) {   $old_events = read_html_table($old_content, true);    // Parse the old events HTML table } else {   unset($old_events); } if ($debug) { print("Number of current events = " . count($curr_events) . "\n"); } if ($debug) { print("Number of old events = " . count($old_events) . "\n"); } //Go through the current events and look for a corrisponding old event $send_email = false;    // Flag used to see if an email should be sent for new closure events $event_message = $message_header; for ($curr_event_num = 0; $curr_event_num < count($curr_events); $curr_event_num++) {   $curr_event_found = false;   for ($old_event_num = 0; $old_event_num < count($old_events); $old_event_num++) {     if ($debug) { print("Comparing (" . $curr_events[$curr_event_num][0] . "?" . $old_events[$old_event_num][0] . ") & (" . $curr_events[$curr_event_num][1] . "?" . $old_events[$old_event_num][1] . ") & (" . $curr_events[$curr_event_num][2] . "?" . $old_events[$old_event_num][2] . ")\n"); }     if (($curr_events[$curr_event_num][0] == $old_events[$old_event_num][0]) && ($curr_events[$curr_event_num][1] == $old_events[$old_event_num][1]) && ($curr_events[$curr_event_num][2] == $old_events[$old_event_num][2]) ) {       // Found a matching event       $curr_event_found = true;     }   }   if (!$curr_event_found) {    // Add the not found current event to the message     $send_email = true;     if ($debug) { print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n=====END=====\n"); } //    $event_message .= ">>> NEW <<<\n";     $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n";   } else { //    if ($debug) { print ("Found a previously sent event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n"); } //    $event_message .= "Region: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n====END====\n";   } } $event_message .= $message_footer; //Send an email if there is a new event if ($send_email) {   print ("Sending the message below to " . $email_recip . ", from \"" . $email_from . "\" <" .$email_from_addr . ">.\n" . $event_message . "\n");   mail ($email_recip, $email_subject, $event_message, "From: \"" . $email_from . "\" <" .$email_from_addr . ">");   print ("Found a new event:\nRegion: " . $curr_events[$curr_event_num][1] . "\nHighway(s): " . $curr_events[$curr_event_num][0] . "\nReason: " . $curr_events[$curr_event_num][2] . "\n==========\n"); } //Save the events to check the next time the script is run write_events_file($content); // Functions // //read_web - Read the web page //          $strURL ==> URL of the webpage function read_web($strURL) {   global $debug;   $buffer = "";   if($debug){ print("Reading \"$strURL\".\n"); }   $fh = fopen($strURL, "rb");   if ($fh === false) {     return "";   }   while (!feof($fh)) {     $buffer .= fread($fh, 1024);   }   fclose($fh);   return $buffer; } // end function read_web // find)data - Gets a substring of the webpage by scraping the data //            $strFile ==> text of the webpage //            $strStart_Pattern ==> start of the substring //            $strEnd_Pattern ==> end of the substring function find_data($strFile,$strStart_Pattern,$strEnd_Pattern, $intStart_Position = 0) {   global $debug;   if($debug){ print("Searching for \"$strStart_Pattern\"...\"$strEnd_Pattern\".\n"); }   $first_match = strpos($strFile,$strStart_Pattern, $intStart_Position);   if ($first_match) {     # find the begining of the tag     for ($i = $first_match; $i>0;$i--) {       $char = $strFile[$i];       if ($char == "<" ) {         $first_match = $i; //record the location of the tag         break;       }     }     $partialbuffer = substr($strFile,$first_match,strlen($strFile) - $first_match);     # find the end of the sub string     $second_match = strpos($partialbuffer,$strEnd_Pattern);     return substr($partialbuffer,0,$second_match + strlen($strEnd_Pattern));   } else {     return(false);   } } //end function find_data // read_html_table - Read the contents of an HTML table and return the array(s) //                  strHTMLTable ==> HTML table text //                  boolSkipFirstRow ==> Skip the first row if it contains column titles (true/false) function read_html_table($strHTMLTable, $boolSkipFirstRow) {   global $debug;   $table = "";   $th = "";   $td = "";   if($boolSkipFirstRow) {     $rownum = -2;   } else {     $rownum = -1;   }   $rowopen = false;   $strPos = 0;   do {    // Scrape through the table     if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TR") {  //Look for the rows       $strPos +=3;       $strPos = strpos($strHTMLTable, ">", $strPos) + 1;      // Find the end of the row open tag       $rowopen = true;       $rownum++;     }     if (strtoupper(substr($strHTMLTable, $strPos, 5)) == "</TR>") {       $rowopen = false;       $strPos += 5;    // jump to the end of the close tag     }     if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TH") { // Look for headers       $strPos += 3;       $strPos = strpos($strHTMLTable, ">", $strPos) + 1;      // Find the end of the header open tag       $endPos = strpos(strtoupper($strHTMLTable), "</TH>", $strPos);  // Find the header close tag       $th[] = substr($strHTMLTable, $strPos, $endPos - $strPos);       $strPos = $endPos + 5;       if($debug) { print("Found column header \"" . end($th) . "\" (" . (count($th) - 1) . ")\n"); }     }     if (strtoupper(substr($strHTMLTable, $strPos, 3)) == "<TD") { // Look for cells       $strPos += 3;       $strPos = strpos($strHTMLTable, ">", $strPos) + 1;      // Find the end of the cell open tag       $endPos = strpos(strtoupper($strHTMLTable), "</TD>", $strPos);  // Find the cell close tag       $td[$rownum][] = str_replace(array("\n","\t"),"",substr($strHTMLTable, $strPos, $endPos - $strPos));       $td[$rownum][count($td[$rownum]) - 1] = str_replace("\r"," ",$td[$rownum][count($td[$rownum]) - 1]);       $td[$rownum][count($td[$rownum]) - 1] = preg_replace("/[\s]+/"," ",$td[$rownum][count($td[$rownum]) - 1]);       $strPos = $endPos + 5;       if($debug) { print("Found cell data \"" . end($td[$rownum]) . "\" (" . $rownum . "|" . (count($td[$rownum]) - 1) . ")\n"); }     }     $strPos++;   } while ($strPos <= strlen($strHTMLTable));   return $td;   /*   if ($boolRowOneColumnTitles) {     if ($th[0] != "") {        // Create a new array that uses the headers as the array key       $newtd = "";       $header = "";       $row = "";       if ($debug) { print(count($th) . " headers\n"); }       if ($debug) { print(count($td) . " rows\n"); }       for ($row = 0; $row <= count($td) - 1; $row++) {         for ($cell = 0; $cell <= count($td[$row]) - 1; $cell++) {           if ($debug) { print("(". $row . "|" . $cell . ") => " . $th[$cell] . "(" . $row . ") = " . $td[$row][$cell] . "\n"); }           $newtd[$th[$cell]][$cell] = $td[$row][$cell];         }       }       return $newtd;     } else {  // There are no headers, so just return the td array.       return $td;     }   } else {  // Just return what's in the td array     return $td;   }   */ } //end function readhtmltable // read_events_file - Reads the old events from event file function read_events_file() {   global $debug;   $eventfile = "roadclosed.txt";   $buffer = "";   if (is_file($eventfile)) { // Check to see if the event log exists     $fh = fopen($eventfile, "r");     while (!feof($fh)) {       $buffer .= fread($fh, 1024);     }     return $buffer;   }   return;  // Return an empty array }  // end function read_events_file // write_events_file - Writes the current events to a file to be read later //                  - strEvents ==> array of events function write_events_file($strEvents) {   global $debug;   $eventfile = "roadclosed.txt";   $fh = fopen($eventfile, "w");   if ($fh === false) {     if ($debug) { print("ERROR: Could not write to \"" . $eventfile . "\"!\n"); }   } else {     fwrite ($fh, $strEvents);     fclose ($fh);   }   return; }  // end function write_events_file print ("ClosedRoadScript"); print date("    (G:i:s)"); ?> [/code] That is my coding. What I need to do is if the script finds the words "There are no reported closures."  it will stop running and not email anything out. I hope someone can help me.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.