Created
February 8, 2011 15:51
-
-
Save markomarkovic/816625 to your computer and use it in GitHub Desktop.
Quick'n'Dirty CakePHP Model for fetching and parsing Belgrade airport flight schedule RSS
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
<?php | |
class BelgradeFlightSchedule extends AppModel { | |
var $useTable = false; | |
var $name = 'BelgradeFlightSchedule'; | |
var $arrivalsURL = 'http://www.beg.aero/rss/time_table_arrivals.php?versionId=1&type=IA'; | |
var $departuresURL = 'http://www.beg.aero/rss/time_table_departures.php?versionId=1&type=ID'; | |
function find($conditions = null, $fields = array(), $order = null, $recursive = null) { | |
Cache::config('flight_schedule', array('engine' => 'File', 'duration' => '10 minutes')); | |
$schedule = Cache::read('flight_schedule'); | |
if ($schedule == false) { | |
$arrivals = file_get_contents($this->arrivalsURL); | |
$arrivals = $this->_parse($arrivals); | |
$departures = file_get_contents($this->departuresURL); | |
$departures = $this->_parse($departures); | |
$schedule = array('arrivals' => $arrivals, 'departures' => $departures); | |
Cache::write('schedule', $schedule); | |
} | |
return $schedule; | |
} | |
function _parse($s) { | |
preg_match('@'.date('d/m/Y').']]></title>.*?<table(.*?)</table>@ms', $s, $m); | |
preg_match_all('@<td .*?>.*?<b>(.*?)</b>.*?</td>.*?<td .*?>(.*?)</td>.*?<td .*?>(.*?)</td>.*?<td .*?>(.*?)</td>.*?<td .*?>(.*?)</td>.*?<td .*?>(.*?)</td>.*?<td .*?>(.*?)</td>@ms', $m[0], $m); | |
$res = array(); | |
foreach ($m[1] as $k => $v) { | |
$res[] = array( | |
'broj_leta' => $m[1][$k], | |
'aerodrom' => $m[2][$k], | |
'planiran' => $m[3][$k], | |
'ocekivan' => $m[4][$k], | |
'izlaz' => $m[5][$k], | |
'tip_aviona' => $m[6][$k], | |
'napomena' => $m[7][$k] | |
); | |
} | |
return $res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment