oebb/lib/oebbApi.php

512 lines
18 KiB
PHP
Raw Normal View History

<?php
class oebbApi {
protected $i = 0;
protected $data = array();
public function __construct() {
session_start();
$_SESSION["oebbApi"]["get"] = false;
$_SESSION["oebbApi"]["getDetails"] = false;
}
public function __destruct() {
2014-08-28 08:43:14 +00:00
// session_destroy();
}
public function get($departure, $arrival, $amount, $date = null, $time = null, $timeMode = false) {
$date = ($date == null ? date("d.m.Y") : $date);
$time = ($time == null ? date("H:i") : $time);
$url = 'http://fahrplan.oebb.at/bin/query.exe/dn?start=1&S='
. urlencode(utf8_decode($departure)) .
'&Z='
. urlencode(utf8_decode($arrival)) .
'&REQ0JourneyDate='
. ($date) .
'&time='
. $time;
if ($timeMode == true) {
$url .= "&timesel=arrive";
}
$this->loadUrl($url);
if (!$this->isValid($departure, $arrival)) {
return false;
}
$i = 0;
while ($amount > $this->i) {
$tempOkey = (pq('#trOverviewC0-' . $i)->getString());
if (!empty($tempOkey)) {
$this->data[] = $this->getConnection($i);
$this->i++;
$i++;
} else {
$time = $this->getConnection($i - 1);
$time = strtotime($time["startTime"] . ' + 1 minute');
$time = date("H:i", $time);
$date = $this->getConnection($i - 1);
$date = $date["startDate"];
$this->get($departure, $arrival, $amount, $date, $time);
}
}
$_SESSION["oebbApi"] = $this->data;
$_SESSION["oebbApi"]["get"] = true;
return $this->data;
}
public function getCoords() {
if($_SESSION["oebbApi"]["getDetails"] == false) {
return false;
}
$details = $_SESSION["oebbApi"]["detail"];
$returnArray = array();
foreach ($details AS $detail) {
$url = $detail["data"]["productUrl"];
if ($url != null) {
$this->loadUrl($url);
$temp = (pq('.detailContainer')->HTML());
$temp = str_replace("zebracol-2", "zebracol", $temp);
$temp = str_replace("zebracol-1", "zebracol", $temp);
$temp = str_replace('title="Anfang Ihrer Verbindung -> Einstieg"', 'class="pqOkey"', $temp);
$temp = str_replace('title="Ende Ihrer Verbindung -> Ausstieg"', 'class="pqOkey"', $temp);
//$temp = $this->str_replace_once("center sepline", "ankunftPq", $temp);
$i = 0;
$j = 0;
$img = pq('.resultTable:ep(0) td')->html();
$img = explode('<a href="', $img);
2014-08-28 08:43:14 +00:00
// while(1) {
while (true) {
$stations = pq('.nowrap.sepline:ep(' . $i . ')')->html();
$ankunft = explode("\n\n", pq(".center.sepline:nth-child(3n+3)")->text());
$abfahrt = explode("\n\n", pq(".center.sepline:nth-child(5n+5)")->text());
$time = array();
for($k = 0; $k < count($ankunft); $k++) {
$ankunft[$k] = $this->trim($ankunft[$k]);
$abfahrt[$k] = $this->trim($abfahrt[$k]);
if(strpos($ankunft[$k], ":") !== -1) {
$time[] = $ankunft[$k];
} else {
if(strpos($abfahrt[$k], ":") !== -1) {
$time[] = $abfahrt[$k];
}
}
}
$stations = explode("</a>", $stations);
$finalStations = "";
foreach ($stations as $station) {
$tempStation = strip_tags($this->trim($station));
$tempStation = preg_replace('/\([^)]*\)|[()]/', '', $tempStation);
$oneStation = json_decode(file_get_contents("http://www.oebb.at/__ressources/system/stationsHafas.jsp?q=" . urlencode($tempStation)), true);
if (isset($oneStation[0]) && $oneStation[0] != null) {
if (strpos($img[$j], 'oc.gif') !== false) {
$finalStations[] = array(
"name" => $oneStation[0]["value"],
"xcoord" => $oneStation[0]["xcoord"],
"ycoord" => $oneStation[0]["ycoord"],
"realName" => $tempStation,
"time" => $time[$j],
);
}
}
$j++;
}
if (pq('.zebracol:ep(' . $i + 1 . ')')->text() == "") {
break;
}
$i++;
}
$returnArray[] = $finalStations;
}
}
$finalArray = array();
$tempArray = array();
$lastTime = null;
$lastXcoord = null;
$lastYcoord = null;
$runned = false;
for($i = 0; $i < count($returnArray); $i++) {
for($j = 0; $j < count($returnArray[$i]); $j++) {
if($runned === false){
$runned = true;
$lastTime = $returnArray[$i][$j]["time"];
$lastXcoord = $returnArray[$i][$j]["xcoord"];
$lastYcoord = $returnArray[$i][$j]["ycoord"];
$tempArray[] = $returnArray[$i][$j];
} else {
$time = $this->timeToMinutes($lastTime, $returnArray[$i][$j]["time"]);
$km = $this->coordsToKm(floatval($lastXcoord)/1000000, floatval($lastYcoord)/1000000, floatval($returnArray[$i][$j]["xcoord"])/1000000, floatval($returnArray[$i][$j]["ycoord"])/1000000);
if(($km)/($time) < 6) {
$tempArray[] = $returnArray[$i][$j];
}
$lastTime = $returnArray[$i][$j]["time"];
$lastXcoord = $returnArray[$i][$j]["xcoord"];
$lastYcoord = $returnArray[$i][$j]["ycoord"];
}
}
$runned = false;
$finalArray[] = $tempArray;
$tempArray = array();
}
return $finalArray;
}
public function validate($name) {
$temp = json_decode(file_get_contents("http://www.oebb.at/__ressources/system/stationsHafas.jsp?q=" . urlencode($name)), true);
$return = array();
foreach($temp as $temp1) {
$return[] = array(
"value" => $temp1["value"],
"id" => $temp1["extId"],
);
}
return $return;
}
public function getDetails($index) {
if ($index > count($_SESSION["oebbApi"])) {
return false;
}
if($_SESSION["oebbApi"]["get"] == false) {
return false;
}
$this->loadUrl($_SESSION["oebbApi"][$index]["changesLink"]);
$temp = (pq('.detailContainer')->HTML());
$temp = str_replace("conFirstSecFirstRow", "intermediateSection", $temp);
$temp = str_replace("conLastSecLastRow", "intermediateSection", $temp);
$temp = str_replace('title="Fahrtinformation"', 'id="connectionInfo"', $temp);
phpQuery::newDocumentHTML($temp, $charset = 'utf-8');
$changes = array();
$i = 0;
$j = 0;
$fuss = 0;
$lastDate = "";
while (1) {
$startDateTemp =
pq('.intermediateSection .date:eq(' . $i . ')')->text();
$startDateTemp = $this->trim($startDateTemp);
if ($startDateTemp == "") {
$startDate = $lastDate;
} else {
$lastDate = $startDateTemp;
$startDate = $startDateTemp;
}
$startStation =
pq('.intermediateSection .station:eq(' . $i . ')')->text();
$startTime =
pq('.intermediateSection .timeValue:eq(' . $i . ')')->text();
$startPlattform =
pq('.intermediateSection .platform:eq(' . $i . ')')->text();
$startPrognose =
pq('.intermediateSection .realTime:eq(' . $i . ')')->text();
$productName =
pq('.intermediateSection img.product:eq(' . $j . ')')->attr("alt");
$productImg =
"http://fahrplan.oebb.at" .
(pq('.intermediateSection img.product:eq(' . $j . ')')->attr("src"));
if ($productImg == "http://fahrplan.oebb.at/img/vs_oebb/fuss_pic.gif") {
$productUrl = null;
$fuss -= 1;
} else {
$productUrl =
(pq('#connectionInfo:eq(' . $fuss . ')')->attr("href"));
}
$i++;
$endDateTemp =
pq('.intermediateSection .date:eq(' . $i . ')')->text();
$endDateTemp = $this->trim($endDateTemp);
if ($endDateTemp == "") {
$endDate = $lastDate;
} else {
$lastDate = $endDateTemp;
$endDate = $endDateTemp;
}
$endStation =
pq('.intermediateSection .station:eq(' . $i . ')')->text();
$endTime =
pq('.intermediateSection .timeValue:eq(' . $i . ')')->text();
$endPlattform =
pq('.intermediateSection .platform:eq(' . $i . ')')->text();
$endPrognose =
pq('.intermediateSection .realTime:eq(' . $i . ')')->text();
$startStation = $this->trim($startStation);
$endStation = $this->trim($endStation);
$startTime = substr($startTime, 3);
$endTime = substr($endTime, 3);
$startTime = $this->trim($startTime);
$endTime = $this->trim($endTime);
$startPrognose = $this->trim($startPrognose);
$endPrognose = $this->trim($endPrognose);
$startPlattform = $this->trim($startPlattform);
$endPlattform = $this->trim($endPlattform);
$startPlattform = $this->trim($startPlattform);
$endPlattform = $this->trim($endPlattform);
$startPlattform = empty($startPlattform) ? "" : $startPlattform;
$endPlattform = empty($endPlattform) ? "" : $endPlattform;
$sTime = strtotime($startTime . " " . $startDate);
$eTime = strtotime($endTime . " " . $endTime);
$dur = $eTime - $sTime;
$duration = date("H:m", $dur);
if ($startStation != "") {
$changes[] = array(
"start" => array(
"station" => $startStation,
"time" => $startTime,
"plattform" => $startPlattform,
"date" => $startDate,
"prognose" => $startPrognose,
),
"end" => array(
"station" => $endStation,
"time" => $endTime,
"plattform" => $endPlattform,
"date" => $endDate,
"prognose" => $endPrognose,
),
"data" => array(
"productName" => $productName,
"productImg" => $productImg,
"productUrl" => $productUrl,
"duration" => $duration,
),
);
$i++;
$j++;
$fuss++;
} else {
break;
}
}
$_SESSION["oebbApi"]["detail"] = $changes;
$_SESSION["oebbApi"]["getDetails"] = true;
return $changes;
}
/* The Crap begins here */
protected function getChangesLink($pos) {
$temp = (pq('#linkDtlC0-' . $pos)->attr("href"));
return $temp;
}
protected function getChanges($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .changes')->text());
return intval($temp);
}
protected function getStartTime($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .planed')->html());
$temp = explode("<br>", $temp);
return substr($temp[0], 1, 5);
}
protected function getStartTimeEarly() {
$temp = (pq('#trOverviewC1-0 .planed')->html());
$temp = explode("<br>", $temp);
return substr($temp[0], 1, 5);
}
protected function getStartDateEarly() {
$temp = (pq('#trOverviewC1-0 .date')->html());
$temp = explode("<br>", $temp);
return $temp[0];
}
protected function getEndTime($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .planed')->html());
$temp = explode("<br>", $temp);
return substr($temp[1], 1, 5);
}
protected function getDuration($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .duration')->text());
return str_replace("\n", "", $temp);
}
protected function getPrognose($pos) {
$temp = (pq('.prognosis:eq(' . $pos . ')')->html());
return $this->trim($temp);
}
protected function getStartStation($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .station div ')->getString());
return $this->trim($temp[0]);
}
protected function getEndStation($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .station div ')->getString());
return $this->trim($temp[2]);
}
protected function getBuyLink($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .fares a')->attr("href"));
return $temp;
}
protected function getStartDate($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .date')->HTML());
$temp = explode("<br>", $temp);
return $temp[0];
}
protected function getEndDate($pos) {
$temp = (pq('#trOverviewC0-' . $pos . ' .date')->HTML());
$temp = explode("<br>", $temp);
if ($temp[1] == "\n") {
return $temp[0];
} else {
return $temp[1];
}
}
protected function loadUrl($url) {
$file = file_get_contents($url);
phpQuery::newDocumentHTML($file);
}
protected function getConnection($pos) {
$data = array(
"startStation" => $this->getStartStation($pos),
"endStation" => $this->getEndStation($pos),
"startDate" => $this->getStartDate($pos),
"endDate" => $this->getEndDate($pos),
"startTime" => $this->getStartTime($pos),
"endTime" => $this->getEndTime($pos),
"duration" => $this->getDuration($pos),
"changes" => $this->getChanges($pos),
"buyLink" => $this->getBuyLink($pos),
"changesLink" => $this->getChangesLink($pos),
"products" => $this->getProducts($pos),
"prognose" => $this->getPrognose($pos),
"id" => $this->i,
"earlier" => $this->getEarlierLink(),
);
return $data;
}
protected function isValid($start, $end) {
$temp = (pq('#trOverviewC0-0')->getString());
if (empty($temp)) {
return false;
} else {
return true;
}
}
protected function str_replace_once($str_pattern, $str_replacement, $string) {
if (strpos($string, $str_pattern) !== false) {
$occurrence = strpos($string, $str_pattern);
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
}
return $string;
}
protected function stringInsert($str, $pos, $insertstr) {
if (!is_array($pos))
$pos = array($pos);
$offset = -1;
foreach ($pos as $p) {
$offset++;
$str = substr($str, 0, $p + $offset) . $insertstr . substr($str, $p + $offset);
}
return $str;
}
protected function getProducts($pos) {
2017-08-31 10:45:39 +00:00
# $products = "";
$products = array();
$i = 0;
while(1) {
$src = (pq('#trOverviewC0-' . $pos . ' .product:eq(' . $i . ')')->attr('src'));
$alt = (pq('#trOverviewC0-' . $pos . ' .product:eq(' . $i . ')')->attr('alt'));
if(empty($src)) {
break;
} else {
$products[] = array(
"name" => $alt,
"img" => "http://fahrplan.oebb.at" . $src,
);
$i++;
}
}
return $products;
}
protected function coordsToKm($x1, $y1, $x2, $y2) {
return acos(sin($x1=deg2rad($x1))*sin($x2=deg2rad($x2))+cos($x1)*cos($x2) *cos(deg2rad($y2)-deg2rad($y1)))*(6378.137);
}
protected function getEarlierLink() {
$temp = (pq('.navi a:ep(0)')->attr("href"));
$this->loadUrl($temp);
$start = $this->getStartTimeEarly();
return $start;
}
protected function timeToMinutes($t1, $t2) {
$t1 = strtotime($t1);
$t2 = strtotime($t2);
return ($t2 - $t1)/60;
}
protected function trim($string) {
return trim($string, "\xC2\xA0\n\t\r\0\x0B");
}
}