Skip to content

Commit

Permalink
Merge 229c535 into 1fd5ad3
Browse files Browse the repository at this point in the history
  • Loading branch information
ronhippler committed Feb 17, 2017
2 parents 1fd5ad3 + 229c535 commit cf85d72
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 112 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: LEm8hxR9pZEiSy9zY0cQvlPi4wi9LfgpV
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
coverage/
.idea/
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
language: node_js

node_js:
- "0.10"
- "0.12"
- "lts/*"
- "node"

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
script: npm run coverage

after_success: 'npm run coveralls'
46 changes: 28 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ Monitor a single stop to see every bus or tram leaving this stop after the speci
Example:

```js
var stopName = 'Helmholtzstraße'; // name of the stop
var stop = 65498388; // ID of the stop
// var stop = 'Postplatz'; // or name of the stop (must be unambiguous)
var timeOffset = 0; // how many minutes in the future, 0 for now
var numResults = 2; // number of results

dvb.monitor(stopName, timeOffset, numResults, function(err, data) {
dvb.monitor(stop, timeOffset, numResults, function(err, data) {
if (err) throw err;
console.log(JSON.stringify(data, null, 4));
});
Expand All @@ -64,29 +65,38 @@ Output:
```js
[
{
"line": "85",
"direction": "Striesen",
"arrivalTimeRelative": 12,
"arrivalTime": "2015-12-13T19:23:18.374Z",
"line": "4",
"direction": "Laubegast",
"platform": 1,
"arrivalTime": "2017-02-17T01:25:00.000Z",
"arrivalTimeRelative": 9,
"scheduledTime": "2017-02-17T01:25:00.000Z",
"scheduledTimeRelative": 9,
"delayTime": 0,
"state": "InTime",
"mode": {
title: "Stadtbus",
name: "citybus",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg"
"title": "Straßenbahn",
"name": "tram",
"icon_url": "https://www.dvb.de/assets/img/trans-icon/transport-tram.svg"
}
},
{
"line": "85",
"direction": "Löbtau Süd",
"arrivalTimeRelative": 18,
"arrivalTime": "2015-12-13T19:29:24.374Z",
"line": "2",
"direction": "Gorbitz",
"platform": 2,
"arrivalTime": "2017-02-17T01:25:00.000Z",
"arrivalTimeRelative": 9,
"scheduledTime": "2017-02-17T01:25:00.000Z",
"scheduledTimeRelative": 9,
"delayTime": 0,
"state": "InTime",
"mode": {
title: "Stadtbus",
name: "citybus",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg"
"title": "Straßenbahn",
"name": "tram",
"icon_url": "https://www.dvb.de/assets/img/trans-icon/transport-tram.svg"
}
}
]

```

### Find routes
Expand Down Expand Up @@ -286,6 +296,6 @@ Output:

## Misc

By the way, stop names in queries are very forgiving. As long as the server sees it as an unique hit, it'll work. 'Helmholtzstraße' finds the same data as 'helmholtzstrasse', 'Nürnberger Platz' as 'nuernbergerplatz' etc.
By the way, stop names in queries are very forgiving. Better use the ID of the stop. As long as the server sees it as an unique hit, it'll work. 'Helmholtzstraße' finds the same data as 'helmholtzstrasse', 'Nürnberger Platz' as 'nuernbergerplatz' etc.

One last note, be sure to use `EDUROAM=TRUE` as environment variable from inside the network of the TU Dresden.
88 changes: 65 additions & 23 deletions lib/monitor.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,81 @@
'use strict';

var find = require('./find');
var requestP = require('request-promise');
var utils = require('./utils');
var bluebird = require('bluebird');

var monitor = function monitor(stop, offset, amount, callback) {
// remove whitespace, vvo-online.de handles all kinds of requests pretty nicely
stop = utils.stripSpaces(stop);
function depatureMonitor(stopid, offset, amount) {

var now = new Date();
var time = now;
if (offset !== 'undefined' && offset !== 0)
time = new Date(now.getTime() + (offset * 60 * 1000));

// hst = name of bus- or tramstop, e.g. 'Helmholtzstraße'
// vz = time offset from now; trims the beginning of the array
// lim = limit the amount of results, max seems to be 31
// var url = 'http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do?hst=' + stop + '&vz=' + offset + '&lim=' + amount;
var options = {
url: 'http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do',
url: 'https://webapi.vvo-online.de/dm?format=json',
qs: {
hst: stop,
vz: offset,
lim: amount
stopid: stopid,
time: time.toISOString(),
isarrival: false,
limit: amount,
shorttermchanges: true,
mentzonly: false
}
};

var now = new Date();

return requestP(options)
.then(JSON.parse)
.map(function (transport) {
var arrivalTimeRelative = parseInt(transport[2]) || 0;
return {
line: transport[0],
direction: transport[1],
arrivalTimeRelative: arrivalTimeRelative,
arrivalTime: new Date(now.getTime() + 60000 * arrivalTimeRelative),
mode: utils.parseMode(transport[0])
};
}).nodeify(callback);
.then(function (data) {
// check status
if (data.Status &&
data.Status.Code == 'Ok' &&
data.Departures) {

return data.Departures.map(function (d) {
var arrivalTime = d.RealTime ? d.RealTime.match(/\d+/)[0] : d.ScheduledTime.match(/\d+/)[0];
arrivalTime = new Date(1 * arrivalTime);
var scheduledTime = new Date(1 * d.ScheduledTime.match(/\d+/)[0]);

return {
line: d.LineName,
direction: d.Direction,
platform: d.Platform ? parseInt(d.Platform.Name) : undefined,
arrivalTime: arrivalTime,
arrivalTimeRelative: Math.round((arrivalTime - now) / 6e4),
scheduledTime: scheduledTime,
scheduledTimeRelative: Math.round((scheduledTime - now) / 6e4),
delayTime: Math.round((arrivalTime - scheduledTime) / 6e4),
state: d.State ? d.State : 'Unknown',
mode: utils.parseMot(d.Mot)
};
})
}
})
}

var monitor = function monitor(stop, offset, amount, callback) {
var stopPromise;

if (typeof(stop) === 'string') {
stopPromise = find(utils.stripSpaces(stop))
.then(function (stops) {
if (!stops || stops.length == 0)
throw new Error('no stops found');
return stops[0].id;
});
} else {
stopPromise = bluebird.resolve(stop);
}

return stopPromise
.then(function (stop) {
return depatureMonitor(stop, offset, amount);
})
.catch(function () {
return [];
})
.nodeify(callback);
};

module.exports = monitor;
59 changes: 31 additions & 28 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var _ = require('lodash');
var utils = {};
var proj4 = require('proj4');

Expand Down Expand Up @@ -44,7 +43,7 @@ utils.parsePin = function parsePin(dataAsString, pinType) {
coords: coords,
platform_nr: data[6]
};
} else if (_.contains(['poi', 'rentabike', 'ticketmachine', 'carsharing', 'parkandride'], pinType)) {
} else if (pinType == 'poi' || pinType == 'rentabike' || pinType == 'ticketmachine' || pinType == 'carsharing' || pinType == 'parkandride') {
return {
id: data[0],
name: data[3],
Expand All @@ -64,26 +63,30 @@ utils.parsePin = function parsePin(dataAsString, pinType) {
utils.parseMode = function parseMode(id) {
if (parseInt(id) != undefined) {
var intID = parseInt(id);
if (between(intID, 0, 59)) return utils.modes['tram'];
if (between(intID, 60, 99)) return utils.modes['citybus'];
if (between(intID, 100, 1000)) return utils.modes['regiobus'];
if (between(intID, 0, 59)) return utils.modes.tram;
if (between(intID, 60, 99)) return utils.modes.citybus;
if (between(intID, 100, 1000)) return utils.modes.intercitybus;
}
if (id === 'SWB' || id === 'STB') return utils.modes['lift'];
if (id === 'SWB' || id === 'STB') return utils.modes.cableway;

if (matches(id, /^E\d+/)) {
var match = id.match(/^E(\d+)/);
if (match[1] <= 59) return utils.modes['tram'];
else return utils.modes['citybus'];
if (match[1] <= 59) return utils.modes.tram;
else return utils.modes.citybus;
}

if (matches(id, /^\D$|^\D\/\D$/)) return utils.modes['regiobus'];
if (matches(id, /^F/)) return utils.modes['ferry'];
if (matches(id, /^RE|^IC|^TL|^RB|^SB|^SE|^U\d/)) return utils.modes['train'];
if (matches(id, /^S/)) return utils.modes['metropolitan'];
if (matches(id, /alita/)) return utils.modes['ast'];
if (matches(id, /^\D$|^\D\/\D$/)) return utils.modes.intercitybus;
if (matches(id, /^F/)) return utils.modes.ferry;
if (matches(id, /^RE|^IC|^TL|^RB|^SB|^SE|^U\d/)) return utils.modes.train;
if (matches(id, /^S/)) return utils.modes.suburbanrailway;
if (matches(id, /alita/)) return utils.modes.hailedsharedtaxi;

return null;
}
return undefined;
};

utils.parseMot = function parseMode(name) {
return utils.modes[name.toLowerCase()];
};

utils.modes = {
'tram': {
Expand All @@ -96,35 +99,35 @@ utils.modes = {
name: "citybus",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg"
},
'regiobus': {
'intercitybus': {
title: "Regionalbus",
name: "regiobus",
name: "intercitybus",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-bus.svg"
},
'metropolitan': {
'suburbanrailway': {
title: "S-Bahn",
name: "metropolitan",
name: "SuburbanRailway",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-metropolitan.svg"
},
'lift': {
'train': {
title: "Zug",
name: "train",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-train.svg"
},
'cableway': {
title: "Seil-/Schwebebahn",
name: "lift",
name: "cableway",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-lift.svg"
},
'ferry': {
title: "Fähre",
name: "ferry",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-ferry.svg"
},
'ast': {
'hailedsharedtaxi': {
title: "Anrufsammeltaxi (AST)/ Rufbus",
name: "ast",
name: "hailedsharedtaxi",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-alita.svg"
},
'train': {
title: "Zug",
name: "train",
icon_url: "https://www.dvb.de/assets/img/trans-icon/transport-train.svg"
}
};

Expand Down
27 changes: 17 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "dvbjs",
"version": "4.2.2",
"version": "4.3.0",
"description": "query Dresden's public transport system for current bus- and tramstop data",
"main": "index",
"scripts": {
"test": "mocha",
"test-live": "NODE_ENV=test_live mocha"
"test-live": "NODE_ENV=test_live mocha",
"update-mocks": "NODE_ENV=test_update mocha",
"coverage": "node node_modules/.bin/istanbul cover _mocha -- -R spec",
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -38,6 +41,11 @@
"name": "Andreas Hippler",
"email": "andreas@tabaro.de",
"url": "https://github.com/ronhippler"
},
{
"name": "Jens Maus",
"email": "mail@jens-maus.de",
"url": "https://github.com/jens-maus"
}
],
"license": "MIT",
Expand All @@ -46,15 +54,14 @@
},
"homepage": "https://github.com/kiliankoe/dvbjs",
"dependencies": {
"lodash": "^3.10.0",
"proj4": "^2.3.12",
"request-promise": "^1.0.2"
"proj4": "^2.3.17",
"request-promise": "^4.1.1"
},
"devDependencies": {
"bluebird": "^3.0.6",
"coveralls": "^2.11.6",
"istanbul": "^0.4.1",
"mocha": "^2.2.5",
"mockery": "^1.4.0"
"bluebird": "^3.4.7",
"coveralls": "^2.11.16",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mockery": "^2.0.0"
}
}
2 changes: 1 addition & 1 deletion test/data/coords-poi.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"5656699|4621216"
"5656699|4621216"
1 change: 1 addition & 0 deletions test/data/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
""
2 changes: 1 addition & 1 deletion test/data/empty_json.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[]
{ "parameters": [ { "name":"serverID", "value":"efa.vvo-online.de:8080" }, { "name":"requestID", "value":"0" }, { "name":"sessionID", "value":"0" } ], "stopFinder": { "message": [ { "name":"code", "value":"-2000" }, { "name":"error", "value":"stop invalid" } ], "input": { "input":"0" }, "points": null } }
1 change: 1 addition & 0 deletions test/data/find-postplatz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "parameters": [ { "name":"serverID", "value":"efa.vvo-online.de:8080" }, { "name":"requestID", "value":"0" }, { "name":"sessionID", "value":"0" } ], "stopFinder": { "input": { "input":"Postpl" }, "points": { "point": { "usage":"sf", "type":"stop", "name":"Dresden, Postplatz", "stateless":"33000037", "ref": { "id":"33000037", "omc":"14612000", "placeID":"1", "place":"Dresden", "coords":"13.733513736,51.050834998" }, "infos": null } } } }
1 change: 0 additions & 1 deletion test/data/find-zellescherweg.json

This file was deleted.

1 change: 1 addition & 0 deletions test/data/monitor-33000037.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Name":"Postplatz","Status":{"Code":"Ok"},"Place":"Dresden","ExpirationTime":"\/Date(1487292603015+0100)\/","Departures":[{"Id":"65501116","LineName":"62","Direction":"Gompitz","Platform":{"Name":"3","Type":"Platform"},"Mot":"CityBus","RealTime":"\/Date(1487294700000+0100)\/","ScheduledTime":"\/Date(1487294700000+0100)\/","State":"InTime","RouteChanges":["509223"],"Diva":{"Number":"21062","Network":"voe"}},{"Id":"65499188","LineName":"7","Direction":"Pennrich","Platform":{"Name":"4","Type":"Platform"},"Mot":"Tram","RealTime":"\/Date(1487294700000+0100)\/","ScheduledTime":"\/Date(1487294700000+0100)\/","State":"InTime","Diva":{"Number":"11007","Network":"voe"}},{"Id":"65501226","LineName":"62","Direction":"Pillnitz","Platform":{"Name":"4","Type":"Platform"},"Mot":"CityBus","RealTime":"\/Date(1487294700000+0100)\/","ScheduledTime":"\/Date(1487294700000+0100)\/","State":"InTime","RouteChanges":["509223"],"Diva":{"Number":"21062","Network":"voe"}},{"Id":"65498389","LineName":"2","Direction":"Kleinzschachwitz","Platform":{"Name":"1","Type":"Platform"},"Mot":"Tram","RealTime":"\/Date(1487294700000+0100)\/","ScheduledTime":"\/Date(1487294700000+0100)\/","State":"InTime","Diva":{"Number":"11002","Network":"voe"}},{"Id":"65499315","LineName":"7","Direction":"Weixdorf","Platform":{"Name":"3","Type":"Platform"},"Mot":"Tram","RealTime":"\/Date(1487294700000+0100)\/","ScheduledTime":"\/Date(1487294700000+0100)\/","State":"InTime","Diva":{"Number":"11007","Network":"voe"}}]}
1 change: 0 additions & 1 deletion test/data/monitor-postplatz.json

This file was deleted.

Loading

0 comments on commit cf85d72

Please sign in to comment.