node-emtmad-bus-promise
v2.2.0
Published
Nodejs library to access services from the public API of the Public Transport Authority of Madrid, Spain (EMT) with a Promises twist
Downloads
9
Readme
node-emtmad-bus-promise
Node.js module to access services from the public API of the Public Transport Authority of Madrid, Spain (EMT).
This module is a fork of https://github.com/alvaroreig/emtmad-bus-times-node that returns Promise objects instead of doing sync calls to the EMT API.
API Access
Request access to the API at http://opendata.emtmadrid.es/Formulario. You will be given a Client ID and a Passkey.
Installation
node-emtmad-bus-promise is packaged using NPM. Just require the package inside your code:
var EMTAPI = require('node-emtmad-bus-promise');
Initializing the API
In order to use this API you need to set two environment variables with your Client ID and Passkey. These variables need to be set before using require on the module.
EMT_APP_ID = <your client id>
EMT_PASSKEY = <your passkey>
Usage: get incoming buses to a given bus stop
Call the function getIncomingBusesToStop(busStopNumber) and a Promise will be returned that fulfills to an array of estimations:
let stop = {
Id: 3041
}
EMTAPI.getIncomingBusesToStop(stop.Id)
.then(function (arriving) {
stop.arriving = arriving;
resolve(stop);
})
.catch(function (error) {
resolve(`Error: ${error}`);
});
The array of estimations is built with Bus objects that represent an incoming bus to the specified bus stop. The most relevant attributes for each bus are:
{
"lineId": "32", // The line of the bus
"busDistance": 9, // In meters
"busTimeLeft": 0 // In seconds
}
Usage: get stops close to a location
Call the function getStopsFromLocation(location, radius) and a Promise will be returned that fulfills to an array of stops:
let location = {
latitude: -3.7324823992585,
longitude: 40.377653538528
}
let searchRadius = 250;
EMTAPI.getStopsFromLocation(location, searchRadius)
.then(function (stops) {
// do something
})
.catch(function (error) {
resolve(`Error: ${error}`);
});
The array of stops is built with Stop objects that look like this:
{
stopId: '2443',
name: 'AV.ABRANTES-PZA.LASMENINAS',
postalAddress: 'Av.deAbrantes, 106',
longitude: -3.7324823992585,
latitude: 40.377653538528,
line: [Line Object]
}
Usage: get stops of a line
Call the function getStopsLine(line, direction) and a Promise will be returned that fulfills to the whole of object returned by the EMT API. Since this query is pretty time consuming, the results are cached per line/direction combination to speed up things.
let line = "118"; // line should be the lineId and not the label of the line
let direction = 1; // lines have two directions represented either by: 1 or 2
EMTAPI.getStopsLine(line, direction)
.then(function (results) {
// do something
})
.catch(function (error) {
resolve(`Error: ${error}`);
});
The object is described in the EMT API and looks like this:
{
lineId: 516, // Line Id
label: 'N16', // Label for this line
destination: 'TEXT', // Destination of line
incidents: 0 // 0: No incidents, 1: Incidents
stop: [] // List of Stop
line: [] // List of Line
}
Further reading
The objects returned in this API are exactly the same as the EMT API. A comprehensive documentation of the objects returned by the EMT API can be downloaded in the following link: http://opendata.emtmadrid.es/Documentos/Opendata-v-1-12.aspx
Development
Do you want to contribute? Great! Pull requests are more than welcome.