/** * @param $dataroot * @param $request * @throws Exception */ public static function fillDataRoot($dataroot, $request) { $arr = explode(".", $request->getStation()); $stationr = $request->getStation(); if (sizeof($arr) > 1) { $stationr = $arr[2]; } $dataroot->station = stations::getStationFromName($stationr, strtolower($request->getLang())); if (strtoupper(substr($request->getArrdep(), 0, 1)) == "A") { $html = liveboard::fetchData($dataroot->station, $request->getTime(), $request->getLang(), "arr"); $dataroot->arrival = liveboard::parseData($html, $request->getTime(), $request->getLang(), $request->isFast()); } elseif (strtoupper(substr($request->getArrdep(), 0, 1)) == "D") { $html = liveboard::fetchData($dataroot->station, $request->getTime(), $request->getLang(), "dep"); $dataroot->departure = liveboard::parseData($html, $request->getTime(), $request->getLang(), $request->isFast()); } else { throw new Exception("Not a good timeSel value: try ARR or DEP", 300); } }
/** * @param $dataroot * @param $request * @throws Exception */ public static function fillDataRoot($dataroot, $request) { //detect if this is an id or a station if (sizeof(explode(".", $request->getStation())) > 1) { $dataroot->station = stations::getStationFromID($request->getStation(), $request->getLang()); } else { $dataroot->station = stations::getStationFromName($request->getStation(), $request->getLang()); } if ($request->getArrdep() == "ARR") { $xml = liveboard::fetchData($dataroot->station, $request->getTime(), $request->getLang(), "A"); $dataroot->arrival = liveboard::parseData($xml, $request->getLang()); } elseif ($request->getArrdep() == "DEP") { $xml = liveboard::fetchData($dataroot->station, $request->getTime(), $request->getLang(), "D"); $dataroot->departure = liveboard::parseData($xml, $request->getLang()); } else { throw new Exception("Not a good timeSel value: try ARR or DEP", 300); } }
private static function parseData($html, $time, $lang) { $hour = substr($time, 0, 2); preg_match_all("/<tr>(.*?)<\\/tr>/ism", $html, $m); $nodes = array(); $i = 0; //for each row foreach ($m[1] as $tr) { preg_match("/<td valign=\"top\">(.*?)<\\/td><td valign=\"top\">(.*?)<\\/td>/ism", $tr, $m2); //$m2[1] has: time //$m2[2] has delay, stationname & platform //GET LEFT OR NOT $left = 0; if (preg_match("/color=\"DarkGray\"/ism", $tr, $lll) > 0) { $left = 1; } //GET TIME: preg_match("/((\\d\\d):\\d\\d)/", $m2[1], $t); //if it is 23 pm and we fetched data for 1 hour, we may end up with data for the next day and thus we will need to add a day $dayoffset = 0; if ($t[2] != 23 && $hour == 23) { $dayoffset = 1; } $time = "0" . $dayoffset . "d" . $t[1] . ":00"; $dateparam = date("ymd"); //day is previous day if time is before 4 O clock (NMBS-ish thing) if (date('G') < 4) { $dateparam = date("ymd", strtotime("-1 day")); } $unixtime = tools::transformTime($time, "20" . $dateparam); //GET DELAY $delay = 0; preg_match("/\\+(\\d+)'/", $m2[2], $d); if (isset($d[1])) { $delay = $d[1] * 60; } preg_match("/\\+(\\d):(\\d+)/", $m2[2], $d); if (isset($d[1])) { $delay = $d[1] * 3600 + $d[2] * 60; } //GET STATION preg_match("/.* (.*?) <span/", $m2[2], $st); //echo $st[1] . "\n"; $st = explode("/", $st[1]); $st = trim($st[0]); try { $stationNode = stations::getStationFromRTName(strtoupper($st), $lang); } catch (Exception $e) { //fallback: if no railtime name is available, let's ask HAFAS to solve this issue for us $stationNode = stations::getStationFromName($st, $lang); } //GET VEHICLE AND PLATFORM $platform = ""; $platformNormal = true; preg_match("/\\[(.*?)( .*?)?\\]/", $m2[2], $veh); $vehicle = "BE.NMBS." . $veh[1]; if (isset($veh[2])) { if (preg_match("/<[bf].*?>(.*?)<\\/.*?>/", $veh[2], $p)) { $platform = $p[1]; $platformNormal = false; } else { //echo $veh[2] . "\n"; preg_match("/ .*? (.*)/", $veh[2], $p2); if (isset($p2[1])) { $platform = $p2[1]; } } } $nodes[$i] = new DepartureArrival(); $nodes[$i]->delay = $delay; $nodes[$i]->station = $stationNode; $nodes[$i]->time = $unixtime; $nodes[$i]->vehicle = $vehicle; $nodes[$i]->platform = new Platform(); $nodes[$i]->platform->name = $platform; $nodes[$i]->platform->normal = $platformNormal; $nodes[$i]->left = $left; $i++; } return liveboard::removeDuplicates($nodes); }
private static function parseData($html, $time, $lang) { $hour = substr($time, 0, 2); preg_match_all("/<table class=\"StationList\">(.*?)<\\/table>/ism", $html, $m); $nodes = array(); $i = 0; //for each row foreach ($m[0] as $table) { $left = 0; preg_match_all("/<tr class=(.*?)>(.*?)<\\/tr>/ism", $table, $m2); if ($m2[1][0] == "rowStation trainLeft") { $left = 1; } preg_match_all("/<label>(.*?)<\\/label>/ism", $m2[2][0], $m3); //$m3[1][0] has : time //$m3[1][1] has : stationname preg_match_all("/<label class=\"orange\">(.*?)<\\/label>/ism", $m2[2][0], $delay); preg_match_all("/<label class=\"bold\">(.*?)<\\/label>/ism", $m2[2][0], $platform); preg_match_all("/<a class=\"button cmd blue\" href=(.*?)>><\\/a>/ism", $m2[2][0], $id); $delay = $delay[1][0]; $platform = $platform[1][0]; $id = $id[1][0]; //GET TIME: preg_match("/((\\d\\d):\\d\\d)/", $m3[1][0], $t); //if it is 23 pm and we fetched data for 1 hour, we may end up with data for the next day and thus we will need to add a day $dayoffset = 0; if ($t[2] != 23 && $hour == 23) { $dayoffset = 1; } $time = "0" . $dayoffset . "d" . $t[1] . ":00"; $dateparam = date("ymd"); //day is previous day if time is before 4 O clock (NMBS-ish thing) if (date('G') < 4) { $dateparam = date("ymd", strtotime("-1 day")); } $unixtime = tools::transformTime($time, "20" . $dateparam); //GET DELAY preg_match("/\\+(\\d+)'/", $delay, $d); if (isset($d[1])) { $delay = $d[1] * 60; } preg_match("/\\+(\\d):(\\d+)/", $delay, $d); if (isset($d[1])) { $delay = $d[1] * 3600 + $d[2] * 60; } //GET STATION $st = trim(utf8_encode($m3[1][1])); try { $stationNode = stations::getStationFromRTName(strtoupper($st), $lang); } catch (Exception $e) { //fallback: if no railtime name is available, let's ask HAFAS to solve this issue for us $stationNode = stations::getStationFromName($st, $lang); } //GET VEHICLE AND PLATFORM $platformNormal = true; $veh = explode(";", $id); $veh = explode("=", $veh[2]); $vehicle = "BE.NMBS." . str_replace("&", "", $veh[1]); $nodes[$i] = new DepartureArrival(); $nodes[$i]->delay = $delay; $nodes[$i]->station = $stationNode; $nodes[$i]->time = $unixtime; $nodes[$i]->vehicle = $vehicle; $nodes[$i]->platform = new Platform(); $nodes[$i]->platform->name = $platform; $nodes[$i]->platform->normal = $platformNormal; $nodes[$i]->left = $left; $i++; } return liveboard::removeDuplicates($nodes); }