summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsa Venton <asav1410@gmail.com>2019-10-18 21:07:50 +0100
committerAsa Venton <asav1410@gmail.com>2019-10-18 21:07:50 +0100
commitbf914a5ed0a5173dea527aa0dc319ae3425018a3 (patch)
tree947b4bb9e45429a47ba46edb1fb11daea1d8e07a
initial commit
-rw-r--r--README.md0
-rwxr-xr-xgetjson.sh25
-rw-r--r--index.php146
3 files changed, 171 insertions, 0 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/README.md
diff --git a/getjson.sh b/getjson.sh
new file mode 100755
index 0000000..3ff6080
--- /dev/null
+++ b/getjson.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+if [ ! -f ./timetable_full.json ]; then
+ curl https://admin.libertybus.je/cache/timetables/timetable_full.json > ./timetable_full.json
+ md5sum ./timetable_full.json | cut -f1 -d ' ' > timetable_full.json.md5hash.txt
+else
+ curl https://admin.libertybus.je/cache/timetables/timetable_full.json > ./timetable_full.new.json
+ newHash=`md5sum timetable_full.new.json | cut -f1 -d ' '`
+ if [ $newHash != ./timetable_full.json.md5hash.txt ]; then
+ rm ./timetable_full.json && rm ./timetable_full.json.md5hash.txt
+ mv ./timetable_full.new.json ./timetable_full.json
+ echo "$newHash" > ./timetable_full.json.md5hash.txt
+ fi
+fi
+if [ ! -f ./service_updates.json ]; then
+ curl https://admin.libertybus.je/api/v1/service_updates > ./service_updates.json
+ md5sum ./service_updates.json | cut -f1 -d ' ' > service_updates.json.md5hash.txt
+else
+ curl https://admin.libertybus.je/api/v1/service_updates > ./service_updates.new.json
+ newHash=`md5sum service_updates.new.json | cut -f1 -d ' '`
+ if [ $newHash != ./service_updates.json.md5hash.txt ]; then
+ rm ./service_updates.json && rm ./service_updates.json.md5hash.txt
+ mv ./service_updates.new.json ./service_updates.json
+ echo "$newHash" > ./service_updates.json.md5hash.txt
+ fi
+fi
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..13f03af
--- /dev/null
+++ b/index.php
@@ -0,0 +1,146 @@
+<!DOCTYPE HTML>
+<html>
+
+<head>
+<title>bus.of.je</title>
+</head>
+
+<style>
+table {
+ border-collapse: collapse;
+}
+tr:nth-child(even) {background-color: #f2f2f2;}
+</style>
+
+<body>
+<h2>bus.of.je | Liberty Bus Timetables</h2>
+
+<?php
+
+// TODO: tidy up HTML creation
+// TODO: make it pretty
+
+ini_set('display_errors', 1);
+error_reporting(~0);
+
+function getData($type, $route, $direction, $day) {
+ $timetables = json_decode(file_get_contents("timetable_full.json"), true);
+ switch ($type){
+ case "routes":
+ $result = $timetables['timetables'];
+ break;
+ case "trips":
+ $result = $timetables['timetables'][$route]['timetable'][$direction][$day]['trips'];
+ break;
+ case "stoplist":
+ $result = $timetables['timetables'][$route]['timetable'][$direction]['stops_list'];
+ break;
+ case "schedule":
+ $result = $timetables['timetables'][$route];
+ break;
+ case "meta":
+ $result = $timetables['meta'];
+ break;
+ case "service":
+ $result = json_decode(file_get_contents("service_updates.json"), true);
+ default:
+ break;
+ }
+ if (!$result) {
+ $result = False;
+ }
+ return $result;
+}
+
+$servUpdates = getData('service', false, false, false);
+
+if (isset($_GET['route'])) {
+ $route = $_GET['route'];
+ $days = array('mon_fri', 'sat', 'sun');
+ $directions = array('outbound', 'inbound');
+ $schedule = getData('schedule', $route, false, false);
+ $routeUpdates = array();
+ echo '<h3>'. $schedule['service_number'] . ' - ' . $schedule['service_name'] . '</h3>';
+
+ // print service updates for current timetable
+ foreach ($servUpdates as $update) {
+ if(in_array($route, $update['services'])) {
+ array_push($routeUpdates, $update['content']);
+ }
+ }
+ if (!empty($routeUpdates)) {
+ echo '<h4> Service Updates:</h4>';
+ foreach ($routeUpdates as $update) {
+ echo $update . '<br>';
+ }
+ }
+ // print timetables
+ foreach ($days as $day) {
+ if ($schedule['schedule'][$day] == 'true') {
+ // set display friendly variables for days
+ switch ($day){
+ case "mon_fri":
+ $displayDay = "Mon - Fri";
+ break;
+ case "sat":
+ $displayDay = "Saturday";
+ break;
+ case "sun":
+ $displayDay = "Sunday";
+ break;
+ default:
+ break;
+ }
+ foreach ($directions as $direction) {
+ if ($schedule['directions'][$direction] == 'true') {
+ $stopList = getData('stoplist', $route, $direction, false);
+ // print timetable info while setting display friendly case for directions
+ // TODO: add linebreaks between end of table and next table title
+ echo '<p><br><b>' . ucfirst($direction) . ' | ' . $displayDay . '</b>';
+ echo '<table align="left" border="1" cellpadding="3" width="100%">';
+ $trips = getData('trips', $route, $direction, $day);
+ // TODO: sort stopList by stopList['order']
+ // TODO: must be a more efficient way to search for $rowStop['stop_name'] in $trip?
+ foreach ($stopList as $rowStop) {
+ echo '<tr><td><b>' . $rowStop['stop_name'] . '</b></td>';
+ foreach ($trips as $trip) {
+ $found=False;
+ foreach ($trip['stops'] as $stop) {
+ if ($stop['stop_name'] == $rowStop['stop_name']) {
+ echo '<td>' . $stop['departure_time'] . '</td>';
+ $found=True;
+ }
+ }
+ // print something to show bus doesn't use this stop on this trip e.g. route variation
+ if ($found != True) {
+ echo '<td>----</td>';
+ }
+ }
+ echo '</tr>';
+ }
+ echo '</table><br><br></p>';
+ }
+ }
+ }
+ }
+}
+// print landing page if no route is selected
+else {
+ $routes = getData('routes', false, false, false);
+ $meta = getData('meta', false, false, false);
+ echo '<h3>' . $meta['name']. ' | ' . $meta['from'] . ' - ' . $meta['to'] . '</h3>';
+ echo '<h4>Service Updates:</h4>\n<p>';
+ foreach ($servUpdates as $update) {
+ echo $update['content'] . '- <b>Affected Routes: </b>' . implode(", ",$update['services']) . '<br><br>';
+ }
+ foreach ($routes as $route) {
+ echo '<h4><a href="index.php?route=' . $route['service_number'] . '">' . $route['service_number'] . ' - ' . $route['service_name'] . '</a></h4>';
+ }
+ echo '</p>';
+}
+?>
+
+</p>
+</body>
+
+</html>