Utilities for NYC's realtime MTA data feeds.
Project description
Python MTA Utilities
This is a set of Python utilities that I use to deal with real-time NYC subway data.
I usually want to know when trains are going to depart a specific stop along a specific train line, so right now the tools are mostly for that. But I tried to write them to support arbitrary functionality.
Install
pip install underground
Or if you'd like to live dangerously:
pip install git+https://github.com/nolanbconaway/underground.git#egg=underground
Python API
Use the Python API like:
import os
from underground import metadata, SubwayFeed
ROUTE = 'Q'
feed = SubwayFeed.get(ROUTE)
# under the hood, the Q route is mapped to a URL. This call is equivalent:
URL = 'https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-nqrw'
feed = SubwayFeed.get(URL)
# or
URL = metadata.resolve_url(ROUTE)
feed = SubwayFeed.get(URL)
List train stops on each line
feed.extract_stop_dict
will return a dictionary of dictionaries, like:
>>> feed.extract_stop_dict()
{
"route_1": {
"stop_1": [datetime.datetime(...), datetime.datetime(...)],
"stop_2": [datetime.datetime(...), datetime.datetime(...)],
...
},
"route_2": {
"stop_1": [datetime.datetime(...), datetime.datetime(...)],
"stop_2": [datetime.datetime(...), datetime.datetime(...)],
...
}
}
CLI
The underground
command line tool is also installed with the package.
feed
$ underground feed --help
Usage: underground feed [OPTIONS] ROUTE_OR_URL
Request an MTA feed via a route or URL.
ROUTE_OR_URL may be either a feed URL or a route (which will be used to
look up the feed url).
Examples (both access the same feed):
underground feed Q --json > feed_nrqw.json
URL='https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-nqrw' &&
underground feed $URL --json > feed_nrqw.json
Options:
--json Option to output the feed data as JSON. Otherwise
output will be bytes.
-r, --retries INTEGER Retry attempts in case of API connection failure.
Default 100.
--help Show this message and exit.
stops
$ underground stops --help
Usage: underground stops [OPTIONS] [H|M|D|1|Z|A|N|GS|SI|J|G|Q|L|B|R|F|E|2|7|W|
6|4|C|5|FS]
Print out train departure times for all stops on a subway line.
Options:
-f, --format TEXT strftime format for stop times. Use `epoch` for a
unix timestamp.
-r, --retries INTEGER Retry attempts in case of API connection failure.
Default 100.
-t, --timezone TEXT Output timezone. Ignored if --epoch. Default to NYC
time.
-s, --stalled-timeout INTEGER Number of seconds between the last movement
of a train and the API update before
considering a train stalled. Default is 90 as
recommended by the MTA. Numbers less than 1
disable this check.
--help Show this message and exit.
Stops are printed to stdout in the format stop_id t1 t2 ... tn
.
$ underground stops Q | tail -2
Q05S 19:01 19:09 19:16 19:25 19:34 19:44 19:51 19:58
Q04S 19:03 19:11 19:18 19:27 19:36 19:46 19:53 20:00
If you know your stop id (stop IDs can be found in stops.txt), you can grep the results:
$ underground stops Q | grep Q05S
Q05S 19:09 19:16 19:25 19:34 19:44 19:51 19:58
If you don't know your stop, see below for a handy tool!
findstops
$ underground findstops --help
Usage: underground findstops [OPTIONS] QUERY...
Find your stop ID.
Query a location and look for your stop ID, like:
$ underground findstops parkside av
Options:
--json Option to output the data as JSON. Otherwise will be human readable
table.
--help Show this message and exit.
Enter the name of your stop and a table of stops with matching names will be returned.
$ underground findstops parkside
ID: D27N Direction: NORTH Lat/Lon: 40.655292, -73.961495 Name: PARKSIDE AV
ID: D27S Direction: SOUTH Lat/Lon: 40.655292, -73.961495 Name: PARKSIDE AV
Some names are ambiguous (try "fulton st"), for these you'll have to dig into the metadata more carefully.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file underground-1.0.0.tar.gz
.
File metadata
- Download URL: underground-1.0.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb48fdb1de77e2dcb50c377ddc66c2a1d0b556104737c82019b9f288d2cd6fbd |
|
MD5 | 3b9890b9b0f4d1da361aad49733a03df |
|
BLAKE2b-256 | 94e54ce33a08a85e76fc0c3d3f7d191440582c05741097beed617de63047c3c1 |
Provenance
The following attestation bundles were made for underground-1.0.0.tar.gz
:
Publisher:
release_to_pypi.yml
on nolanbconaway/underground
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
underground-1.0.0.tar.gz
- Subject digest:
cb48fdb1de77e2dcb50c377ddc66c2a1d0b556104737c82019b9f288d2cd6fbd
- Sigstore transparency entry: 147957847
- Sigstore integration time:
- Predicate type:
File details
Details for the file underground-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: underground-1.0.0-py3-none-any.whl
- Upload date:
- Size: 16.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40fe2dbc3fb0a703efb4bbf1b14fa7bb51920296221c2d43f23ab929f9ea44d0 |
|
MD5 | e61bc3b59b17edf82dd7cbb66b3c237d |
|
BLAKE2b-256 | e6b6c94b76b12671b388a889dfba0d8ad75a695aa09f98422d9d53e09771a5fa |
Provenance
The following attestation bundles were made for underground-1.0.0-py3-none-any.whl
:
Publisher:
release_to_pypi.yml
on nolanbconaway/underground
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
underground-1.0.0-py3-none-any.whl
- Subject digest:
40fe2dbc3fb0a703efb4bbf1b14fa7bb51920296221c2d43f23ab929f9ea44d0
- Sigstore transparency entry: 147957848
- Sigstore integration time:
- Predicate type: