hehe.... indeed.
Now I just want more raises. Anyway, since this is the programming forum, I'm going to post my finished source. This is currently live. I'm cutting out some parts that might be considered sensitive.
[php]
<?
define("StockUpdateFrequency", 60*60);
define("NewsUpdateFrequency", 60*60);
define("WeatherUpdateFrequency", 60*60);
class checkMyWebsite {
private $host = "http://theurl.com";
private $username = 'username';
private $password = 'password';
private $mydataarray;
private $bad_entries;
private $error;
private $emailTo =
'monitoringemail@mycompany.com';
public function __construct(){
$this->checkDate();
$this->checkTime();
$this->checkHolidays();
$this->executeCheck();
}
private function checkDate(){
if(date("l") == 'Sunday' || date("l") == 'Saturday'){
//it's a weekend, exit
die('weekend');
}
}
private function checkTime(){
$opening = mktime(8,30,0);
$closing = mktime(15,0,0);
if(time() < $opening || time() > $closing){
//it's either before 9:30am or after 4:00pm
die("too early or late");
}
}
private function checkHolidays(){
$holidays[] = '2006 November 23';
$holidays[] = '2006 December 25';
$holidays[] = '2007 January 1';
$holidays[] = '2007 January 15';
$holidays[] = '2007 February 19';
$holidays[] = '2007 April 6';
$holidays[] = '2007 May 28';
$holidays[] = '2007 July 4';
$holidays[] = '2007 September 3';
$holidays[] = '2007 November 22';
$holidays[] = '2007 December 25';
foreach($holidays AS $holiday){
if($holiday == date('Y F j')){
//today is a NYSE holiday
die("holiday");
}
}
}
private function executeCheck(){
$data = $this->connect();
$this->populateArray($data);
$errors = $this->getErrors();
if($this->bad_entries){
$this->send_error_report($errors);
}
}
private function connect(){
//set up url and posting data
$post_data = 'username=' .$this->username .'&password=' .$this->password;
//initialize curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->host);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
//execute curl
$store = curl_exec ($ch);
if(!$store){
//catch any errors
$this->error = curl_error($ch);
}
//close connection
curl_close ($ch);
return $store;
}
private function populateArray($store){
//explode results based on tr tags
$InStock = false;
$InNews = false;
$InWeather = false;
$exploded = explode("<tr>",$store);
for($i=0;$i<count($exploded);$i++){
if($InStock){
$tempExploded = explode("\r\n",strip_tags($exploded[$i]));
$this->mydataarray[$i]['type'] = 'Stock';
$this->mydataarray[$i]['symbol'] = $tempExploded[1];
$this->mydataarray[$i]['company'] = $tempExploded[2];
$this->mydataarray[$i]['lastupdate'] = strtotime($tempExploded[3]);
}
if($InNews){
$tempExploded = explode("\r\n",strip_tags($exploded[$i]));
$this->mydataarray[$i]['type'] = 'News';
$this->mydataarray[$i]['name'] = $tempExploded[1];
$this->mydataarray[$i]['lastupdate'] = strtotime($tempExploded[2]);
}
if($InWeather){
$tempExploded = explode("\r\n",strip_tags($exploded[$i]));
$this->mydataarray[$i]['type'] = 'Weather';
$this->mydataarray[$i]['zip'] = $tempExploded[1];
$this->mydataarray[$i]['location'] = $tempExploded[2];
$this->mydataarray[$i]['lastupdate'] = strtotime($tempExploded[3]);
}
//are we in the stock section?
if(strpos($exploded[$i],'Stock Symbol')){
$InStock = true;
}
if($InStock && strpos($exploded[$i],'</table>')){
$InStock = false;
}
//are we in the news section?
if(strpos($exploded[$i],'Description')){
$InNews = true;
}
if($InNews && strpos($exploded[$i],'</table>')){
$InNews = false;
}
//are we in the weather section?
if(strpos($exploded[$i],'Location')){
$InWeather = true;
}
if($InWeather && strpos($exploded[$i],'</table>')){
$InWeather = false;
}
}
}
private function getErrors(){
$stock = false;
$news = false;
$weather = false;
$return = '<table style="width: 100%;">';
foreach($this->mydataarray AS $row){
switch($row['type']){
case 'Stock':
if($row['lastupdate'] + StockUpdateFrequency > time()){ break; }
$this->bad_entries += 1;
if(!$stock){
$return .= '<tr><th style="text-align: left;">Symbol</th><th style="text-align: left;">Name</th><th style="text-align: left;">Last update</th></tr>';
}
$return .= '<tr><td>' .$row['symbol'] .'</td>';
$return .= '<td>' .$row['company'] .'</td>';
$return .= '<td>' .date("g:ia M j",$row['lastupdate']) .'</td></tr>';
$stock = true;
break;
//due to unavoidable update delays, I'm not checking the news any more
// case 'News':
// if($row['lastupdate'] + NewsUpdateFrequency > time()){ break; }
// $this->bad_entries += 1;
// if(!$news){
// $return .= '<tr><th>News Feed</th><th>Last update</th></tr>';
// }
// $return .= '<tr><td colspan=2>' .$row['name'] .'</td>';
// $return .= '<td>' .date("g:ia M j",$row['lastupdate']) .'</td></tr>';
// $news = true;
// break;
case 'Weather':
if($row['lastupdate'] + WeatherUpdateFrequency > time()){ break; }
$this->bad_entries += 1;
if(!$weather){
$return .= '<tr><th style="text-align: left;">Zip</th><th style="text-align: left;">Location</th><th style="text-align: left;">Last update</th></tr>';
}
$return .= '<tr><td>' .$row['zip'] .'</td>';
$return .= '<td>' .$row['location'] .'</td>';
$return .= '<td>' .date("g:ia M j",$row['lastupdate']) .'</td></tr>';
$weather = true;
break;
}
}
$return .= '</table>';
return $return;
}
private function send_error_report($errors){
$body = 'The following item';
if($this->bad_entries > 1){
$body .= 's have';
}
else{
$body .= ' has';
}
$body .= ' not updated within the last ' .(StockUpdateFrequency / 60) .' minutes.<br><br>' .$errors;
//set up smtp settings
ini_set('SMTP','myexchangeserver.mycompany.com');
ini_set('smtp_port','25');
$subject = 'Error With mypage.com Updates (' .$this->bad_entries .')';
$headers = 'from:
my.page@company.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send mail
mail($this->emailTo, $subject, $body, $headers);
}
}
$checkMypage = new checkMyWebsite();
?>
[/php]
This requires curl to be installed, but installing curl is easy as hell. Just download it and basically uncomment the curl line in php.ini. The install docs for curl in the php manual are retarded...they talk about recompiling php and all that, but it's not actually true.
Also, I know the code is a bit dirty and could've been handled better, but it's a quick thing I threw together. Given another week of dev time I probably could make it alot more pretty. I probably shouldn't have instantiated the class either, but bleh.