Jump to content

Help with a script


JayMan

Recommended Posts

Hi All,

 

Looking for some help if anyone can provide it.

 

I run this Twitter Bot.  https://twitter.com/OntarioRoads   A friend made this script years ago but he has passed and I've been able to keep it running just by sheer luck but I am trying to improve it since Twitter can do 280 char now.

 

How it works is the below script looks on this website for anything new  http://www.mto.gov.on.ca/english/traveller/trip/road_closures.shtml  and then makes it into a format like this and emails it out.

 

Subject: *-NEW-* HWY 401 Westbound Collector(s) [ McCOWAN ROAD -  ] - Disabled Vehicle  ...21:23:09

Body:

*-NEW-*
HWY 401 Westbound Collector(s) [ McCOWAN ROAD - ] - Disabled Vehicle
Highway: HWY 401
Direction: Westbound
From/At: McCOWAN ROAD
Lanes Affected: right lane and right shoulder blocked
Reason Disabled Vehicle
Last Change: Entered 2018-01-01 21:13

Event Start: January 01, 2018 21:13
====END====

 

To which then I grab the subject line and feed it into a basic command line windows app I have and tweet it out.

 

What I am looking to do is increase the info in the tweets now that I can.  I think if I can just add more info to the subject line and then tweet that out it would work but I am up for any ideas you may all have to make it better.  I don't make any money off this twitter bot you will see there are no ads etc..  

 

I've included a screenshot of a piece of the DB so you can see what all gets populated.  

 

DB.jpg

<?php
// Declare variables //
$debug = false;
//$url = "http://www.mto.gov.on.ca/english/traveller/conditions/rdclosure.htm";
$url = "http://www.mto.gov.on.ca/english/traveller/trip/road_closures.shtml";
//$url = "./road_closures.html";

$email_recip = "REMOVED";
#$email_recip = "REMOVEDt";
$email_from = "REMOVED";
$email_from_addr = "REMOVED";
$message_header = "";
$message_footer = "";
$arfield1 = array("summary","highway","direction","fromat","to","lanesaffected","trafficimpact","reason","others","eventstart","eventend");
$cntarfield1 = count($arfield1);
$timenow = date("Y-m-d H:i:s");
// Connect to database
$dbname = "mevents";
$dbuser = "REMOVED";
$dbpass = "REMOVED";
$datestamp = date("  ...G:i:s");

$link = mysql_connect('localhost',$dbuser,$dbpass);
if (!$link) { 
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname);


// DON'T EDIT PAST HERE UNLESS YOU KNOW WHAT YOU'RE DOING //
// MAIN PROGRAM ///
$webpage = read_web($url);
$content = find_data($webpage, "<!-- MIDDLE COLUMN", "</html>"); 
$content = find_data($content, "</div><a ", "</html>");
$curr_events = read_html_table($content,true);       // Parse the events HTML table

if ($curr_events["totalevents"]>0)
{
  $sql = "update mEvents set eventid = 'xRoadInfo' where eventid like 'RoadInfo%'";
  $result = mysql_query($sql);
}

for ($curr_event_num = 1; $curr_event_num <= $curr_events["totalevents"]; $curr_event_num++) {
  $send_email = false;     // Flag used to see if an email should be sent for new closure events
  $event_message = $message_header;
  $curr_event_found = false;

  if ($curr_events[$curr_event_num]["unscheduledevents"]==0)
  {
  	$sql = "select * from mEvents where eventid = '".($curr_events[$curr_event_num]["eventid"])."' limit 1";
  }
  else
  {
  	$unscd = explode("\n",$curr_events[$curr_event_num]["others"]["data"]);
  	$sql = "select * from mEvents where eventid='xRoadInfo' and highway='".$curr_events[$curr_event_num]["highway"]["data"]."' and direction='".$curr_events[$curr_event_num]["direction"]["data"]."' and fromat='".$curr_events[$curr_event_num]["fromat"]["data"]."'";
  	if ($unscd[0]!="") $sql .= " and others like '".$unscd[0]."%'";
  }
  $result = mysql_query($sql);
  if (mysql_num_rows($result)>0) $curr_event_found = true;

  if (!$curr_event_found) {    // Add the not found current event to the message
	$send_email = true;
	
	if ($curr_events[$curr_event_num]["eventend"]["data"]=="")
    {
        $event_message .= "*-NEW-*\n";
    }
	else
	{
		$event_message .= "-CLEARED-\n";
	}
	if ($debug) $event_message .= $curr_events[$curr_event_num]["eventid"];
	$sqlquery = "insert into mEvents (eventid,date_insert";
	$sqlval = "('".$curr_events[$curr_event_num]["eventid"]."','".$timenow."'";
	for ($a=0;$a<$cntarfield1;$a++)
	{
	   $sqlquery .= ",`".$arfield1[$a]."`";
	   
	   $sqlval .= ",'".addslashes($curr_events[$curr_event_num][$arfield1[$a]]["data"])."'";
	   if ($curr_events[$curr_event_num][$arfield1[$a]]["data"]!="")
	   {
	     $event_message .= $curr_events[$curr_event_num][$arfield1[$a]]["field"]."".$curr_events[$curr_event_num][$arfield1[$a]]["data"]."\n";
	   }
	   if ($debug) $event_message .= "<br/>";
	}
	$sqlquery .= ") values".$sqlval.")";
	mysql_query($sqlquery);
	$event_message .= "====END====\n";
	$email_subject = "*-NEW-* ";
	$email_subject .= $curr_events[$curr_event_num]["summary"]["data"];
	$email_subject .= $datestamp;
	//$email_subject .= " #BellLetsTalk";
	
  } else {
	$row = mysql_fetch_array($result);
	if ($row["eventend"]=="")
	{
		if ($curr_events[$curr_event_num]["unscheduledevents"]==0)
		{
		if ($curr_events[$curr_event_num]["eventend"]["data"]!="")
		{
			$send_email = true;
			$event_message .= "-CLEARED-\n";
			if ($debug) $event_message .= $curr_events[$curr_event_num]["eventid"];
			$sqlquery = "update mEvents set date_update='$timenow'";
			for ($a=0;$a<$cntarfield1;$a++)
			{
			   $sqlquery .= ",`".$arfield1[$a]."`='".addslashes($curr_events[$curr_event_num][$arfield1[$a]]["data"])."'";
			   if ($curr_events[$curr_event_num][$arfield1[$a]]["data"]!="")
	   		   {
			     $event_message .= $curr_events[$curr_event_num][$arfield1[$a]]["field"]."".$curr_events[$curr_event_num][$arfield1[$a]]["data"]."\n";
			   }
			   if ($debug) $event_message .= "<br/>";
			}
			$sqlquery .= " where eventid='".$curr_events[$curr_event_num]["eventid"]."'";
			mysql_query($sqlquery);
			$event_message .= "====END====\n";
			$email_subject = "*-CLR-* ";
			$email_subject .= $curr_events[$curr_event_num]["summary"]["data"];
			$email_subject .= $datestamp;
			//$email_subject .= " #BellLetsTalk";
			
		}
		}
		else
		{
			$sqlquery = "update mEvents set eventid='".$curr_events[$curr_event_num]["eventid"]."' where id=".$row["id"];
			mysql_query($sqlquery);
		}
	}
 }
 $event_message .= $message_footer;

 //Send an email if there is a new/resolved event 
 if ($send_email) { 
	if ($debug) print ("Sending the message below to " . $email_recip . ", from \"" . $email_from . "\" <" .$email_from_addr . ">.\n" . $event_message . "\n");
	else mail ($email_recip, $email_subject, $event_message, "From: \"" . $email_from . "\" <" .$email_from_addr . ">");
 }

}
// FOUR LINES BELOW COMMENTED OUT  TO STOP REPEATING MESSAGE
//if ($curr_events["totalevents"]>0)
//{
 // $sql = "delete from mEvents where eventid = 'xRoadInfo'";
  //$result = mysql_query($sql);
