1.0.3 • Published 9 years ago
traintrack v1.0.3
TrainTrack
Simple node.js module for scraping and parsing SydneyTrains timetable data for analysis.
Install
$ npm install traintrack
Usage
var tt = require('traintrack');
Example
This example code extracts timetable data for the "North Shore & Northern Line", in the "Berowra to Parramatta"-direction, and then converts it into CSV format.
tt.getBody('ns', 0, function(html){
tt.parseBody(html, function(data){
tt.toCSV(data, function(string){
console.log(string);
});
});
});
Timetables
Currently supported timetables for querying.
Code | Line | Timetable |
---|---|---|
ns | T1 | North Shore & Northern Line |
nt | T1 | Northern Line |
wt | T1 | Western Line |
eh | T2 | Airport Line |
iw | T2 | Inner West & South Line |
bk | T3 | Bankstown Line |
il | T4 | Eastern Suburbs & Illawarra Line |
cu | T5 | Cumberland Line |
cl | T6 | Carlingford Line |
op | T7 | Olympic Park Line |
Data Format
- All stations are enumerated from
stations.json
. - The data object holds two arrays, stations and times, respectively.
- Times are stored as rows per station, each row is equal in length.
stations | times |
---|---|
s[0] | t[0][0] t[0][1] t[0][2] ... |
s[1] | t[1][0] t[1][1] t[1][2] ... |
s[2] | t[2][0] t[2][1] t[2][2] ... |
s[3] | t[3][0] t[3][1] t[3][2] ... |
{
// array of stations
s: [ "Station1", "Station2", ... ]
// multidimensional array of times
t: [ [ "00:00", "00:10", ... ], ... ]
}
API Reference
Retrieve a Timetable
- line (
string
): Code of timetable to retrieve (refer to above table.) - direction (
integer
): 0 or 1 for timetable train direction. - callback (
function(html)
): Callback returns html body throughhtml
parameter.
tt.getBody(line, direction, callback(html));
Parse the HTML
- html (
string
): HTML body of timetable source.. - callback (
function(data)
): Callback returns object of parsed timetable source throughdata
parameter.
tt.parseBody(html, callback(data));
Output as CSV
- data (
object
): Data object returned from HTML parsing. - callback (
function(data)
): Callback returns the CSV encoded string of object throughstring
parameter.
tt.toCSV(data, callback(string));