NYCT-GTFS - Real-time NYC subway data parsing for humans
This python library provides a human-friendly, native python interface for dealing with the NYCT Subway data published by the MTA. By default, this data is provided in a protobuf-encoded format called GTFS-realtime, which further has NYCT-specific customization. This is quite difficult to parse, and requires a lot of boilerplate to do even very simple queries.
However, with NYCT-GTFS, you can access and query this data in just a few lines of Python:
>>> from nyct_gtfs import NYCTFeed
# Load the realtime feed from the MTA site
>>> feed = NYCTFeed("1", api_key="YOUR_MTA_API_KEY_GOES_HERE")
# Get all 123 trains currently underway to Times Sq-42 St
>>> trains = feed.filter_trips(line_id=["1", "2", "3"], headed_for_stop_id=["127N", "127S"], underway=True)
# Let's look closer at the first train included in the filter above:
>>> str(trains[0])
'Northbound 1 to Van Cortlandt Park-242 St, departed origin 22:20:00, Currently INCOMING_AT 34 St-Penn Station, last update at 22:34:11'
# We can extract each of these details programatically as well,
# to get arrival time information for the next station (which in this case is 34th St-Penn Station):
>>> trains[0].stop_time_updates[0].arrival
datetime.datetime(2021, 11, 26, 22, 34, 51)
# What about the next stop after that? Should be Times Square
>>> trains[0].stop_time_updates[1].stop_name
'Times Sq-42 St'
# And what time will it get there?
>>> trains[0].stop_time_updates[1].arrival
datetime.datetime(2021, 11, 26, 22, 36, 21)
# To pull new data, use the refresh() method
>>> feed.refresh()
# You must also update the trains list, existing objects are not modified by refresh()
>>> trains = feed.filter_trips(line_id=["1", "2", "3"], headed_for_stop_id=["127N", "127S"], underway=True)
See trip.py
and stop_time_update.py
for more information about the fields available
Built With
Installation
- Get a free MTA API Key at https://api.mta.info/
- Install nyct-gtfs
pip install nyct-gtfs
- Load the data feed
from nyct_gtfs import NYCTFeed # Load the realtime feed from the MTA site feed = NYCTFeed("1", api_key="YOUR_MTA_API_KEY_GOES_HERE")
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt
for more information.
Contact
Andrew Dickinson - [email protected]
Project Link: https://github.com/Andrew-Dickinson/nyct-gtfs
Acknowledgments
Disclaimer
This project is not endorsed by, directly affiliated with, maintained, authorized, or sponsored by any transit agency. All names and marks are the registered trademarks of their original owners. The use of any trade name or trademark is for identification and reference purposes only and does not imply any association with the trademark holder or their brand.