Created
January 18, 2016 10:54
-
-
Save thuma/18d27b33071f804f135c to your computer and use it in GitHub Desktop.
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 file: departures.php | |
<?php | |
// Print heder: | |
header('Content-Type: application/json; charset=utf-8'); | |
if(intval(date("H")) > 21 OR intval(date("H")) < 6) | |
{ | |
die('{"error":"night"}'); | |
} | |
// Get all departures: | |
$alldep = array_merge(getById('9521')->Trains,getById('7565')->Buses,getById('7562')->Buses,getById('7564')->Buses); | |
// Generate empty array: | |
$alldepnew = array(); | |
// Loop all departures and puth them in groups: | |
foreach($alldep as $dep){ | |
if(isset($alldepnew[$dep->Destination.$dep->SiteId])==false){ | |
$alldepnew[$dep->Destination.$dep->SiteId] = array(); | |
} | |
$alldepnew[$dep->Destination.$dep->SiteId][] = $dep; | |
} | |
// Empty the data: | |
$alldep = array(); | |
// Put the data in order for export: | |
foreach($alldepnew as $stop){ | |
$alldep[] = $stop; | |
} | |
// Put the data in a objet for output: | |
$result->Departures = $alldep; | |
// Export data to user: | |
print json_encode($result); | |
// Function to get the data from API/File: | |
function getById($id){ | |
// Get key from key file: | |
$key = trim(file_get_contents('SL.key')); | |
// Check file age and if exists: | |
if(time()-filemtime($id.'.cache')>240 OR is_file($id.'.cache')==false){ | |
// Get data from API: | |
$data = json_decode(file_get_contents('http://api.sl.se/api2/realtimedepartures.json?key='.$key.'&siteid='.$id.'&timewindow=60')); | |
// Store data to Cache: | |
file_put_contents($id.'.cache', json_encode($data)); | |
// Return data: | |
return $data->ResponseData; | |
} | |
else{ | |
// Send data from cache: | |
return json_decode(file_get_contents($id.'.cache'))->ResponseData; | |
} | |
} | |
?> | |
##### HTML file, index.html: | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
table | |
{ | |
border-collapse:collapse; | |
width:100%; | |
} | |
td | |
{ | |
padding:5px; | |
font-size:110% | |
} | |
.time{text-align:right;} | |
.header{ | |
background-color:#c0c0c0; | |
font-weight:bolder; | |
} | |
th {background: #c0c0c0; | |
text-align:left; | |
font-size:110%; | |
font-weight:bold; | |
} | |
tr:nth-child(even) {background: #fafafa} | |
tr:nth-child(odd) {background: #f0f0f0} | |
body{ | |
background-color:#fafafa; | |
font-family:arial; | |
} | |
</style> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<script> | |
function getData() | |
{ | |
var xmlhttp; | |
if (window.XMLHttpRequest) | |
{// code for IE7+, Firefox, Chrome, Opera, Safari | |
xmlhttp=new XMLHttpRequest(); | |
} | |
else | |
{// code for IE6, IE5 | |
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
xmlhttp.onreadystatechange=function() | |
{ | |
if (xmlhttp.readyState==4 && xmlhttp.status==200) | |
{ | |
var data = JSON.parse(xmlhttp.responseText); | |
var nu = new Date(); | |
var min = nu.getMinutes(); | |
if(min<10){ | |
min = "0"+min; | |
} | |
var utdata = "<tr class='header'><th>Hållplats:</th><th>Läge/Spår:</th><th>Linje:</td><th>Mot:</th><th colspan='2'>Nästa:</th><th colspan='2' style='text-align:right'>uppdaterad: "+nu.getHours()+":"+min+"</th></tr>"; | |
var rowclass = 'rowa'; | |
data = data.Departures; | |
for (index = 0; index < data.length; ++index) | |
{ | |
if(data[index][0]['StopPointDesignation']==null){ | |
data[index][0]['StopPointDesignation'] = '-'; | |
} | |
utdata = utdata + '<tr><td>' + data[index][0]['StopAreaName'] | |
+ '</td><td>' + data[index][0]['StopPointDesignation'] | |
+ '</td><td>' + data[index][0]['LineNumber'] | |
+ '</td><td>' + data[index][0]['Destination'] | |
+ '</td><td class="time">' + data[index][0]['DisplayTime']; | |
try | |
{ | |
utdata = utdata + '</td><td class="time">' + data[index][1]['DisplayTime']; | |
} | |
catch (e) {utdata = utdata + '</td><td> ';} | |
try | |
{ | |
utdata = utdata + '</td><td class="time">' + data[index][2]['DisplayTime']; | |
} | |
catch (e) {utdata = utdata + '</td><td> ';} | |
try | |
{ | |
utdata = utdata + '</td><td class="time">' + data[index][3]['DisplayTime']; | |
} | |
catch (e) {utdata = utdata + '</td><td> ';} | |
utdata = utdata + '</td></tr>'; | |
if(rowclass == 'rowa') | |
{ | |
rowclass = 'rowb' | |
} | |
else{ | |
rowclass = 'rowa' | |
} | |
} | |
document.getElementById("myDiv").innerHTML=utdata; | |
} | |
} | |
xmlhttp.open("GET","departures.php",true); | |
xmlhttp.send(); | |
} | |
</script> | |
</head> | |
<body onload="getData();window.setInterval(function(){getData();},120000);"> | |
<table id="myDiv"><tr><th>Hämtar avgångar från sl...</th></tr></table> | |
</body> | |
</html> | |
## File SL.key must contain the SL key from trafiklab. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment