Skip to main content

API wrappers for interacting with real-time public transport data in Victoria, Australia

Project description

Victorian public transport information API wrappers for Python (pre-release)

Python utilities for interacting with real-time information for public transport in Victoria, Australia, via the Public Transport Victoria (PTV) Timetable API, Yarra Trams' TramTracker data service and the V/Line website.

Package version: 0.3.0
Last updated: 12 December 2024
Tested on Python version: ≥ 3.12


Overview

This package of modules aims to simplify the process of retrieving and manipulating real-time data for public transport in Victoria, Australia and document each operation and response supported by the APIs.

The package implements interfaces for three data sources:

  • PTV Timetable API - the main service for real-time and scheduled public transport information across Victoria;
  • Yarra Trams TramTracker - live passenger information for the Melbourne tram network, including planned diversions which the Timetable API lacks; and
  • V/Line website - since November 2024, real-time V/Line departures and arrivals information at Southern Cross station for the next 30 minutes, including platform information and estimated time of departure/arrival.

The package minimises the use of third-party modules to improve portability, especially on systems with restrictions.

What's different from accessing the Timetable API directly?

  • Simplifying output "types": instead of having a different response schema for each API operation, any object that represents the same concept are consolidated into the same response type (e.g. all responses that represent a public transport stop are instances of the same class: Stop, instead of the ten or so different representations in the API). Any attribute/field for which the API does not provide a response for will have a sentinel value.
  • Best-effort documentation: all operations and fields have, as far as practicable, been documented in type hints and docstrings (although some of these are guesses).
  • Date and time representation: date inputs and outputs are converted from and to datetime objects with the local time zone of Victoria, so that you do not have to deal with the different string representations of dates and speaking to the API in the UTC time zone as implemented by the Timetable API.
  • Other quality of life modifications: such as consistent attribute names, fixing typos and removing trailing whitespaces.

Pre-release package

This package is in pre-release. Breaking changes may be made without notice during development.

Direct dependencies

Package name Tested on version Notes
ratelimit ≥ 2.2.1
requests ≥ 2.32.3
tzdata ≥ 2024.1 Only required on OSes without a native tz database, including Windows.

Usage

The recommended method to install this package is via the Python Package Index:

python -m pip install ptv-timetable

You can also install from the GitLab Package Registry (authentication not required):

python -m pip install --index-url https://gitlab.com/api/v4/projects/54559866/packages/pypi/simple ptv-timetable

These commands will also install any required dependencies.

This package adds three modules into the root namespace of your interpreter (so they can be directly imported into your code with import <module_name>):

  • ptv_timetable for interacting with the PTV Timetable API;
    • ptv_timetable.types defines dataclasses used to represent returned API objects;
  • tramtracker for interacting with the TramTracker data service; and
  • vline for retrieving V/Line Southern Cross departure and arrival information.

Each module defines data types that encapsulate the responses from the APIs to allow access by attribute reference (.) to take advantage of autocompletion systems in IDEs where available. This format also allows each field to be documented, which is not a feature that is available in the raw dicts returned by the APIs.

To use the Timetable API service, you will first need to obtain credentials from PTV:

  • Send an email to APIKeyRequest@ptv.vic.gov.au with the subject line PTV Timetable API - request for key.
  • You will receive a user ID and a UUID-format signing key in response. This may take several days depending on volume of requests; you will not receive confirmation that your request was received, so hang tight!
    (Details: http://ptv.vic.gov.au/ptv-timetable-api/)
  • Specify the credentials when instantiating the ptv_timetable.TimetableAPI class: TimetableAPI(dev_id, key)

Credentials are not required for the tramtracker and vline modules.

Logging

Some actions are logged under the logger names ptv-timetable.ptv_timetable, ptv-timetable.tramtracker and ptv-timetable.vline. Use logging.getLogger() to obtain the loggers and you can register your own handlers to retrieve their contents.

Issues and error reporting

To report problems with the package or otherwise give feedback, go to the Issues tab of the repository.

Contributing

All constructive contributions are welcome! By contributing, you agree to license your contributions under the Apache Licence 2.0.

Copyright and licensing

This project's source code is licensed under the Apache Licence 2.0; however, data obtained from the APIs themselves via these modules are licensed separately: PTV Timetable API data are under a Creative Commons Attribution 4.0 International licence and TramTracker data is proprietary. See LICENCE.md for further information.

Summary of module contents

ptv_timetable/__init__.py

Constant/function/method Description
METROPOLITAN_TRAIN
METRO_TRAIN
MET_TRAIN
METRO
Use in route_type parameters to specify the metropolitan train network.
TRAM Use in route_type parameters to specify the metropolitan tram network.
BUS Use in route_type parameters to specify the metropolitan or regional bus network.
REGIONAL_TRAIN
REG_TRAIN
COACH
VLINE
Use in route_type parameters to specify the regional train or coach network.
EXPAND_<property> Use in expand parameters to tell the API to return the specified properties in full.
class TimetableAPI(dev_id, key, *, calls=1, period=10, ratelimit_handler=ratelimit.decorators.sleep_and_retry) Constructs a new instance of the TimetableAPI class with the supplied credentials.

To obtain your own set of credentials, follow the instructions on this page.
TimetableAPI.list_route_directions(route_id) List directions for a specified route.

API operation: /v3/directions/route/{route_id}
TimetableAPI.get_direction(direction_id, route_type=None) List directions with a specified identifier.

API operation: /v3/directions/{direction_id}/route_type/{route_type}
TimetableAPI.get_pattern(run_ref, route_type, stop_id=None, date=None, include_skipped_stops=None, expand=None, include_geopath=None) Retrieve the stopping pattern and times of arrival at each stop for a particular run.

API operation: /v3/pattern/run/{run_ref}/route_type/{route_type}
TimetableAPI.get_route(route_id, include_geopath=None, geopath_date=None) Return the route with the specified identifier.

API operation: /v3/routes/{route_id}
TimetableAPI.list_routes(route_types=None, route_name=None) List all routes.

API operation: /v3/routes/
TimetableAPI.list_route_types() List all route types (modes of travel) and their identifiers.

API operation: /v3/route_types/
TimetableAPI.get_run(run_ref, route_type=None, expand=None, date=None, include_geopath=None) Return the run with the specified identifier.

API operation: /v3/runs/{run_ref}/route_type/{route_type}
TimetableAPI.list_runs(route_id, route_type=None, expand=None, date=None) List runs for a specified route.

API operation: /v3/runs/route/{route_id}/route_type/{route_type}
TimetableAPI.get_stop(stop_id, route_type, stop_location=None, stop_amenities=None, stop_accessibility=None, stop_contact=None, stop_ticket=None, gtfs=None, stop_staffing=None, stop_disruptions=None) Return the stop with the specified identifier and route type.

API operation: /v3/stops/{stop_id}/route_type/{route_type}
TimetableAPI.list_stops(route_id, route_type, direction_id=None, stop_disruptions=None) List all stops on a specified route.

API operation: /v3/stops/route/{route_id}/route_type/{route_type}
TimetableAPI.list_stops_near_location(latitude, longitude, route_types=None, max_results=None, max_distance=None, stop_distuptions=None) List all stops near a specified location.

API operation: /v3/stops/location/{latitude},{longitude}
TimetableAPI.list_departures(route_type, stop_id, route_id=None, platform_numbers=None, direction_id=None, gtfs=None, include_advertised_interchange=None, date=None, max_results=None, include_cancelled=None, expand=None, include_geopath=None) List the departures from a specified stop.

API operation: /v3/departures/route_type/{route_type}/stop/{stop_id}/route/{route_id}
TimetableAPI.list_disruptions(route_id=None, stop_id=None, route_types=None, disruption_modes=None, disruption_status=None) List disruptions on the network.

API operation: /v3/disruptions/route/{route_id}/stop/{stop_id}
TimetableAPI.list_disruption_modes() List all disruption modes.

API operation: /v3/disruptions/modes
TimetableAPI.fare_estimate(zone_a, zone_b, touch_on=None, touch_off=None, is_free_fare_zone=None, route_types=None) Return the fare for a specified journey.

API operation: /v3/fare_estimate/min_zone/{minZone}/max_zone/{maxZone}
TimetableAPI.list_outlets(latitude=None, longitude=None, max_distance=None, max_results=None) List ticket outlets near a specified location.

API operation: /v3/outlets/location/{latitude},{longitude}
TimetableAPI.search(search_term, route_types=None, latitude=None, longitude=None, max_distance=None, include_outlets=None, match_stop_by_locality=None, match_route_by_locality=None, match_stop_by_gtfs_stop_id=None) Search for a stop, route or ticket outlet by name.

API operation: /v3/search/{search_term}

tramtracker/__init__.py

Constant/function/method Description
class TramTrackerService(*, calls=1, period=10, ratelimit_handler=ratelimit.decorators.sleep_and_retry) Constructs a new instance of the TramTrackerService class.
TramTrackerService.list_destinations() List all destinations on the tram network.
TramTrackerService.list_stops(route_id, up_direction) List stops for a specified route and direction of travel.
TramTrackerService.get_stop(stop_id) Return details about a specified stop.
TramTrackerService.list_routes_for_stop(stop_id) List the routes serving a specified stop.
TramTrackerService.next_trams(stop_id, route_id=None, low_floor_tram=False, as_of=datetime.now(tz=ZoneInfo("Australia/Melbourne"))) List the next tram departures from a specified stop.
TramTrackerService.get_route_colour(route_id, as_of=datetime.now(tz=ZoneInfo("Australia/Melbourne"))) Return the route's colour on public information paraphernalia.
TramTrackerService.get_route_text_colour(route_id, as_of=datetime.now(tz=ZoneInfo("Australia/Melbourne"))) Return the route's text colour on public information paraphernalia.

vline/__init__.py

Constant/function/method Description
next_services() Gets the details of the next 30 minutes of services departing and arriving Southern Cross station.

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

ptv_timetable-0.3.0.tar.gz (41.3 kB view details)

Uploaded Source

Built Distribution

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

ptv_timetable-0.3.0-py3-none-any.whl (37.1 kB view details)

Uploaded Python 3

File details

Details for the file ptv_timetable-0.3.0.tar.gz.

File metadata

  • Download URL: ptv_timetable-0.3.0.tar.gz
  • Upload date:
  • Size: 41.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for ptv_timetable-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ef21cd2581f271011c1258de1fca8b5ca96707bbc6bec64fde03f04e9c201ed8
MD5 f8774df85d0497f06bf72a50d7c92a87
BLAKE2b-256 c2d2012a5b395ee3be040e54e192ff3db74eb74debc3a8ab13b35f2d46f3a0e4

See more details on using hashes here.

File details

Details for the file ptv_timetable-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: ptv_timetable-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 37.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for ptv_timetable-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb950e165da5808e649593e852fe22cf49c3bbebff1f138b010aaa6662e41927
MD5 639639c55e875be3fe1cba3d6f78c065
BLAKE2b-256 a85bf15121130bd6d523bf3bdfffa8ab76e9c12ccbbd5c61a9c8e34ca6f9569d

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