//}

mysql_close();

// 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,$arfield1,$cntarfield1;
  $arrevents = array();

  preg_match_all('/<a id="Event.+<\/table>/Us',$strHTMLTable,$dataevents);
  $dataevents = $dataevents[0];

  $countdataevents = count($dataevents);

  $j = 0;
  for($i=0; $i<$countdataevents; $i++)
  {
	preg_match('/<a id="Event([0-9]{4,8})">/', $dataevents[$i], $matches);
	$eventid = $matches[1];

	//if ($eventid>0)
	{
		$j++;
		$arrevents[$j]["eventid"] = $eventid;
		preg_match('/<th colspan="2">(.*)<\/th>/', $dataevents[$i], $matches);
		$arrevents[$j]["summary"]["field"] = "";
		$arrevents[$j]["summary"]["data"] = $matches[1];

		preg_match_all('/<tr>.+<\/tr>/Us',$dataevents[$i],$tr);
		$tr = $tr[0];
		$counttr = count($tr);

		for ($k=1;$k<$cntarfield1;$k++)
		{
			$arrevents[$j][$arfield1[$k]]["field"] = "";
			$arrevents[$j][$arfield1[$k]]["data"] = "";
			
		}
		if ($eventid>0) $arrevents[$j]["unscheduledevents"] = 0;
		else $arrevents[$j]["unscheduledevents"] = 1;

		for ($k=1;$k<$counttr;$k++)
		{
			preg_match_all('/<td.+<\/td>/Us',$tr[$k],$td);
			$td = $td[0];
			$td1 = cleandatafield(cleandata($td[0]));

			if (checkdatafield($td1)==1)
			{
				$arrevents[$j][$td1]["field"] = cleandata($td[0])." ";
				$arrevents[$j][$td1]["data"] = cleandata($td[1]);
			}
			else
			{
				if (cleandata($td[1])!="")
				{
					$arrevents[$j]["others"]["field"] = "";
					$arrevents[$j]["others"]["data"] .= cleandata($td[0])." ".cleandata($td[1])."\n";
				}
			}
		}
	}
  }
  echo "<br/>\ntotal ".$j;
  $arrevents["totalevents"] = $j;
  return $arrevents;
} //end function readhtmltable

function cleandata($x)
{
  $y = $x;
  $y = preg_replace('/<td.+>/U',"",$y);
  $y = str_replace("</td>","",$y);
  $y = str_replace("<strong>","",$y);
  $y = str_replace("</strong>","",$y);
  $y = str_replace("/r/n","",$y);
  $y = trim($y);

  return $y;
}
function cleandatafield($x)
{
  $y = $x;
  $y = str_replace("/","",$y);
  $y = str_replace(":","",$y);
  $y = str_replace(" ","",$y);
  $y = strtolower($y);
  return $y;
}
function checkdatafield($x)
{
  global $debug,$arfield1,$cntarfield1;
  $found = 0;
  for ($i=0;$i<$cntarfield1;$i++)
  {
	if ($x==$arfield1[$i]) $found = 1;
  }
  return $found;
}

print ("ClosedRoadScript");
print date("     G:i:s");
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.