JavaScript library to access services from the public API of the Public Transport Authority of Madrid, Spain (EMT)
Request access to the API at http://opendata.emtmadrid.es/Formulario
emtmad-bus-times-bw is packaged using Bower ( http://bower.io/ ):
bower install emtmad-bus-times-bw --save
If you don't use bower, you can just download the emtmadBusTimes.js library.
Once you have the library, you just need to call the getIncomingBuses(busStopNumber, YOUR_API_ID, YOUR_API_PASSKEY, your_callback_function(jsonOutput) and access the JSON file:
var busStopNumber = '1924';
var idClient = 'XXXXXXX';
var passKey = 'YYYYYY';
getIncomingBuses(busStopNumber,idClient,passKey, function(output){
if (output.status == 200)
console.log(output.arrives);
else if (output.status == 400)
console.log(output.error);
else
console.log("unknown error");
});
If the call to the API is successful, the returned JSON will be like this:
{
"status":200,
"arrives":[{bus0},{bus1}....{busN}]
}
Where every {busN} object represents 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
}
If the call to the API wasn't successful, the returned JSON will be like this:
{
"status":400,
"error": e
}
The SSL certificate in the endpoint ( https://openbus.emtmadrid.es ) is self-signed, so you will need to add it to your browser's truststore.
Do you want to contribute? Great! Pull requests are more than welcome.