Browse files

Initial version

  • Loading branch information...
0 parents commit f68ffae6d4e8125e581e29c25a3b7a2f87ba322b @rnelson committed Jan 4, 2012
2 .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+Thumbs.db
BIN images/bus-large.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN images/bus-small.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 include/cities/lnk.php
@@ -0,0 +1,165 @@
+<?php
+
+function get_data($route='53') {
+ // OTvia2 -- invalid JSON
+ //$url = 'http://getonboard.lincoln.ne.gov/packet/json/vehicle?routes=203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219&lastVehicleHttpRequestTime=' . time();
+
+ // nko2/dinosaur-attack
+ $url = 'http://elysium.pretendamazing.org:5309';
+
+ // Request the stringified JSON
+ $data = @json_decode(file_get_contents($url), true);
+
+ if (NULL == $data) {
+ return array();
+ }
+
+ // If we got a response, get the data we care about
+ $json = '';
+ foreach ($data as $key => $value) {
+ if (is_array($value)) {
+ $data = $value;
+ break;
+ }
+ }
+
+ /*
+ // temp data
+ $data = array(
+ array (
+ 'vehicle' =>
+ array (
+ 'routeID' => 53,
+ 'id' => 206,
+ 'oos' => false,
+ 'update' => true,
+ 'WebLabel' => '<div class=\'labelVehicleHeader\'>53 SOUTHPOINTE</div><div class=\'labelCloseIcon\'></div><div class=\'labelVehicleCurrentRow\'>Current Stop&nbsp;&nbsp;22ND & VAN DORN-INBOUND</div><div class=\'labelVehicleCurrentRow\'>ETA&nbsp;&nbsp;ARRIVED <span class=\'bold\'>(NOW)</span></div><div class=\'clear\'></div><div class=\'labelVehicleNextRow\'>Next Stop&nbsp;&nbsp;STATE OFFICE BLDG-RT 53 IB</div><div class=\'labelVehicleNextRow\'>ETA&nbsp;&nbsp;&nbsp;7:01 PM Jan&nbsp;&nbsp;4th <span class=\'bold\' id=\'vehiclePred2061323512012115\'></span></div><div class=\'clear\'></div>',
+ 'PredictionTimes' =>
+ array (
+ 'vehiclePred2061323512012115' => 1325725260000,
+ ),
+ 'CVLocation' =>
+ array (
+ 'vehicleID' => 206,
+ 'latitude' => 4078517,
+ 'longitude' => -9668968,
+ 'angle' => 2,
+ 'locStatus' => 1,
+ 'locTime' => 1325702504,
+ 'speed' => 0,
+ 'bValidSanp' => false,
+ 'snapLat' => -1207938642,
+ 'snapLong' => 20,
+ 'snapAngle' => 0,
+ ),
+ ),
+ ), array (
+ 'vehicle' =>
+ array (
+ 'routeID' => 53,
+ 'id' => 201,
+ 'oos' => false,
+ 'update' => true,
+ 'WebLabel' => '<div class=\'labelVehicleHeader\'>53 SOUTHPOINTE</div><div class=\'labelCloseIcon\'></div><div class=\'labelVehicleCurrentRow\'>Next Stop&nbsp;&nbsp;SOUTHPOINTE</div><div class=\'labelVehicleCurrentRow\'>ETA&nbsp;&nbsp;&nbsp;6:46 PM Jan&nbsp;&nbsp;4th <span class=\'bold\' id=\'vehiclePred2011323512012116\'></span></div><div class=\'clear\'></div>',
+ 'PredictionTimes' =>
+ array (
+ 'vehiclePred2011323512012116' => 1325724360000,
+ ),
+ 'CVLocation' =>
+ array (
+ 'vehicleID' => 201,
+ 'latitude' => 4074845,
+ 'longitude' => -9666350,
+ 'angle' => 180,
+ 'locStatus' => 1,
+ 'locTime' => 1325702510,
+ 'speed' => 41,
+ 'bValidSanp' => false,
+ 'snapLat' => -1258270302,
+ 'snapLong' => 20,
+ 'snapAngle' => 0,
+ ),
+ ),
+ )
+ );
+ */
+
+ // Find the actual buses in that mess
+ $buses = array();
+ foreach ($data as $k => $v) {
+ if (isset($v['vehicle'])) {
+ if (isset($v['vehicle']['routeID']) && $route == $v['vehicle']['routeID']) {
+ $buses[] = $v;
+ }
+ }
+ }
+
+ $routes = array();
+ if (count($buses) > 0) {
+ foreach ($buses as $vehicle => $bRoute) {
+ foreach ($bRoute as $junk => $aRoute) {
+ // Create our new route, save the ID, default our label to just the ID
+ $newRoute = array();
+ $newRoute['buses'] = array();
+
+ $newRoute['id'] = $aRoute['routeID'];
+ $newRoute['label'] = $aRoute['routeID'];
+
+ if (isset($aRoute['CVLocation']) && count($aRoute['CVLocation']) > 0) {
+ foreach ($aRoute['CVLocation'] as $vehicle) {
+ $bus = array();
+ $bus['lat'] = intval($aRoute['CVLocation']['latitude']) * 10E-6;
+ $bus['lon'] = intval($aRoute['CVLocation']['longitude']) * 10E-6;
+
+ // Grab the WebLabel text and toss it into an object we can use
+ $webLabelText = str_replace("'", '"', $aRoute['WebLabel']);
+ $dom = new DOMDocument();
+ @$dom->loadHTML($webLabelText);
+ $xpath = new DOMXPath($dom);
+
+ // Get the vehicle ID
+ $bus['vid'] = $vehicle['vehicleID'];
+
+ // Get the vehicle header (number and route name)
+ $vehicleHeaders = $xpath->query("//*[contains(@class,'labelVehicleHeader')]");
+ foreach ($vehicleHeaders as $vehicleHeader) {
+ $newRoute['label'] = getTextFromNode($vehicleHeader);
+ }
+
+ // Get the hover text
+ $bus['hover'] = '';
+ /*
+ $currentRows = $xpath->query("//*[contains(@class,'labelVehicleCurrentRow')]");
+ $nextRows = $xpath->query("//*[contains(@class,'labelVehicleNextRow')]");
+ $bus['hover'] = '';
+ foreach ($currentRows as $currentRow) {
+ $bus['hover'] .= getTextFromNode($currentRow) . "<br>\n";
+ }
+ foreach ($nextRows as $nextRow) {
+ $bus['hover'] .= getTextFromNode($nextRow) . "<br>\n";
+ }
+ */
+
+ $bus['mapurl'] = buildStaticMapUrl($bus['lat'], $bus['lon']);
+
+ // Add the bus; TODO: why am I getting duplicates and needing to do this?
+ $add = true;
+ foreach ($newRoute['buses'] as $aBus) {
+ if ($aBus['mapurl'] == $bus['mapurl']) {
+ $add = false;
+ break;
+ }
+ }
+ if ($add) {
+ $newRoute['buses'][] = $bus;
+ }
+ }
+ }
+ }
+
+ $routes[] = $newRoute;
+ }
+ }
+
+ return $routes;
+}
71 include/dinosaur-attack.js
@@ -0,0 +1,71 @@
+var sys = require("util"),
+ http = require("http"),
+ url = require("url"),
+ path = require("path"),
+ fs = require("fs"),
+ events = require("events");
+
+function load_static_file(uri, response) {
+ var filename = path.join(process.cwd(), uri);
+ path.exists(filename, function(exists) {
+ if(!exists) {
+ response.sendHeader(404, {"Content-Type": "text/plain"});
+ response.write("404 Not Found\n");
+ response.close();
+ return;
+ }
+
+ fs.readFile(filename, "binary", function(err, file) {
+ if(err) {
+ response.sendHeader(500, {"Content-Type": "text/plain"});
+ response.write(err + "\n");
+ response.close();
+ return;
+ }
+
+ response.sendHeader(200);
+ response.write(file, "binary");
+ response.close();
+ });
+ });
+}
+
+var OTVIAHOST = "getonboard.lincoln.ne.gov";
+var OTVIAPATH = "/packet/json/vehicle?routes=203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219";
+
+var gob_client = http.createClient(80, OTVIAHOST);
+var last_req = Math.round((new Date()).getTime());
+var gob_emitter = new events.EventEmitter();
+var latest_json = '{}';
+
+function get_buses() {
+ var request = gob_client.request("GET", OTVIAPATH + "&lastVehicleHttpRequestTime="+last_req.toString(), {"host": OTVIAHOST});
+ last_req = Math.round((new Date()).getTime());
+ request.addListener("response", function(response) {
+ var body = "";
+ response.addListener("data", function(data) {
+ body += data;
+ });
+
+ response.addListener("end", function() {
+ eval("var buses = " + body);
+ sys.puts(buses)
+ if(!(buses === undefined)) {
+ gob_emitter.emit("buses", buses);
+ }
+ });
+ });
+
+ request.end();
+}
+
+setInterval(get_buses, 15000);
+var listener = gob_emitter.addListener("buses", function(buses) {
+ //sys.puts(JSON.stringify(buses));
+ latest_json = JSON.stringify(buses);
+ });
+http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'application/javascript'});
+ res.end(latest_json)
+}).listen(5309, "0.0.0.0");
+sys.puts("Server running at http://0.0.0.0:5309/");
34 include/googlemaps.php
@@ -0,0 +1,34 @@
+<?php
+
+define('GMAP_STATIC_MAP_BASE', 'https://maps.googleapis.com/maps/api/staticmap');
+define('GMAP_CUSTOM_MARKER_URL', 'http://pretendamazing.org/tmp/bus-small.jpg'); // TODO: get this on wtfmb.tractorfeed.org
+
+// TODO: what size is sane?
+function buildStaticMapUrl($lat, $lon, $size='200x200', $zoom=17) {
+ $url = GMAP_STATIC_MAP_BASE . '?';
+
+ $location = $lat . ',' . $lon;
+
+ // The point to have as the center of our map
+ $url .= 'center=' . $location . '&';
+
+ // Zoom level
+ $url .= 'zoom=' . $zoom . '&';
+
+ // The size of the map
+ $url .= 'size=' . $size . '&';
+
+ // Use JPEG since everything can read it
+ $url .= 'format=jpg&';
+
+ // Roads are what we probably care about
+ $url .= 'maptype=roadmap&';
+
+ // Add a marker for the bus
+ $url .= 'markers=icon:' . urlencode(GMAP_CUSTOM_MARKER_URL) . '%7C' . $location . '&';
+
+ // We're not acquiring the user's location for the map, so this is false
+ $url .= 'sensor=false';
+
+ return $url;
+}
17 include/util.php
@@ -0,0 +1,17 @@
+<?php
+
+// http://www.php.net/manual/en/class.domelement.php#93340
+function getTextFromNode($Node, $Text = "") {
+ if ($Node->tagName == null)
+ return $Text.$Node->textContent;
+
+ $Node = $Node->firstChild;
+ if ($Node != null)
+ $Text = getTextFromNode($Node, $Text);
+
+ while($Node->nextSibling != null) {
+ $Text = getTextFromNode($Node->nextSibling, $Text);
+ $Node = $Node->nextSibling;
+ }
+ return $Text;
+}
65 index.php
@@ -0,0 +1,65 @@
+<?php
+
+ require_once('include/googlemaps.php');
+ require_once('include/util.php');
+
+ // See if we got the city and route from the user. If nothing was specified, we'll default
+ // to showing all routes in the default city. And because we only support Lincoln, NE right
+ // now, that'll be our default.
+ $city = 'lnk';
+ $route = '53';
+
+ // Get route and/or city if the user specified them
+ if (isset($_GET['city']) && strlen($_GET['city'] > 0)) {
+ $city = $_GET['city'];
+ }
+ if (isset($_GET['route'])) {
+ $route = $_GET['route'];
+ }
+
+ // Get the data
+ require_once('include/cities/' . $city . '.php');
+ $routes = get_data($route);
+
+ // Build our page title
+ $pageTitle = 'WTFMB - ' . $city;
+ if (strlen($route)) {
+ $pageTitle .= ' - ' . $route;
+ }
+
+?>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+ <head>
+ <title><?php echo $pageTitle; ?></title>
+ </head>
+ <body>
+ <center>
+<?php
+ if (count($routes) > 0) {
+ foreach ($routes as $route) {
+?>
+ <h1><?php echo $route['label']; ?></h1>
+<?php
+ foreach ($route['buses'] as $bus) {
+?>
+ <p>
+ <img src="<?php echo $bus['mapurl']; ?>" alt="<?php echo $bus['hover']; ?>"><br>
+ <?php echo $bus['hover']; ?>
+ </p>
+<?php
+ }
+ }
+ }
+ else {
+?>
+ <p>Sorry! No buses on route <?php echo $route; ?> are currently running.</p>
+<?php
+ }
+?>
+ <p>
+ <font size="-2">Where the #@&*'s My Bus?, a <a href="http://tractorfeed.org">Tractor Feed</a> project</font>
+ </p>
+ </center>
+ </body>
+</html>

0 comments on commit f68ffae

Please sign in to comment.