diff --git a/api/data/NMBS/stations.php b/api/data/NMBS/stations.php index fd0d3a2f..91274109 100644 --- a/api/data/NMBS/stations.php +++ b/api/data/NMBS/stations.php @@ -108,7 +108,8 @@ public static function getStationFromName($name, $lang){ $post = http_get($url) or die("iRail down"); $stationsgraph = json_decode(http_parse_message($post)->body); if(!isset($stationsgraph->{'@graph'}[0])){ - die("No station found for " . urldecode($name)); + // die("No station found for " . urldecode($name)); + return ""; } $station = $stationsgraph->{'@graph'}[0]; @@ -142,7 +143,7 @@ public static function getStationFromRTName($name,$lang){ 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'"; + $query = "SELECT stations.`ID`,stations.`X`, stations.`Y`, stations.`STD`,stations.`$lang` FROM stations WHERE stations.`$lang` = '$name'"; $result = mysql_query($query) or die("Could not get stationslist from DB"); $line = mysql_fetch_array($result, MYSQL_ASSOC); $station = new Station(); @@ -160,7 +161,7 @@ public static function getStationFromRTName($name,$lang){ 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 diff --git a/api/data/NMBS/vehicleinformation.php b/api/data/NMBS/vehicleinformation.php index 135c0fcd..ae05ef30 100644 --- a/api/data/NMBS/vehicleinformation.php +++ b/api/data/NMBS/vehicleinformation.php @@ -1,4 +1,4 @@ -getLang(); - $serverData = vehicleinformation::getServerData($request->getVehicleId(),$lang); - $dataroot->vehicle = vehicleinformation::getVehicleData($serverData, $request->getVehicleId(), $lang); - $dataroot->stop = array(); - $dataroot->stop = vehicleinformation::getData($serverData, $lang, $request->getFast()); + $lang= $request->getLang(); + // NMBS stationnames in English are in Dutch + if($lang == 'EN') $lang = 'nl'; // Dutch + + $serverData = vehicleinformation::getServerData($request->getVehicleId(),$lang); + $dataroot->vehicle = vehicleinformation::getVehicleData($serverData, $request->getVehicleId(), $lang); + $dataroot->stop = array(); + $dataroot->stop = vehicleinformation::getData($serverData, $lang, $request->getFast()); } private static function getServerData($id,$lang){ - include_once("../includes/getUA.php"); - $request_options = array( - "referer" => "http://api.irail.be/", - "timeout" => "30", - "useragent" => $irailAgent, - ); - $scrapeURL = "http://www.railtime.be/mobile/HTML/TrainDetail.aspx"; - $id = preg_replace("/.*?(\d.*)/smi", "\\1", $id); - $scrapeURL .= "?l=" . $lang . "&tid=" . $id . "&dt=" . date( 'd%2fm%2fY' ); - $post = http_post_data($scrapeURL, "", $request_options) or die(""); - return http_parse_message($post)->body; + include_once("../includes/getUA.php"); + $request_options = array( + "referer" => "http://api.irail.be/", + "timeout" => "30", + "useragent" => $irailAgent, + ); + $scrapeURL = "http://www.belgianrail.be/jp/sncb-nmbs-routeplanner/trainsearch.exe/" . $lang . "ld=std&seqnr=1&ident=at.02043113.1429435556&"; + $id = preg_replace("/.*?(\d.*)/smi", "\\1", $id); + $post_data = "trainname=" . $id . "&start=Zoeken&selectDate=oneday&date=" . date( 'd%2fm%2fY' ) . "&realtimeMode=Show"; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $scrapeURL); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + + curl_close ($ch); + + return $result; } @@ -38,33 +51,35 @@ private static function getData($serverData, $lang, $fast){ try{ $stops = array(); $html = str_get_html($serverData); - $nodes = $html->find("tr.rowHeightTraject"); - $i = 0; - foreach ($nodes as $node) { - $row_delay = str_replace("'",'',str_replace('+','',trim($node->children(3)->first_child()->plaintext))); - if (isset($row_delay)) { - $arr= array(); - $arr = explode(":",$row_delay); - if(isset($arr[1])){ - $delay = (60*$arr[0] + $arr[1])*60; - }else{ - $delay = $row_delay * 60; - } - } else { - $delay = 0; - } - $stops[$i] = new Stop(); + $nodes = $html->getElementById('tq_trainroute_content_table_alteAnsicht')->getElementByTagName('table')->children; + + for($i=1; $iattr)) continue; // row with no class-attribute contain no data + + $delaynodearray = $node->children[2]->find('span'); + $delay = count($delaynodearray) > 0 ? trim(array_values($delaynodearray[0]->nodes[0]->_)[0]) : "0"; + $delayseconds = preg_replace("/[^0-9]/", '', $delay)*60; + + $arriveTime = array_values($node->children[1]->find('span')[0]->nodes[0]->_)[0]; + $departureTime = count($nodes[$i]->children[1]->children) == 3 ? array_values($nodes[$i]->children[1]->children[0]->nodes[0]->_)[0] : $arriveTime; + + if(count($node->children[3]->find('a'))) + $stationname = array_values($node->children[3]->find('a')[0]->nodes[0]->_)[0]; + else $stationname = array_values($node->children[3]->nodes[0]->_)[0]; + + $stops[$i-1] = new Stop(); $station = new Station(); if($fast == "true"){ - $station->name = $node->children(1)->first_child()->plaintext; + $station->name = $stationname; }else{ - $station = stations::getStationFromRTName($node->children(1)->first_child()->plaintext,$lang); + $station = stations::getStationFromName($stationname,$lang); } - $stops[$i]->station = $station; - $stops[$i]->delay = $delay; - $stops[$i]->time = tools::transformTime("00d" . $node->children(2)->first_child()->plaintext . ":00", date("Ymd")); - $i++; + $stops[$i-1]->station = $station; + $stops[$i-1]->delay = $delayseconds; + $stops[$i-1]->time = tools::transformTime("00d" . $departureTime . ":00", date("Ymd")); } + return $stops; } catch(Exception $e){ @@ -75,23 +90,33 @@ private static function getData($serverData, $lang, $fast){ 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; + + $test = $html->getElementById('tq_trainroute_content_table_alteAnsicht'); + if (!is_object($test)) die(""); // catch errors + + $nodes = $html->getElementById('tq_trainroute_content_table_alteAnsicht')->getElementByTagName('table')->children; + //echo $html->getElementById('tq_trainroute_content_table_alteAnsicht'); + for($i=1; $iattr)) continue; // row with no class-attribute contain no data + + $station = array_values($node->children[3]->find('a')[0]->nodes[0]->_)[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; } };