Created
February 9, 2010 18:15
-
-
Save kenzie/299486 to your computer and use it in GitHub Desktop.
Tide class fetches, caches and returns tide data from waterlevels.gc.ca
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// Tide class fetches, caches and returns tide data from waterlevels.gc.ca | |
// Kenzie Campbell <kenzie@route19.com> | |
// Feb. 9, 2010 | |
class Tide | |
{ | |
// instance variables (w/ defaults) | |
private $post_url = "http://www.waterlevels.gc.ca/cgi-bin/tide-shc.cgi"; | |
private $post_content = "zone=27&language=english®ion=5&station=610&queryType=predict&view=text&TZ=AST"; | |
private $cache_file = '/tmp/tide.cache'; | |
var $cache_life; | |
var $formatted_table; | |
// constructor | |
function Tide($cache_life=720) | |
{ | |
date_default_timezone_set('America/Halifax'); | |
$this->post_content .= "&year=".date('Y')."&month=".date('n')."&day=".date('j'); | |
$this->cache_life = $cache_life; | |
$this->setup_cache(); | |
} | |
// cache init | |
private function setup_cache() | |
{ | |
// are we cacheing? and is there a cache file? | |
if(($this->cache_life > 0) && (file_exists($this->cache_file))) | |
{ | |
// how old is the cache? | |
$file_age = (time() - filemtime($this->cache_file)); | |
// is the cache still viable? | |
if($file_age < ($this->cache_life * 60)) | |
{ | |
// use cache | |
$this->formatted_table = implode('', file($this->cache_file)); | |
} | |
// cache expired | |
else $this->update(); | |
} | |
// no cache file | |
else $this->update(); | |
} | |
private function update() | |
{ | |
// get live feed | |
$this->formatted_table = $this->parse_raw_tide_data($this->fetch_tide_data()); | |
// update cache | |
$this->write_cache(); | |
} | |
private function write_cache() | |
{ | |
// can we open the cache file? | |
if (!$fp = @fopen($this->cache_file, 'wb')) | |
{ | |
echo ("ERROR: Unable to open and write to cache file."); | |
return; | |
} | |
// write the data | |
flock($fp, LOCK_EX); | |
fwrite($fp, $this->formatted_table); | |
flock($fp, LOCK_UN); | |
fclose($fp); | |
} | |
// grab tide data | |
private function fetch_tide_data() | |
{ | |
$params = array('http' => array( | |
'method' => 'POST', | |
'content' => $this->post_content | |
)); | |
$ctx = stream_context_create($params); | |
$fp = @fopen($this->post_url, 'rb', false, $ctx); | |
if (!$fp) { | |
echo ("Problem with $url, $php_errormsg"); | |
} | |
$response = @stream_get_contents($fp); | |
if ($response === false) { | |
echo ("Problem reading data from $url, $php_errormsg"); | |
} | |
return $response; | |
} | |
// parse tide data | |
private function parse_raw_tide_data($data) | |
{ | |
preg_match("/#\sDate;Time;Height<br>(.*)<br><\/p><\/td>/", $data, $match); | |
$tides = $match[1]; | |
return $this->format_tide_table($tides); | |
} | |
// format tide table | |
private function format_tide_table($tides) | |
{ | |
$tides = str_replace('<br>', "\n", $tides); | |
$tides = explode("\n", $tides); | |
$formatted = "<table summary=\"Tide tables with columns for time of day and tide height (in meters).\">\n\t<thead>\n\t\t<tr><th scope=\"col\">Time</th><th scope=\"col\">Height</th></tr>\n\t</thead>\n"; | |
$old_date = ''; | |
foreach($tides as $tide) | |
{ | |
$data = explode(';', $tide); | |
if($data[0]!=$old_date) { | |
if($old_date!='') $formatted .= "\t</tbody>\n"; | |
$old_date = $data[0]; | |
$formatted .= "\t<tbody>\n\t\t<tr><th scope=\"rowgroup\" colspan=\"2\">".date('l, F jS, Y', strtotime($data[0]))."</th></tr>\n"; | |
} | |
$formatted .= "\t\t<tr><td>".$data[1]."</td><td>".$data[2]."m</td></tr>\n"; | |
} | |
$formatted .= "\t</tbody>\n</table>\n"; | |
return $formatted; | |
} | |
} | |
$tide = new Tide(0); | |
echo $tide->formatted_table; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment