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
pietercolpaert
merged 7 commits into
iRail:development
from
StanCallewaert:feature/date-paraeter-GET-vehicle
Jul 25, 2016
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab51925
Add the occupancy score to the API vehicle GET request.
StanCallewaert 28bc7d5
Date parameter added to the GET vehicle request.
StanCallewaert 719e6e9
Made sure the date parameter has the same format as all the other iRa…
StanCallewaert 0b4493f
Create a date and time field in the occupancy table, that way one can…
StanCallewaert e4d398e
- Made improvements according to: https://github.com/iRail/iRail/pull…
StanCallewaert 52dd218
Hotfix (made sure user has to wait a minute to do a new post request).
StanCallewaert 0860da6
Documentation for the NumberToURI function.
StanCallewaert File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
/** | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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); | ||
|
@@ -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 = []; | ||
|
@@ -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; | ||
} | ||
} | ||
|
||
$j++; | ||
} | ||
|
||
|
@@ -401,4 +431,14 @@ private static function getServerDataByUrl($url) | |
|
||
return $result; | ||
} | ||
|
||
private static function getURLForOccupancy($occupancy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?