Skip to content

Commit

Permalink
Merge pull request #48 from brechtvdv/Fix-#45
Browse files Browse the repository at this point in the history
Fix #45
  • Loading branch information
Pieter Colpaert committed Apr 21, 2015
2 parents e37a590 + e194d44 commit 26b91eb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 59 deletions.
7 changes: 4 additions & 3 deletions api/data/NMBS/stations.php
Expand Up @@ -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];

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
137 changes: 81 additions & 56 deletions api/data/NMBS/vehicleinformation.php
@@ -1,4 +1,4 @@
<?
<?php
/** Copyright (C) 2011 by iRail vzw/asbl
*
* This will fetch all vehicledata for the NMBS.
Expand All @@ -12,59 +12,74 @@
include_once("../includes/simple_html_dom.php");
class vehicleinformation{
public static function fillDataRoot($dataroot,$request){
$lang= $request->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;
}


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; $i<count($nodes); $i++){
$node = $nodes[$i];
if(!count($node->attr)) 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){
Expand All @@ -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; $i<count($nodes); $i++){
$node = $nodes[$i];
if(!count($node->attr)) 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;
}

};
Expand Down

0 comments on commit 26b91eb

Please sign in to comment.