timetable
Version:
Using GTFS and other sources to produce beautiful and usable timetables for public transport.
412 lines (372 loc) • 12 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var $, $_XXX_show, $filter, $read_trips_from_route, P, REGISTRY, TRM, alert, badge, debug, echo, help, info, log, new_lte, options, rainbow, rpr, urge, warn, whisper;
TRM = require('coffeenode-trm');
rpr = TRM.rpr.bind(TRM);
badge = 'TIMETABLE/main';
log = TRM.get_logger('plain', badge);
info = TRM.get_logger('info', badge);
whisper = TRM.get_logger('whisper', badge);
alert = TRM.get_logger('alert', badge);
debug = TRM.get_logger('debug', badge);
warn = TRM.get_logger('warn', badge);
help = TRM.get_logger('help', badge);
urge = TRM.get_logger('urge', badge);
echo = TRM.echo.bind(TRM);
rainbow = TRM.rainbow.bind(TRM);
REGISTRY = require('../REGISTRY');
options = require('../../options');
P = require('pipedreams');
$ = P.$.bind(P);
new_lte = function(text) {
var R, length;
length = Buffer.byteLength(text);
R = new Buffer(1 + length);
R.write(text);
R[length] = 0xff;
return R;
};
"\n# realm / type / ID\n\n %|gtfs/routes|gtfs/agency/0NV___|11'\n %|gtfs/stop_times|gtfs/stops/9001103|59'\n %|gtfs/stop_times|gtfs/trips/100288|140'\n %|gtfs/trips|gtfs/routes/1001|124813'\n %|gtfs/trips|gtfs/service/000000|105962'\n\n % | gtfs/routes | gtfs/agency/BVB--- | 330\n type ref-id id\n\n % | gtfs/routes | gtfs/agency/BVB--- | 330\n type ref-id id\n\n\n %|gtfs/routes|name:S5|549\n";
this.show_routes_of_agency = function(db, agency_id, handler) {
var input, key, query;
help('show_routes_of_agency');
key = "%|gtfs/routes|gtfs/agency/" + agency_id;
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
help(query);
return input = (db.createReadStream(query)).pipe($(function(record, handler) {
return handler(null, record);
})).pipe(P.$show()).pipe(P.$on_end(function() {
return handler(null, db);
}));
};
$filter = function(f) {
return $(function(record, handler) {
var error;
try {
if (!f(record)) {
return handler();
}
} catch (_error) {
error = _error;
return handler(error);
}
return handler(null, record);
});
};
this.show_subway_routes = function(db, handler) {
var input, key, query, seen_stops;
help('show_subway_routes');
seen_stops = {};
/* TAINT must escape name */
key = "%|gtfs/routes|name:U";
key = "gtfs/routes/";
query = {
'gte': key,
'lte': new_lte(key),
'keys': false,
'values': true
};
help(key);
return input = (db.createReadStream(query)).pipe($filter(function(record) {
var name;
if (/^U/.test(name = record['name'])) {
return true;
}
return false;
})).pipe(P.$show());
};
this.show_db_sample = function(db, handler) {
return db.createReadStream({
gt: new_lte('%')
}).pipe(P.$sample(1 / 1e5, {
seed: 5
})).pipe($(function(_arg, handler) {
var key, value;
key = _arg.key, value = _arg.value;
if (value === true) {
return handler(null, key);
}
return handler(null, value);
})).pipe(P.$show()).on('end', function() {
return handler(null, null);
});
};
this.show_stops_of_route = function(db, route_name, handler) {
var input, key, query, seen_stops;
help('show_stops_of_route');
seen_stops = {};
/* TAINT must escape name */
key = "%|gtfs/routes|name:" + route_name + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
whisper(key);
return input = (db.createReadStream(query)).pipe($(function(record, handler) {
var route_id, _ref;
_ref = record.split('|'), route_id = _ref[_ref.length - 1];
key = "%|gtfs/trips|gtfs/routes/" + route_id + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
whisper(key);
return input = (db.createReadStream(query)).pipe($(function(record) {
var trip_id, _ref1;
_ref1 = record.split('|'), trip_id = _ref1[_ref1.length - 1];
key = "%|gtfs/stop_times|gtfs/trips/" + trip_id + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
return input = (db.createReadStream(query)).pipe($(function(record) {
var id, stoptime_key, type, _, _ref2;
_ref2 = record.split('|'), _ = _ref2[0], type = _ref2[1], _ = _ref2[2], id = _ref2[3];
stoptime_key = "" + type + "/" + id;
return db.get(stoptime_key, function(error, record) {
var stop_key;
if (error != null) {
return handler(error);
}
stop_key = "gtfs/stops/" + record['gtfs-stops-id'];
return db.get(stop_key, function(error, record) {
var name;
if (error != null) {
return handler(error);
}
name = record['name'];
if (name in seen_stops) {
return;
}
seen_stops[name] = 1;
return help(name);
});
});
}));
}));
}));
};
$_XXX_show = function(label) {
return $(function(record, handler) {
log(TRM.grey(label), TRM.lime(record));
return handler(null, record);
});
};
$read_trips_from_route = function(db) {
help('read_trips_from_route');
return P.through(function(record_1) {
var count, emit, key, query, route_id, _ref;
_ref = record_1.split('|'), route_id = _ref[_ref.length - 1];
key = "%|gtfs/trips|gtfs/routes/" + route_id + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
whisper(key);
emit = this.emit.bind(this);
count = 0;
return db.createReadStream(query).pipe(P.through(function(record_2) {
emit('data', record_2);
return count += 1;
}, function() {
return whisper(count);
}));
});
};
this.$read_trips_from_route = function(db) {
help('read_trips_from_route');
return P.through(function(record_1) {
var count, emit, key, query, route_id, _ref;
_ref = record_1.split('|'), route_id = _ref[_ref.length - 1];
key = "%|gtfs/trips|gtfs/routes/" + route_id + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
whisper(key);
emit = this.emit.bind(this);
count = 0;
return db.createReadStream(query).pipe(P.through(function(record_2) {
emit('data', record_2);
return count += 1;
}, function() {
return whisper(count);
}));
});
};
this.read_routes_id_from_name = function(db, route_name, handler) {
var key, query;
help('read_routes_id_from_name');
/* TAINT must escape name */
key = "%|gtfs/routes|name:" + route_name;
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
whisper(key);
return db.createReadStream(query).on('data', function(record) {
var Z, facet, id, name, record_id, _, _ref, _ref1;
_ref = record.split('|'), _ = _ref[0], _ = _ref[1], facet = _ref[2], id = _ref[3];
_ref1 = facet.split(':'), _ = _ref1[0], name = _ref1[1];
record_id = "gtfs/routes/" + id;
Z = {
'id': record_id
};
Z['name'] = name;
return handler(null, Z);
}).on('end', function() {
return handler(null, null);
});
};
this.read_trips_id_from_route_id = function(db, route_id, handler) {
var key, query;
help('read_trips_id_from_route_id');
/* TAINT must escape name */
key = "%|gtfs/trips|" + route_id + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
return db.createReadStream(query).on('data', function(record) {
var Z, gtfs_type, id, record_id, _, _ref;
_ref = record.split('|'), _ = _ref[0], gtfs_type = _ref[1], _ = _ref[2], id = _ref[3];
record_id = "" + gtfs_type + "/" + id;
Z = {
'id': record_id
};
return handler(null, Z);
}).on('end', function() {
return handler(null, null);
});
};
this.read_stoptimes_id_from_trips_id = function(db, trip_id, handler) {
var key, query;
help('read_stoptimes_id_from_trips_id');
key = "%|gtfs/stop_times|" + trip_id + "|";
query = {
'gte': key,
'lte': new_lte(key),
'keys': true,
'values': false
};
return db.createReadStream(query).on('data', function(record) {
var Z, gtfs_type, id, record_id, _, _ref;
_ref = record.split('|'), _ = _ref[0], gtfs_type = _ref[1], _ = _ref[2], id = _ref[3];
record_id = "" + gtfs_type + "/" + id;
Z = {
'id': record_id
};
return handler(null, Z);
}).on('end', function() {
return handler(null, null);
});
};
this.f = function(db, route_name, handler) {
return this.read_routes_id_from_name(db, route_name, (function(_this) {
return function(error, record) {
var name, route_id;
if (error != null) {
return handler(error);
}
if (record === null) {
return;
}
route_id = record.id, name = record.name;
whisper('1', route_id, name);
return _this.read_trips_id_from_route_id(db, route_id, function(error, record) {
var trip_id;
if (error != null) {
return handler(error);
}
if (record === null) {
return;
}
trip_id = record['id'];
return _this.read_stoptimes_id_from_trips_id(db, trip_id, function(error, record) {
var stoptimes_id;
if (error != null) {
return handler(error);
}
if (record === null) {
return;
}
stoptimes_id = record['id'];
return db.get(stoptimes_id, function(error, record) {
if (error != null) {
return handler(error);
}
return debug(record);
});
});
});
};
})(this));
};
this.show_unique_indexes = function(db, handler) {
var count, input, query, seen;
help('show_unique_indexes');
seen = {};
count = 0;
query = {
'gte': '%|gtfs',
'lte': new_lte('%|gtfs'),
'keys': true,
'values': false
};
help(query);
return input = (db.createReadStream(query)).pipe($(function(record, handler) {
count += 1;
return handler(null, record);
})).pipe($(function(record, handler) {
var gtfs_id, gtfs_ref_id, gtfs_ref_type, gtfs_type, key, _, _ref;
_ref = record.split('|'), _ = _ref[0], gtfs_type = _ref[1], gtfs_ref_id = _ref[2], gtfs_id = _ref[3];
gtfs_ref_type = gtfs_ref_id.replace(/[\/:][^\/:]+$/, '');
key = gtfs_type + '|' + gtfs_ref_type;
if (key in seen) {
return handler();
}
seen[key] = 1;
return handler(null, record);
})).pipe(P.$show()).pipe(P.$on_end(function() {
help(count, "records in DB");
return handler(null, db);
}));
};
this.main = function(handler) {
var registry;
registry = REGISTRY.new_registry();
this.f(registry, 'U1', function(error, record) {
return debug(record);
});
return null;
};
if (module.parent == null) {
this.main((function(_this) {
return function(error, registry) {
if (error != null) {
throw error;
}
return help('ok');
};
})(this));
}
}).call(this);