About
CMetropolitana.js is an unofficial Node.js API wrapper for Carris Metropolitana's public api.
With this wrapper, you can interact with all of the existing API endpoints, as well as listen to specific events (such as when a vehicle arrives/departs a bus stop...)
Instalation
npm install cmetropolitana.js
yarn add cmetropolitana.js
Example usage
Feel free to also check the suplied example.js file for further details! This package also includes JSDocs (which should work with any existing IDE).
Installation:
npm install cmetropolitana.js
yarn add cmetropolitana.js
Fetch details for a specific stop by its ID:
const CMetropolitana = require("cmetropolitana.js")
CMetropolitana.stops.fetch("060033").then(stop => {
console.log(stop.name);
console.log(stop.patterns);
console.log(stop.alerts());
})
Fetch details for a specific vehicle by its ID:
const CMetropolitana = require("cmetropolitana.js")
CMetropolitana.vehicles.fetch("41|1100").then(vehicle => {
console.log(vehicle);
vehicle.parent().then(pattern => {
console.log(pattern)
})
})
Afterwards, we can listen to specific events:
const CMetropolitana = require("cmetropolitana.js")
const vehicle = CMetropolitana.vehicles.cache.get("41|1100")
vehicle.on("positionUpdate", (lat, lon) => {
console.log(`New position for ${vehicle.id}: ${lat}, ${lon}`)
})
vehicle.on("serviceStart", () => {
console.log(`${vehicle.id} has started a new service!`)
})
CMetropolitana.vehicles.on("serviceStart", (vec) => {
console.log(`${vec.id} has started a new service!`)
})
vehicle.on("serviceEnd", () => {
console.log(`${vehicle.id} has finished a service!`)
})
CMetropolitana.vehicles.on("serviceEnd", (vec) => {
console.log(`${vec.id} has finished a service!`)
})
Get the line/route from a pattern, using .parent():
const CMetropolitana = require("cmetropolitana.js")
CMetropolitana.patterns.fetch("1001_0_1").then(async pattern => {
let route = await pattern.pattern();
let line = await route.parent();
})
Or, you can get all lines/routes/patterns on a specific line, route or stop:
const CMetropolitana = require("cmetropolitana.js")
CMetropolitana.lines.fetch("1001").then(async line => {
let routes = await line.getRoutes();
let patterns = await line.getPatterns();
})
CMetropolitana.routes.fetch("1001_0").then(async route => {
let patterns = await route.getPatterns();
})
CMetropolitana.stops.fetch("121270").then(async stop => {
let lines = await stop.getLines();
let routes = await stop.getRoutes();
let patterns = await stop.getPatterns();
})
Get info of a specific school:
const CMetropolitana = require("cmetropolitana.js")
CMetropolitana.schools.fetch("200098").then(school => {
console.log(school.name);
console.log(school.stops);
school.getStops().then(stops => console.log(stops))
})
Get existing alerts:
const CMetropolitana = require("cmetropolitana.js")
async function load() {
CMetropolitana.alerts.fetchAll();
CMetropolitana.routes.fetchAll();
CMetropolitana.stops.fetchAll();
}
CMetropolitana.alerts.forStop("060006");
CMetropolitana.stops.cache.get("060006").alerts();
CMetropolitana.alerts.forRoute("1001_0");
CMetropolitana.routes.cache.get("1001_0").alerts();
Get details for a vehicle that's already cached by its ID
const CMetropolitana = require("cmetropolitana.js")
console.log(CMetropolitana.vehicles.cache.get("41|1100"))
Get departures for a specific stop
const CMetropolitana = require("cmetropolitana.js")
const stop = CMetropolitana.stops.cache.get("060033")
stop.departures().then(departures => {
departures.forEach(async d => {
console.log(d)
console.log(await d.getVehicle())
})
})
You can also listen to specific events:
const CMetropolitana = require("cmetropolitana.js")
const stop = CMetropolitana.stops.cache.get("060033")
stop.on("vehicleArrival", (vec) => {
console.log(`${vec.id} has arrived at ${stop.name}`)
})
stop.on("vehicleDeparture", (vec) => {
console.log(`${vec.id} has departed fromm ${stop.name}`)
})
If necessary, you can fetch all of the stops, lines and routes beforehand:
const CMetropolitana = require("cmetropolitana.js")
async function load() {
await CMetropolitana.lines.fetchAll()
await CMetropolitana.routes.fetchAll()
await CMetropolitana.stops.fetchAll()
await CMetropolitana.alerts.fetchAll()
await CMetropolitana.schools.fetchAll()
}
load()