Skip to content

Instantly share code, notes, and snippets.

@echoz
Last active December 19, 2015 18:29
Show Gist options
  • Save echoz/5999143 to your computer and use it in GitHub Desktop.
Save echoz/5999143 to your computer and use it in GitHub Desktop.
NTU Campus bus parser
function versionString() {
return "1.0";
}
function parseURL() {
return "http://campusbus.ntu.edu.sg/ntubus";
}
function headers() {
return {
"User_Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1",
"Accept" : "text/html",
"Accept-Encoding" : "gzip",
"Accept-Language" : "en-us"
};
}
function stripHTML(html) {
var div = document.createElement("div");
div.innerHTML = html;
return div.textContent || div.innerText || "";
}
function dictifyBusStop(busstop) {
if (null == busstop || 'object' != typeof busstop) throw TypeError;
var copy = {};
for (var attr in busstop) {
if (attr == 'marker') continue;
if (attr == 'markerShadow') continue;
copy[attr] = busstop[attr];
}
return copy;
}
function dictifyBusRoute(route) {
if (null == route || 'object' != typeof route) throw TypeError;
// var copy = {};
// for (var attr in route) {
// copy[attr] = route[attr];
// }
// return copy;
return route;
}
function dictifyBusDevice(busdevice) {
if (null == busdevice || 'object' != typeof busdevice) throw TypeError;
var copy = {};
for (var attr in busdevice) {
if (attr == 'marker') continue;
if (attr == 'plotter') continue;
if (attr == 'info1') {
copy[attr] = stripHTML(busdevice[attr]);
continue;
}
if (attr == 'info2') {
copy[attr] = stripHTML(busdevice[attr]);
continue;
}
copy[attr] = busdevice[attr];
}
return copy;
}
function parseBusRoutes() {
var routes = [];
for (var i=0;i<ntu.routes.length;i++) {
try {
routes.push(dictifyBusRoute(ntu.routes[i]));
} catch (e) {
}
}
return routes;
}
function parseBusStops() {
var busStops = [];
for (var i=0;i<ntu.busStop.length;i++) {
try {
busStops.push(dictifyBusStop(ntu.busStop[i]));
} catch (e) {
}
}
return busStops;
}
function parseBusDevices() {
var busDevices = [];
for (var i=0;i<ntu.busDevice.length;i++) {
try {
busDevices.push(dictifyBusDevice(ntu.busDevice[i]));
} catch (e) {
}
}
return busDevices;
}
function parse() {
return "{\"busStops\" : " + JSON.stringify(parseBusStops()) + ", \"busDevices\" : " + JSON.stringify(parseBusDevices()) + ", \"routes\" : " + JSON.stringify(parseBusRoutes()) + "}";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment