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

updated the dvb.monitor() functionality to use webapi.vvo-online.de #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 108 additions & 24 deletions lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,121 @@
var requestP = require('request-promise');
var utils = require('./utils');

var monitor = function monitor(stop, offset, amount, callback) {
function pointFinder(name, callback) {

// remove whitespace, vvo-online.de handles all kinds of requests pretty nicely
stop = utils.stripSpaces(stop);
var stopName = utils.stripSpaces(name);

// 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',
qs: {
hst: stop,
vz: offset,
lim: amount
url: 'https://webapi.vvo-online.de/tr/pointfinder?format=json',
qs: {
limit: 0,
query: stopName,
stopsOnly: true,
dvb: true,
assignedStops: true
}
}

return requestP(options).then(function(response) {
var data = JSON.parse(response);

// check status
if(data.Status !== 'undefined' &&
data.Status.Code === 'Ok'&&
data.Points !== 'undefined') {

for(var i=0; i < data.Points.length; i++) {
var p = data.Points[i].split('|');

// only and empty city string points to
// DVB (Dresden) stops
if(p[2].length === 0 || p[2] === 'Dresden') {
var stopid = p[0];

if(typeof(callback) === 'function')
callback(stopid);

break;
}
}
};
}
});
}

function depatureMonitor(stopid, offset, amount, callback) {

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

var options = {
url: 'https://webapi.vvo-online.de/dm?format=json',
qs: {
stopid: stopid,
time: time.toISOString(),
isarrival: false,
limit: amount,
shorttermchanges: true,
mentzonly: false
}
}

return requestP(options).then(function(response) {
var data = JSON.parse(response);
var departures = [];

// check status
if(data.Status !== 'undefined' &&
data.Status.Code === 'Ok'&&
data.Departures !== 'undefined') {

for(var i=0; i < data.Departures.length; i++) {
var d = data.Departures[i];

var arrivalTime;
if(d.RealTime)
arrivalTime = new Date(parseInt(d.RealTime.substr(6)));
else
arrivalTime = new Date(parseInt(d.ScheduledTime.substr(6)));

var arrivalTimeRelative = Math.round((arrivalTime - now) / 1000 / 60);

var scheduledTime = new Date(parseInt(d.ScheduledTime.substr(6)));
var scheduledTimeRelative = Math.round((scheduledTime - now) / 1000 / 60);

var platform;
if(d.Platform)
platform = { name: d.Platform.Name, type: d.Platform.Type };

departures.push({
line: d.LineName,
direction: d.Direction,
platform: platform,
arrivalTime: arrivalTime,
arrivalTimeRelative: arrivalTimeRelative,
scheduledTime: scheduledTime,
scheduledTimeRelative: scheduledTimeRelative,
delayTime: Math.round((arrivalTime - scheduledTime) / 1000 / 60),
state: d.State ? d.State : 'Unknown',
mode: utils.parseMode(d.Mot)
});
}

return departures;
}
}).nodeify(callback);
}

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

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);
if(typeof(stop) === 'string') {
pointFinder(utils.stripSpaces(stop), function(stopid) {
return depatureMonitor(stopid, offset, amount, callback);
});
} else
return depatureMonitor(stop, offset, amount, callback);
};

module.exports = monitor;
50 changes: 15 additions & 35 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,8 @@ 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 (id === 'SWB' || id === 'STB') return utils.modes['lift'];

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 (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'];

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

utils.modes = {
Expand All @@ -96,35 +76,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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"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": {
Expand Down