MBTA

From LibrePlanet
Revision as of 16:32, 6 March 2020 by Mmcmahon (talk | contribs) (Add best practices for termux)
Jump to: navigation, search

Introduction

This page includes ways to liberate the MBTA. By default, using the MBTA schedules and apps contain nonfree software. Let us explore ways to plan travel around Boston in a better way together.

MBTA Maps

Low-tech

Go to a bus stop or train station and wait until a bus or train arrives. Find paper time schedules for the routes you frequent at popular stations such as Park Street.

The drawback to this method is that buses and trains do not always stick to the schedule. Planning is difficult and you may be late to your destination.

MBTA API

Usage of the MBTA API seems to be the most freedom respecting digital route at this time. This method is efficient as only the requested data is pulled instead of an entire webpage which helps when using metered connections.

Website

Documentation

Source code with MIT (expat) license

Requesting a key does require nonfree JavaScript to be enabled, but does not require a captcha.

Configuring dependencies for Replicant

The MBTA API can be accessed through Replicant using Termux. These tools come with most GNU/Linux operating systems be default.

  1. Install Termux from F-Droid.
  2. Open Termux.
  3. Install curl and python. pkg install -y curl python
  4. All set!

Installing the Hacker's Keyboard from F-Droid is also recommended, but not required.

Note: If Termux is closed improperly without exit, the .bash_history might be lost. It is recommended to use these upcoming commands in simple bash scripts so that they can be easily be copied or modified without needing to retype the addresses or API keys.

Using Curl without an API key

Replace 70020 with the stop id. Bus stop ids can usually be read from the sign at the stop. Additional methods of finding stop ids can be found below.

curl "https://api-v3.mbta.com/predictions?filter%5Bstop%5D=70020" | python -m json.tool

The API will let you pull once every so often without an API key. This is useful if you do not have an API key yet.

Using Curl with an API key

Replace INSERTAPIKEYHERE with a valid API key.

curl -sN -H "x-api-key: INSERTAPIKEYHERE" "https://api-v3.mbta.com/predictions?filter%5Bstop%5D=70020" | python -m json.tool

Pulling a stream using an API key

curl -sN -H "accept: text/event-stream" -H "x-api-key: INSERTAPIKEYHERE" "https://api-v3.mbta.com/predictions?filter%5Bstop%5D=70020"

The stream will be kept open and only updates will come through. Between updates the connection will give a keep alive notification. The stream will have to be stopped with CTRL + c.

Note that busy stations will include noise that does not pertain to the next vehicle.

Downloading the list of stops

curl "https://api-v3.mbta.com/stops" > stops.txt

The stops list is about 5.4MB of json.

Parsing the stops list

Example: Find the Orange line stop ids leading to Forest Hills.

python -m json.tool stops.txt | grep -B8 -A4 '"platform_name": "Forest Hills",'

Downtown Crossing - Orange Line - Forest Hills is stop: 70020

Complications:

  • The red and green lines have converging tracks so the above method will not always work.
  • Stations have several stop ids that include platforms, busways, elevators, escalators, and more.
  • End stations have two platforms going the same direction with two separate ids. They usually run frequently enough that this is not an issue.