Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date parameter in vehicle GET request #210

Merged
merged 7 commits into from Jul 25, 2016
56 changes: 48 additions & 8 deletions api/data/NMBS/vehicleinformation.php
Expand Up @@ -11,6 +11,8 @@
include_once 'data/NMBS/stations.php';
include_once '../includes/simple_html_dom.php';
include_once '../includes/getUA.php';
use MongoDB\Collection;

class vehicleinformation
{
/**
Expand All @@ -21,8 +23,9 @@ class vehicleinformation
public static function fillDataRoot($dataroot, $request)
{
$lang = $request->getLang();
$date = $request->getDate();

$serverData = self::getServerData($request->getVehicleId(), $lang);
$serverData = self::getServerData($request->getVehicleId(), $date, $lang);
$html = str_get_html($serverData);

// Check if there is a valid result from the belgianrail website
Expand All @@ -40,16 +43,19 @@ public static function fillDataRoot($dataroot, $request)
if ($request->getAlerts() && self::getAlerts($html)) {
$dataroot->alert = self::getAlerts($html);
}

$vehicleOccupancy = self::getOccupancy(substr(strrchr($request->getVehicleId(), "."), 1));

$dataroot->stop = [];
$dataroot->stop = self::getData($html, $lang, $request->getFast());
$dataroot->stop = self::getData($html, $lang, $request->getFast(), iterator_to_array($vehicleOccupancy), $date);
}

/**
* @param $id
* @param $lang
* @return mixed
*/
private static function getServerData($id, $lang)
private static function getServerData($id, $date, $lang)
{
global $irailAgent; // from ../includes/getUA.php

Expand All @@ -61,7 +67,7 @@ private static function getServerData($id, $lang)
$scrapeURL = 'http://www.belgianrail.be/jp/sncb-nmbs-routeplanner/trainsearch.exe/'.$lang.'ld=std&seqnr=1&ident=at.02043113.1429435556&';
$id = preg_replace("/[a-z]+\.[a-z]+\.([a-zA-Z0-9]+)/smi", '\\1', $id);

$post_data = 'trainname='.$id.'&start=Zoeken&selectDate=oneday&date='.date('d%2fm%2fY').'&realtimeMode=Show';
$post_data = 'trainname='.$id.'&start=Zoeken&selectDate=oneday&date='.DateTime::createFromFormat('dmy', $date)->format('d%2fm%2fY').'&realtimeMode=Show';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $scrapeURL);
Expand All @@ -79,14 +85,26 @@ private static function getServerData($id, $lang)
return $result;
}

/**
* @param $vehicle
*/
private static function getOccupancy($vehicle)
{
$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$occupancy = new MongoDB\Collection($m, 'spitsgids', 'occupancy');

// If we ever start using a date as parmater the parameter should be put here as date
return $occupancy->find(array('vehicle' => $vehicle, 'date' => date('Ymd')));
}

/**
* @param $html
* @param $lang
* @param $fast
* @return array
* @throws Exception
*/
private static function getData($html, $lang, $fast)
private static function getData($html, $lang, $fast, $occupancyArr, $date)
{
try {
$stops = [];
Expand Down Expand Up @@ -207,23 +225,35 @@ private static function getData($html, $lang, $fast)
$nextDayArrival = 1;
}
$previousHour = (int)substr($departureTime, 0, 2);
$dateDatetime = DateTime::createFromFormat('dmy', $date);

$stops[$j] = new Stop();
$stops[$j]->station = $station;
$stops[$j]->departureDelay = $departureDelay;
$stops[$j]->departureCanceled = $departureCanceled;
$stops[$j]->scheduledDepartureTime = tools::transformTime('0' . $nextDay . 'd'.$departureTime.':00', date('Ymd'));
$stops[$j]->scheduledArrivalTime = tools::transformTime('0' . $nextDayArrival . 'd'.$arrivalTime.':00', date('Ymd'));
$stops[$j]->scheduledDepartureTime = tools::transformTime('0' . $nextDay . 'd'.$departureTime.':00', $dateDatetime->format('Ymd'));
$stops[$j]->scheduledArrivalTime = tools::transformTime('0' . $nextDayArrival . 'd'.$arrivalTime.':00', $dateDatetime->format('Ymd'));
$stops[$j]->arrivalDelay = $arrivalDelay;
$stops[$j]->arrivalCanceled = $arrivalCanceled;
$stops[$j]->platform = new Platform();
$stops[$j]->platform->name = $platform;
$stops[$j]->platform->normal = $normalplatform;
//for backward compatibility
$stops[$j]->time = tools::transformTime('0' . $nextDay . 'd'.$departureTime.':00', date('Ymd'));
$stops[$j]->time = tools::transformTime('0' . $nextDay . 'd'.$departureTime.':00', $dateDatetime->format('Ymd'));
$stops[$j]->delay = $departureDelay;
$stops[$j]->canceled = $departureCanceled;

// Add occupancy
foreach ($occupancyArr as $stopOccupancy) {
if($station->{'@id'} == $stopOccupancy["from"][0]) {
$URI = self::getURLForOccupancy($stopOccupancy["occupancy"]);

$stops[$j]->occupancy->{'@id'} = $URI;
$stops[$j]->occupancy->name = basename($URI);
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't breaking out of a for lop not writing very ugly code? Looks like this should have been a while loop instead?

}
}

$j++;
}

Expand Down Expand Up @@ -401,4 +431,14 @@ private static function getServerDataByUrl($url)

return $result;
}

private static function getURLForOccupancy($occupancy) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be strict, it's a URI, not a URL. A URL is a locator of a document, a URI is an identifier.

if($occupancy < 1/3) {
return 'https://api.irail.be/terms/low';
} else if ($occupancy > 2/3) {
return 'https://api.irail.be/terms/high';
} else {
return 'https://api.irail.be/terms/medium';
}
}
};
9 changes: 9 additions & 0 deletions api/requests/VehicleinformationRequest.php
Expand Up @@ -16,6 +16,7 @@ public function __construct()
{
parent::__construct();
parent::setGetVar('id', '');
parent::setGetVar('date', date('dmy'));
parent::setGetVar('fast', 'false');
parent::setGetVar('alerts', 'false');
parent::processRequiredVars(['id']);
Expand All @@ -29,6 +30,14 @@ public function getVehicleId()
return $this->id;
}

/**
* @return string
*/
public function getDate()
{
return $this->date;
}

/**
* @return mixed
*/
Expand Down