Skip to main content

Lightweight Python library for NYC MTA train arrival times

Project description

py-nycmta

A lightweight Python library for getting NYC MTA train arrival times. Simple, fast, and focused.

Installation

pip install py-nycmta

Quick Start

from py_nycmta import Train

# Create a train instance for any line
f_train = Train('F')

# Get arrivals at 7 Av (F24)
arrivals = f_train.get_arrivals('F24')

for arrival in arrivals:
    print(f"{arrival.train_id} train in {arrival.minutes_away} minutes ({arrival.direction}bound)")

Features

  • Ultra-lightweight: Only 3 dependencies (httpx, gtfs-realtime-bindings, protobuf)
  • Simple: Just one Train class for all subway lines
  • Fast: Direct GTFS feed access, no unnecessary abstractions
  • Type-safe: Full type hints for better IDE support

Usage

Basic Usage

from py_nycmta import Train

# Create train instances for any line
f_train = Train('F')
n_train = Train('N')
a_train = Train('A')
one_train = Train('1')

# Get arrivals at a stop
arrivals = f_train.get_arrivals('F24')  # 7 Av

Filter by Direction

f_train = Train('F')

# Get only northbound trains
northbound = f_train.get_arrivals('F24', direction='N')

# Get only southbound trains
southbound = f_train.get_arrivals('F24', direction='S')

Get Next Few Arrivals

f_train = Train('F')

# Get next 2 arrivals
next_trains = f_train.get_next_arrivals('F24', count=2)

Working with Arrival Data

f_train = Train('F')
arrivals = f_train.get_arrivals('F24')

for arrival in arrivals:
    print(f"Train: {arrival.train_id}")
    print(f"Minutes away: {arrival.minutes_away}")
    print(f"Direction: {arrival.direction}")
    print(f"Status: {arrival.status}")
    print(f"Arrival time: {arrival.arrival_time}")
    print(f"Stop ID: {arrival.stop_id}")
    print("---")

Supported Train Lines

All NYC subway lines are supported using the single Train class:

from py_nycmta import Train

# Letter trains
Train('A'), Train('B'), Train('C'), Train('D'), Train('E')
Train('F'), Train('G'), Train('J'), Train('L'), Train('M')
Train('N'), Train('Q'), Train('R'), Train('W'), Train('Z')

# Number trains  
Train('1'), Train('2'), Train('3'), Train('4'), Train('5'), Train('6'), Train('7')

Stop IDs

Use MTA stop IDs without the direction suffix:

  • F24 - 7 Av
  • A41 - Jay St-MetroTech
  • R16 - Times Sq-42 St
  • R20 - 14 St-Union Sq

The library automatically handles northbound (N) and southbound (S) directions.

Error Handling

from py_nycmta import Train

try:
    # Invalid train line
    train = Train('X')  # Raises ValueError
except ValueError as e:
    print(f"Error: {e}")  # Shows valid train options

try:
    train = Train('F')
    arrivals = train.get_arrivals('INVALID_STOP')
except Exception as e:
    print(f"Error: {e}")

Development

git clone https://github.com/dvd-rsnw/py-nycmta
cd py-nycmta
pip install -e ".[dev]"
pytest

Requirements

  • Python 3.8+
  • Internet connection (for MTA GTFS feeds)

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_nycmta-0.2.0.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_nycmta-0.2.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file py_nycmta-0.2.0.tar.gz.

File metadata

  • Download URL: py_nycmta-0.2.0.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for py_nycmta-0.2.0.tar.gz
Algorithm Hash digest
SHA256 704b5b54eecf610e6bbf6af9bea86e6edaf9beaa3baaa46e7292a689349d12a6
MD5 e01b74bc89f151b3dc7b359ab9e289cd
BLAKE2b-256 138bc84d4cf482d2bb76e512b86daae1ea89e0640c3d878b15ae0f31df0be087

See more details on using hashes here.

File details

Details for the file py_nycmta-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: py_nycmta-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for py_nycmta-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12e54f2178a102df981b262123b7f64ad49373defa9e2716211766e15ae1be89
MD5 b1a834f7bc64b461eb34a31673d091e5
BLAKE2b-256 acd7301036338d5a4b2529f68d42dc0705873b8077f4767ac13ec8ca709ab99d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page