Created
December 25, 2022 03:54
-
-
Save Bluscream/ce03f3e905237a9f05b87f2923ce0009 to your computer and use it in GitHub Desktop.
RNV Parser API for HomeAssistant, etc.
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
<?php | |
// ini_set('display_errors', 1); | |
// ini_set('display_startup_errors', 1); | |
// error_reporting(E_ALL); | |
function getHumanReadableTimespan($minutes) { | |
$units = array( | |
'day' => 1440, | |
'hour' => 60, | |
'minute' => 1, | |
); | |
$timespan = ''; | |
foreach ($units as $name => $divisor) { | |
$quotient = floor($minutes / $divisor); | |
if ($quotient > 0) { | |
$timespan .= "$quotient $name"; | |
$timespan .= $quotient > 1 ? 's' : ''; | |
$timespan .= ' '; | |
$minutes -= $quotient * $divisor; | |
} | |
} | |
return trim($timespan); | |
} | |
$max = intval($_GET['max']) ?? 5; | |
$stop = $_GET['stop'] ?? "6032211"; | |
$platforms = explode(',', $_GET['platforms']) ?? array(); | |
$url = 'https://www.vrn.de/mngvrn/XSLT_DM_REQUEST?outputFormat=json&language=de&stateless=1&coordOutputFormat=WGS84[DD.ddddd]&coordOutputFormatTail=7&type_dm=stop&name_dm='.$stop.'&useRealtime=1&mode=direct&ptOptionsActive=1&deleteAssignedStops_dm=1&useProxFootSearch=0&mergeDep=1'; | |
$jsonData = file_get_contents($url); | |
$data = json_decode($jsonData, true); | |
$departures = array(); | |
$i = 0; | |
$now = new DateTime(array_values(array_filter($data['parameters'], function($v) { return $v['name'] == 'serverTime'; }))[0]['value']); | |
if (isset($_GET["meta"])) $departures["meta"] = [ | |
"datetime" => $now->format('Y-m-d H:i:s'), | |
"stop" => $data['dm']['itdOdvAssignedStops']['nameWithPlace'], | |
"url" => $url | |
]; | |
foreach ($data['departureList'] as $departure) { | |
if (isset($_GET['platforms']) && !in_array($departure['platform'], $platforms, true)) { continue; } | |
$i += 1; | |
if ($i > $max) break; | |
$dateTime = $departure['dateTime']; | |
$timestamp = mktime(0, 0, 0, $dateTime['month'], $dateTime['day'], $dateTime['year']); | |
$date = new DateTime("@$timestamp"); | |
$date->setTime($dateTime['hour'], $dateTime['minute']); | |
$formattedDateTime = $date->format('Y-m-d H:i:s'); | |
// $timeSpan = DateInterval::createFromDateString($departure['countdown'].' minutes'); // $now->diff($date, true); | |
$departures[$i] = [ | |
"time" => $formattedDateTime, | |
"minutes" => $departure['countdown'], | |
"timespan" => getHumanReadableTimespan(intval($departure['countdown'])), // $timeSpan->format('%a day, %h hour, %i minutes, %ss'), | |
"line" => $departure['servingLine']['number'], | |
"from" => $departure['servingLine']['directionFrom'], | |
"to" => $departure['servingLine']['direction'], | |
"platform" => $departure['platform'], | |
]; | |
} | |
header("Content-Type: application/json"); | |
echo json_encode($departures, JSON_PRETTY_PRINT); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment