npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tfl-busboy

v1.2.0

Published

Get TfL bus predictions

Downloads

12

Readme

Nice event stream wrapper for the byzantine TfL Bus Departures API.

npm install tfl-busboy

The what

A bunch of methods that call the API and return nested event stream objects, such as:

busboy.around({lat: 51.371422, lng: -0.227344}, 10);
// eventually: {'2232': { stopPointName: 'North Cheam / Queen Victoria', ... }}

The objects in the stream are a map of Stop IDs to Stop objects, which represent a bus stop:

var Stop = adt.newtype('Stop', {
  stopPointName: adt.only(String, undefined),
  stopId: adt.only(String, undefined),
  stopCode1: adt.only(String, undefined),
  stopCode2: adt.only(String, undefined),
  stopPointType: adt.only(String, undefined),
  towards: adt.only(String, undefined),
  bearing: onlyInt,
  stopPointIndicator: adt.only(String, undefined),
  stopPointState: onlyInt,
  latitude: onlyFloat,
  longitude: onlyFloat,
  // predictions: Map<PredictionID, Prediction>
  // messages: Map<MessageID, FlexibleMessage>
});

A Prediction is an object representing a bus on the way. Yes, there are keys duplicated from Stop. Deal with it.

var Prediction = adt.newtype('Prediction', {
  stopPointName: adt.only(String, undefined),
  stopId: adt.only(String, undefined),
  stopCode1: adt.only(String, undefined),
  stopCode2: adt.only(String, undefined),
  stopPointType: adt.only(String, undefined),
  towards: adt.only(String, undefined),
  bearing: onlyInt,
  stopPointIndicator: adt.only(String, undefined),
  stopPointState: onlyInt,
  latitude: onlyFloat,
  longitude: onlyFloat,
  visitNumber: onlyInt,
  lineID: adt.only(String),
  lineName: adt.only(String),
  directionId: onlyInt,
  destinationText: adt.only(String),
  destinationName: adt.only(String),
  vehicleId: onlyInt,
  tripId: onlyInt,
  registrationNumber: adt.only(String),
  estimatedTime: onlyDate,
  expireTime: onlyDate
});

A FlexibleMessage is a generic service message pertaining to a Stop.

var FlexibleMessage = adt.newtype('FlexibleMessage', {
  stopPointName: adt.only(String, undefined),
  stopId: adt.only(String, undefined),
  stopCode1: adt.only(String, undefined),
  stopCode2: adt.only(String, undefined),
  stopPointType: adt.only(String, undefined),
  towards: adt.only(String, undefined),
  bearing: onlyInt,
  stopPointIndicator: adt.only(String, undefined),
  stopPointState: onlyInt,
  latitude: onlyFloat,
  longitude: onlyFloat,
  messageUUID: adt.only(String),
  messageType: onlyInt,
  messagePriority: onlyInt,
  messageText: adt.only(String),
  startTime: onlyDate,
  expireTime: onlyDate
});

For more details of the keys, have a gander at the TfL documentation (inb4 wtf pdf: i know, right?)

The why

The TfL Live Bus API is awful. There is a single call that returns newline-separated JSON arrays. The arrays represent one of 5 different data types, each with different keys. The keys aren't in the returned data, you have to match up the keys you asked for, the keys in each type and the order in the documentation. There's a one-to-many relationship between Stop and Prediction and Stop and FlexibleMessage, but Prediciton and FlexibleMessage come back joined with their Stop. If you don't ask for any of the keys that a type uniquely has, that type gets filtered out, apart from Stop, which only comes back if you send StopAlso=true, even if you ask for StopID.

Argh.

The API

busboy.around(latlng, radius)()

Queries for stops within radius (in metres) of latlng (object containing lat and lng).

Filter methods busboy[key](value)

StopPointName, StopID, StopCode1, StopCode2, StopPointType, Towards, Bearing, StopPointState, VisitNumber, LineID, LineName, DirectionID, DestinationText, DestinationName, VehicleID, TripID, RegistrationNumber, StopPointIndicator, MessageType, MessagePriority

Filters the response to stops, predictions or messages with a key of value. See Section 4.1.1 of the TfL documentation.

busboy.query(queryObject)

Send an API request with the given query. Use one of the higher-level methods.

busboy.withOptions(options)

Lets you override everything. options is passed to url.format.

Licence

MIT. Data provided by Transport for London. You must register at the TfL Developer Portal if you want to use the official API.