Skip to main content

Package for reading and organizing GTFS data for a given station stop.

Project description

GTFS Station Stop

A project for organizing GTFS Real-Time data for use as a homeassistant sensor.

test coverage'

Usage

This is designed for use with Home Assistant GTFS Realtime Custom Component.

It can also be used for general GTFS update purposes.

Feed Subjects and Station Stops

All updates go through the Feed Subject which is setup to call updates from one or more feed URLS.

Create a feed subject like so, then pass it in the constructor for a Station Stop

from gtfs_station_stop.feed_subject import FeedSubject
from gtfs_station_stop.station_stop import StationStop

# Obtain the API keep from your GTFS provider if needed, otherwise leave blank.
api_key = "YOUR_API_KEY_HERE"
urls = ["https://gtfs.example.com/feed1", "https://gtfs.example.com/feed2"]
feed_subject = FeedSubject(urls, api_key)

# Obtain the Stop ID from GTFS static data from your provider.
# This must match those provided by the realtime feed.
station_stop_nb = StationStop("STOP_ID_NORTHBOUND", feed_subject)
station_stop_sb = StationStop("STOP_ID_SOUTHBOUND", feed_subject)

Calling feed_subject.update() will update all registered listeners.

feed_subject.update()

for arrival in station_stop_nb.arrivals:
    minutes_to = (arrival.time - time.time()) / 60.0
    print(f"{arrival.route} in {minutes_to}")

Active service alerts are also supported for station stops and for routes.

route_status = RouteStatus("Line 1", feed_subject)

feed_subject.update()

for alert in route_status.alerts:
    print(f"{route_status.id} alert {alert.header_text['en']}")

for alert in station_stop_nb.alerts:
    print(f"{station_stop_nb.id} alert {alert.header_text['en']}")

As the update will make one or more http requests, this may improve performance or integrate better with an asynchronous project.

GTFS Static Info

Static data can be loaded into a Dataset for convenient lookup to use alongside GTFS Realtime data. GTFS data can be read from a file or a URL from your service provider. The GTFS file must be provided as a .zip containing the requisite .txt files as defined by GTFS Static Reference.

from gtfs_station_stop.station_stop_info import StationStopInfoDataset

station_stop_infods = StationStopInfoDataset("gtfs_static.zip")
print(f"{station_stop_infods['STOP_ID']}")

Static info can be queried through the station_stop_info, route_info, calendar, and trip_info submodules.

GTFS providers will regularly update their static feeds. In order to account for this, the library will attempt to cache zip file downloads for static info.

Async Updates

Asynchronous updates are also supported through the async_update() method.

await feed_subject.async_update()

Static data can also be obtained similarly with gtfs_station_stop.static_Dataset.async_factory.

station_stop_info_Dataset = await async_get_gtfs_Dataset(StationStopInfoDataset, "https://gtfsprovider.example.com/static.zip")

Command Line Interface

This can be run as a Python module on the command line using

$ python -m gtfs_station_stop

This must be installed with the optional group [cli].

$ pip install gtfs_station_stop[cli]

Use python -m gtfs_station_stop --help for details.

Development Setup

It is recommended to use uv to develop in this project.

$ uv pip install --group dev

Run tests with:

$ uv run pytest

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

gtfs_station_stop-0.9.5.tar.gz (123.3 kB view details)

Uploaded Source

Built Distribution

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

gtfs_station_stop-0.9.5-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file gtfs_station_stop-0.9.5.tar.gz.

File metadata

  • Download URL: gtfs_station_stop-0.9.5.tar.gz
  • Upload date:
  • Size: 123.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for gtfs_station_stop-0.9.5.tar.gz
Algorithm Hash digest
SHA256 038564d5b512245d13ee9354826a74b749613e60937765d4db41282f9ebf2683
MD5 0ae1fab3f93bf8baae350f908ba816cb
BLAKE2b-256 e9b1c2ff1ee724fff4ba4ee27fd85375562ac1c809a42ae759933e67a15aab61

See more details on using hashes here.

Provenance

The following attestation bundles were made for gtfs_station_stop-0.9.5.tar.gz:

Publisher: build-and-publish.yaml on bcpearce/gtfs-station-stop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gtfs_station_stop-0.9.5-py3-none-any.whl.

File metadata

File hashes

Hashes for gtfs_station_stop-0.9.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f35b9cd9f01a7d218ae269dc01aaf19737931d264fe18386028517569e492082
MD5 896993ce53ea120705403107e3505c81
BLAKE2b-256 5bb899d307580f713fdd433417e08d573aee33e006d2420119443f37bf86fea3

See more details on using hashes here.

Provenance

The following attestation bundles were made for gtfs_station_stop-0.9.5-py3-none-any.whl:

Publisher: build-and-publish.yaml on bcpearce/gtfs-station-stop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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