Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 5.0.0 #23

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
.idea
coverage/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ node_js:
notifications:
email: false

after_script: NODE_ENV=test istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
after_script: cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# dvbjs

[![travis-ci](http://img.shields.io/travis/kiliankoe/dvbjs.svg?style=flat)](https://travis-ci.org/kiliankoe/dvbjs)
[![Coverage Status](https://coveralls.io/repos/kiliankoe/dvbjs/badge.svg?branch=master&service=github)](https://coveralls.io/github/kiliankoe/dvbjs?branch=master)
[![npmversion](http://img.shields.io/npm/v/dvbjs.svg?style=flat)](https://www.npmjs.org/package/dvbjs)
[![travis-ci](http://img.shields.io/travis/kiliankoe/dvbjs.svg?style=flat)](https://travis-ci.org/kiliankoe/dvbjs) [![Coverage Status](https://coveralls.io/repos/kiliankoe/dvbjs/badge.svg?branch=master&service=github)](https://coveralls.io/github/kiliankoe/dvbjs?branch=master) [![npmversion](http://img.shields.io/npm/v/dvbjs.svg?style=flat)](https://www.npmjs.org/package/dvbjs)

This is an unofficial node module, giving you a few options to query Dresden's public transport system for current bus- and tramstop data.

Expand Down Expand Up @@ -176,8 +174,17 @@ var nelat = 51.04615;
var nelng = 13.71368;

var pinType = dvb.pins.type.STOP; // type of the Pins
// or
var pinType = [dvb.pins.type.STOP, dvb.pins.type.PLATFORM]; // for multiple types

// options for stops
var options = {
showLines: true, // show connections. default: true
groupByType: true, // group lines by transport type. default: false
fullLineType: true // show type name, title and icon_url or only the name. default: false
};

dvb.pins(swlat, swlng, nelat, nelng, pinType, function (err, data) {
dvb.pins(swlat, swlng, nelat, nelng, pinType, options, function (err, data) {
if (err) throw err;
console.log(JSON.stringify(data, null, 4));
});
Expand All @@ -194,14 +201,18 @@ Output:
51.043733606562675,
13.706279792263878
],
"type": "stop",
"connections": [
{
"line": "7",
"type": "1"
},
{
"line": "8",
"type": "1"
"type": {
"title": "Straßenbahn",
"name": "tram",
"icon_url": "https://www.dvb.de/assets/img/trans-icon/transport-tram.svg"
},
"lines": [
"8",
"1"
]
},
{...}
]
Expand All @@ -211,7 +222,7 @@ Output:

```

The default pin type is `STOP`, other posible types are:
The default pin type is `STOP and POI`, other posible types are:

```js
pins.type = {
Expand Down
4 changes: 3 additions & 1 deletion lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ var monitor = function monitor(stop, offset, amount, callback) {
hst: stop,
vz: offset,
lim: amount
}
},
timeout: 10000
};

var now = new Date();

return requestP(options)
.then(JSON.parse)
.map(function (transport) {
console.log(transport);
var arrivalTimeRelative = parseInt(transport[2]) || 0;
return {
line: transport[0],
Expand Down
159 changes: 152 additions & 7 deletions lib/pins.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
'use strict';

var requestP = require('request-promise');
var _ = require('lodash');
var utils = require('./utils');

var pins = function pins(swlat, swlng, nelat, nelng, pinType, callback) {
var pins = function pins(swlat, swlng, nelat, nelng, pinTypes, options, callback) {
var sw = utils.WGS84toGK4(swlat, swlng);
var ne = utils.WGS84toGK4(nelat, nelng);

var options = {
url: 'https://www.dvb.de/apps/map/pins',
if (options == null) {
options = {};
}
options.showLines = options.showLines != false;

var request_options = {
uri: 'https://www.dvb.de/apps/map/pins',
qs: {
showLines: 'true',
showLines: options.showLines,
swlat: sw[1],
swlng: sw[0],
nelat: ne[1],
nelng: ne[0],
pintypes: pinType
pintypes: pinTypes
}
};

return requestP(options)
return requestP(request_options)
.then(function (data) {
if (!data) {
return [];
Expand All @@ -30,7 +36,7 @@ var pins = function pins(swlat, swlng, nelat, nelng, pinType, callback) {
return data.slice(2, data.length - 2).split('\",\"');
})
.map(function (elem) {
return utils.parsePin(elem, pinType);
return parsePin(elem, options);
}).nodeify(callback);
};

Expand All @@ -44,4 +50,143 @@ pins.type = {
PARK_AND_RIDE: 'parkandride'
};

pins.connection_type = [
{
title: "Straßenbahn",
name: "tram",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-tram.svg"
},
{
title: "Stadtbus",
name: "citybus",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg"
},
{
title: "Regionalbus",
name: "regiobus",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg"
},
{
title: "S-Bahn",
name: "metropolitan",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-metropolitan.svg"
},
{
title: "Seilbahn",
name: "lift",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-lift.svg"
},
{
title: "Fähre",
name: "ferry",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-ferry.svg"
},
{
title: "Anrufsammeltaxi (AST)/ Rufbus",
name: "ast",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-alita.svg"
},
{
title: "Train",
name: "train",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-train.svg"
}
];

function parsePin(dataAsString, options) {
var data = dataAsString.split('|');
var coords = utils.GK4toWGS84(data[4], data[5]);
var pinType = parsePinType(data[1]);

if (pinType === 'platform') {
return {
name: data[3],
coords: coords,
platform_nr: data[6],
type: pinType
};
} else if (_.contains(['poi', 'rentabike', 'ticketmachine', 'carsharing', 'parkandride'], pinType)) {
return {
id: data[0],
name: data[3],
coords: coords,
type: pinType
};
}

// 'stop' id default

if (options.showLines) {
return {
id: data[0],
name: data[3],
coords: coords,
type: pinType,
connections: parseConnections(data[7], options)
};
} else {
return {
id: data[0],
name: data[3],
coords: coords,
type: pinType
};
}
}

function parsePinType(pinID) {
switch (pinID) {
case "t":
return pins.type.TICKET_MACHINE;
case "pf":
return pins.type.PLATFORM;
case "c":
return pins.type.CAR_SHARING;
case "r":
return pins.type.RENT_A_BIKE;
case "pr":
return pins.type.PARK_AND_RIDE;
case "p":
return pins.type.POI;
default:
return pins.type.STOP;
}
}

function parseConnections(data, options) {
var connections = [];

if (options.groupByType == true) {
connections = data.split('#').map(function (types) {
types = types.split(":");
return {
type: parseConnectionType(types[0], options),
lines: types[1].split("~").map(function (line) {
return line;
})
}
});
} else {
data.split('#').forEach(function (types) {
types = types.split(":");
connections = connections.concat(types[1].split("~").map(function (line) {
return {
line: line,
type: parseConnectionType(types[0], options)
}
}));
});
}

return connections;
}

function parseConnectionType(id, options) {
if (options.fullLineType == true) {
return pins.connection_type[id - 1]
} else {
return pins.connection_type[id - 1].name;
}
}

module.exports = pins;
25 changes: 17 additions & 8 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var requestP = require('request-promise');
var utils = require('./utils');
var moment = require('moment');
var querystring = require('querystring');

var route = function route(origin, destination, time, deparr, callback) {
origin = utils.stripSpaces(origin);
Expand All @@ -17,9 +19,7 @@ var route = function route(origin, destination, time, deparr, callback) {
month = '0' + month;
}

if (deparr !== route.DEPARTURE && deparr !== route.ARRIVAL) {
deparr = route.ARRIVAL;
}
deparr = deparr == route.DEPARTURE ? route.DEPARTURE : route.ARRIVAL;

// API docs: http://data.linz.gv.at/katalog/linz_ag/linz_ag_linien/fahrplan/LINZ_LINIEN_Schnittstelle_EFA_V1.pdf
// found here -> http://www.nise81.com/archives/2674
Expand Down Expand Up @@ -75,9 +75,9 @@ var route = function route(origin, destination, time, deparr, callback) {
};

function extractTrip(trip) {
var departure = trip.legs[0].points[0].dateTime.time;
var arrival = trip.legs[trip.legs.length - 1].points[trip.legs[trip.legs.length - 1].points.length - 1].dateTime.time;
var duration = trip.duration;
var departure = parseDateTime(trip.legs[0].points[0].dateTime);
var arrival = parseDateTime(trip.legs[trip.legs.length - 1].points[trip.legs[trip.legs.length - 1].points.length - 1].dateTime);
var duration = parseDuration(trip.duration);
var interchange = parseInt(trip.interchange);

return {
Expand All @@ -93,13 +93,13 @@ function extractTrip(trip) {

var departure = {
stop: leg.points[0].nameWO,
time: leg.points[0].dateTime.time,
time: parseDateTime(leg.points[0].dateTime),
coords: utils.convertCoordinates(leg.points[0].ref.coords)
};

var arrival = {
stop: leg.points[1].nameWO,
time: leg.points[1].dateTime.time,
time: parseDateTime(leg.points[1].dateTime),
coords: utils.convertCoordinates(leg.points[1].ref.coords)
};

Expand All @@ -115,6 +115,15 @@ function extractTrip(trip) {
};
}

function parseDateTime(dateTime) {
return moment(dateTime.date + " " + dateTime.time + "+0100", "DD.MM.YYYY HH:mmZ").toDate();
}

function parseDuration(duration) {
duration = duration.split(":");
return duration[1] + duration[0] * 60;
}

route.DEPARTURE = 'dep';
route.ARRIVAL = 'arr';

Expand Down
Loading