public static function parseData($xml, $lang) { //<waitingtime><line>5 </line><mode>M </mode><minutes>6 </minutes><destination>Herrma</destination> </waitingtime> preg_match_all("/<waitingtime>.*?<line>(.*?)<\\/line>.*?<mode>(.*?)<\\/mode>.*?<minutes>(.*?)<\\/minutes>.*?<destination>(.*?)<\\/destination>.*?<\\/waitingtime>/si", $xml, $matches); // echo $xml . "\n"; // var_dump($matches); $nodes = array(); for ($i = 1; $i < sizeof($matches[0]); $i++) { $nodes[$i - 1] = new DepartureArrival(); $nodes[$i - 1]->vehicle = "BE.MIVB." . $matches[2][$i] . $matches[1][$i]; $nodes[$i - 1]->time = date("U") + $matches[3][$i] * 60; $nodes[$i - 1]->delay = 0; // echo $matches[3][$i] . " "; try { $nodes[$i - 1]->station = stations::getStationFromName($matches[4][$i], $lang); } catch (Exception $e) { //fallback for if a station is not found $nodes[$i - 1]->station = new Station(); $nodes[$i - 1]->station->id = "notfound"; $nodes[$i - 1]->station->locationX = 0; $nodes[$i - 1]->station->locationY = 0; $nodes[$i - 1]->station->name = $matches[4][$i - 1]; $nodes[$i - 1]->station->standardname = $matches[4][$i - 1]; //echo "No stations found for ". $matches[4][$i-1] . "\n"; } // echo $nodes[$i-1]->station->name; $nodes[$i - 1]->platform = $nodes[$i - 1]->station->name; // echo $i-1 . "\n"; } return $nodes; }
private static function parseData($xml, $time, $lang) { $nodes = array(); //var_dump($xml); $i = 0; for ($i = 0; $i < sizeof($xml->VertrekkendeTrein); $i++) { $dep = $xml->VertrekkendeTrein[$i]; $stationNode = stations::getStationFromName($dep->EindBestemming, $lang); $unixtime = strtotime($dep->VertrekTijd); $delay = 0; if (isset($dep->VertrekVertraging)) { $delay = (int) $dep->VertrekVertraging * 60; } $vehicle = "NS.NL." . $dep->TreinSoort; //no ID? $platformNormal = !$dep->VertrekSpoor->wijziging; $vertrekspoor = $dep->VertrekSpoor . " "; $platform = $vertrekspoor; $left = false; $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; } return $nodes; }
public static function getStationFromName($name, $lang) { stations::fetchAllStationsFromDB("EN"); $i = 0; while ($i < sizeof(stations::$stations) && stations::$stations[$i]->name != $name) { $i++; } if (stations::$stations[$i]->name == $name) { return stations::$stations[$i]; } //otherwise search for other method: Hafas? return null; }
private static function getVehicleData($serverData, $id, $lang){ // determine the location of the vehicle preg_match("/=>(\d\d:\d\d)( \+(\d\d?)')? <a href=\"\/mobile\/SearchStation.*? >(.*?)<\/a>/smi", $serverData, $matches); $locationX = 0; $locationY = 0; if(isset($matches[4])){ $now = stations::getStationFromRTName($matches[4], $lang); $locationX = $now -> locationX; $locationY = $now -> locationY; } $vehicle = new Vehicle(); $vehicle->name = $id; $vehicle->locationX = $locationX; $vehicle->locationY = $locationY; return $vehicle; }
private static function getVehicleData($serverData, $id, $lang){ // determine the location of the vehicle $html = str_get_html($serverData); $nodes = $html->find("td[class*=TrainReperage]"); if ($nodes) { $station = $nodes[0]->parent()->children(1)->first_child()->plaintext; } $locationX = 0; $locationY = 0; if(isset($station)){ $now = stations::getStationFromRTName($station, $lang); $locationX = $now -> locationX; $locationY = $now -> locationY; } $vehicle = new Vehicle(); $vehicle->name = $id; $vehicle->locationX = $locationX; $vehicle->locationY = $locationY; return $vehicle; }
private static function getStationFromHafasLocation($locationX, $locationY, $lang) { preg_match("/(.)(.*)/", $locationX, $m); $locationX = $m[1] . "." . $m[2]; preg_match("/(..)(.*)/", $locationY, $m); $locationY = $m[1] . "." . $m[2]; return stations::getStationFromLocation($locationX, $locationY, $lang); }
/** * @param $locationX * @param $locationY * @param $lang * @return Station */ private static function getStationFromHafasDescription($name, $locationX, $locationY, $lang) { return stations::getStationFromName($name, $lang); }
public static function getStationFromRTName($name, $lang) { APICall::connectToDB(); try { $lang = mysql_real_escape_string(strtoupper($lang)); $name = mysql_real_escape_string($name); $query = "SELECT stations.`ID`,stations.`X`, stations.`Y`, stations.`STD`,stations.`{$lang}` FROM stations RIGHT JOIN railtime ON railtime.`ID` = stations.`ID` WHERE railtime.`RAILTIMENAME` = '{$name}'"; $result = mysql_query($query) or die("Could not get stationslist from DB"); $line = mysql_fetch_array($result, MYSQL_ASSOC); $station = new Station(); $station->id = $line["ID"]; $station->locationX = $line["X"]; $station->locationY = $line["Y"]; if ($line[$lang] != "") { $station->name = $line[$lang]; } else { $station->name = $line["STD"]; } $station->standardname = $line["STD"]; if ($station->id == "") { throw new Exception("doesn't matter what's in here. It doesn't get parsed", 0); } } catch (Exception $e) { //no station found, let's try a HAFAS lookup as last resort return stations::getStationFromName($name, $lang); } return $station; }
/** * @param $lang * @return array * @throws Exception */ private static function fetchAllStationsFromDB($lang) { $stations = []; $newstations = irail\stations\Stations::getStations(); foreach ($newstations->{"@graph"} as $station) { array_push($stations, stations::transformNewToOldStyle($station, $lang)); } return $stations; }
/** * @param $html * @param $id * @param $lang * @return null|Vehicle * @throws Exception */ private static function getVehicleData($html, $id, $lang) { // determine the location of the vehicle $test = $html->getElementById('tq_trainroute_content_table_alteAnsicht'); if (!is_object($test)) { throw new Exception('Vehicle not found', 500); } // catch errors $nodes = $html->getElementById('tq_trainroute_content_table_alteAnsicht')->getElementByTagName('table')->children; for ($i = 1; $i < count($nodes); $i++) { $node = $nodes[$i]; if (!count($node->attr)) { continue; } // row with no class-attribute contain no data if (count($node->children[3]->find('a'))) { $as = $node->children[3]->find('a'); $stationname = reset($as[0]->nodes[0]->_); } else { // Foreign station, no anchorlink $stationname = reset($node->children[3]->nodes[0]->_); } $locationX = 0; $locationY = 0; // Station ID can be parsed from the station URL if (isset($node->children[3]->children[0])) { $link = $node->children[3]->children[0]->{'attr'}['href']; // With capital S if (strpos($link, 'StationId=')) { $nr = substr($link, strpos($link, 'StationId=') + strlen('StationId=')); } else { $nr = substr($link, strpos($link, 'stationId=') + strlen('stationId=')); } $nr = substr($nr, 0, strlen($nr) - 1); // delete ampersand on the end $stationId = '00' . $nr; $station = stations::getStationFromID($stationId, $lang); } else { $station = stations::getStationFromName($stationname, $lang); } if (isset($station)) { $locationX = $station->locationX; $locationY = $station->locationY; } $vehicle = new Vehicle(); $vehicle->name = $id; $vehicle->locationX = $locationX; $vehicle->locationY = $locationY; return $vehicle; } return; }
/** * @param $serverData * @param $id * @param $lang * @return null|Vehicle * @throws Exception */ private static function getVehicleData($serverData, $id, $lang) { // determine the location of the vehicle $html = str_get_html($serverData); $test = $html->getElementById('tq_trainroute_content_table_alteAnsicht'); if (!is_object($test)) { throw new Exception("Vehicle not found", 1); } // catch errors $nodes = $html->getElementById('tq_trainroute_content_table_alteAnsicht')->getElementByTagName('table')->children; for ($i = 1; $i < count($nodes); $i++) { $node = $nodes[$i]; if (!count($node->attr)) { continue; } // row with no class-attribute contain no data $as = $node->children[3]->find('a'); $station = reset($as[0]->nodes[0]->_); $locationX = 0; $locationY = 0; if (isset($station)) { $now = stations::getStationFromName($station, $lang); $locationX = $now->locationX; $locationY = $now->locationY; } $vehicle = new Vehicle(); $vehicle->name = $id; $vehicle->locationX = $locationX; $vehicle->locationY = $locationY; return $vehicle; } return null; }
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); }
public static function fillDataRoot($dataroot, $request) { $dataroot->station = stations::fetchAllStationsFromDB($request->getLang()); }
/** * @param $xml * @param $time * @param $lang * @param bool $fast * @param bool $showAlerts * @return array */ private static function parseData($xml, $time, $lang, $fast = false, $showAlerts = false) { //clean XML if (class_exists('tidy', false)) { $tidy = new tidy(); $tidy->parseString($xml, ['input-xml' => true, 'output-xml' => true], 'utf8'); $tidy->cleanRepair(); $xml = $tidy; } $data = new SimpleXMLElement($xml); $hour = substr($time, 0, 2); $data = $data->StationTable; //<Journey fpTime="08:36" fpDate="03/09/11" delay="-" //platform="2" targetLoc="Gent-Dampoort [B]" prod="L 758#L" //dir="Eeklo [B]" is_reachable="0" /> $nodes = []; $i = 0; $hour = substr($time, 0, 2); $hour_ = substr((string) $data->Journey[0]['fpTime'], 0, 2); if ($hour_ != '23' && $hour == '23') { $hour_ = 24; } $minutes = substr($time, 3, 2); $minutes_ = substr((string) $data->Journey[0]['fpTime'], 3, 2); while (isset($data->Journey[$i]) && ($hour_ - $hour) * 60 + ($minutes_ - $minutes) <= 60) { $journey = $data->Journey[$i]; $left = 0; $canceled = false; $delay = (string) $journey['delay']; if ($delay == '-') { $delay = '0'; } if ($delay == 'cancel') { $delay = 0; $canceled = true; } if (isset($journey['e_delay'])) { $delay = $journey['e_delay'] * 60; } preg_match("/\\+\\s*(\\d+)/", $delay, $d); if (isset($d[1])) { $delay = $d[1] * 60; } preg_match("/\\+\\s*(\\d):(\\d+)/", $delay, $d); if (isset($d[1])) { $delay = $d[1] * 3600 + $d[2] * 60; } $platform = '?'; // Indicate to end user platform is unknown $platformNormal = true; if (isset($journey['platform'])) { $platform = (string) $journey['platform']; } if (isset($journey['newpl'])) { $platform = (string) $journey['newpl']; $platformNormal = false; } $time = '00d' . (string) $journey['fpTime'] . ':00'; preg_match("/(..)\\/(..)\\/(..)/si", (string) $journey['fpDate'], $dayxplode); $dateparam = '20' . $dayxplode[3] . $dayxplode[2] . $dayxplode[1]; $unixtime = tools::transformtime($time, $dateparam); if ($fast) { $stationNode = new Station(); $stationNode->name = (string) $journey['targetLoc']; $stationNode->name = str_replace(' [B]', '', $stationNode->name); $stationNode->name = str_replace(' [NMBS/SNCB]', '', $stationNode->name); } else { $stationNode = stations::getStationFromName($journey['targetLoc'], $lang); } $veh = $journey['hafasname']; $veh = substr($veh, 0, 8); $veh = str_replace(' ', '', $veh); $vehicle = 'BE.NMBS.' . $veh; $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]->canceled = $canceled; $nodes[$i]->left = $left; $hour_ = substr((string) $data->Journey[$i]['fpTime'], 0, 2); if ($hour_ != '23' && $hour == '23') { $hour_ = 24; } $minutes_ = substr((string) $data->Journey[$i]['fpTime'], 3, 2); // Alerts if ($showAlerts && isset($journey->HIMMessage)) { $alerts = []; $himmessage = $journey->HIMMessage; for ($a = 0; $a < count($himmessage); $a++) { $alert = new Alert(); $alert->header = htmlentities(trim($himmessage[$a]['header'])); $alert->description = htmlentities(trim($himmessage[$a]['lead'])); array_push($alerts, $alert); } $nodes[$i]->alert = $alerts; } $i++; } return array_merge($nodes); //array merge reindexes the array }
/** * @param $xml * @param $time * @param $lang * @param bool $fast * @return array */ private static function parseData($xml, $time, $lang, $fast = false) { //clean XML if (class_exists("tidy", false)) { $tidy = new tidy(); $tidy->parseString($xml, ['input-xml' => true, 'output-xml' => true], 'utf8'); $tidy->cleanRepair(); $xml = $tidy; } $data = new SimpleXMLElement($xml); $hour = substr($time, 0, 2); $data = $data->StationTable; //<Journey fpTime="08:36" fpDate="03/09/11" delay="-" //platform="2" targetLoc="Gent-Dampoort [B]" prod="L 758#L" //dir="Eeklo [B]" is_reachable="0" /> $nodes = []; $i = 0; $hour = substr($time, 0, 2); $hour_ = substr((string) $data->Journey[0]["fpTime"], 0, 2); if ($hour_ != "23" && $hour == "23") { $hour_ = 24; } $minutes = substr($time, 3, 2); $minutes_ = substr((string) $data->Journey[0]["fpTime"], 3, 2); while (isset($data->Journey[$i]) && ($hour_ - $hour) * 60 + ($minutes_ - $minutes) <= 60) { $journey = $data->Journey[$i]; $left = 0; $delay = (string) $journey["delay"]; if ($delay == "-") { $delay = "0"; } $platform = ""; if (isset($journey["platform"])) { $platform = (string) $journey["platform"]; } $time = "00d" . (string) $journey["fpTime"] . ":00"; preg_match("/(..)\\/(..)\\/(..)/si", (string) $journey["fpDate"], $dayxplode); $dateparam = "20" . $dayxplode[3] . $dayxplode[2] . $dayxplode[1]; $unixtime = tools::transformtime($time, $dateparam); //GET DELAY preg_match("/\\+\\s*(\\d+)/", $delay, $d); if (isset($d[1])) { $delay = $d[1] * 60; } preg_match("/\\+\\s*(\\d):(\\d+)/", $delay, $d); if (isset($d[1])) { $delay = $d[1] * 3600 + $d[2] * 60; } if ($fast) { $stationNode = new Station(); $stationNode->name = (string) $journey["dir"]; $stationNode->name = str_replace(" [B]", "", $stationNode->name); $stationNode->name = str_replace(" [NMBS/SNCB]", "", $stationNode->name); } else { $stationNode = stations::getStationFromName($journey["dir"], $lang); } //GET VEHICLE AND PLATFORM $platformNormal = true; $veh = $journey["hafasname"]; $veh = substr($veh, 0, 8); $veh = str_replace(" ", "", $veh); $vehicle = "BE.NMBS." . $veh; $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; $hour_ = substr((string) $data->Journey[$i]["fpTime"], 0, 2); if ($hour_ != "23" && $hour == "23") { $hour_ = 24; } $minutes_ = substr((string) $data->Journey[$i]["fpTime"], 3, 2); $i++; } return array_merge($nodes); //array merge reindexes the array }
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); }